6.6.1 Built-in and user-devised subprograms
Two Kinds of Subprogram
A subprogram is a named, reusable block of code. There are two sources:
- Built-in / library subprograms — provided by the language or its standard libraries; ready to use without writing them yourself.
- User-devised subprograms — written by the programmer to solve a specific problem in their own program.
Both are called in the same way — by name, with arguments in parentheses. The distinction matters because built-ins are tested, optimised and documented; user-devised ones must be written, tested and maintained by you.
Built-in Subprograms
import random
# Built-in functions — no import needed
print(len("hello")) # 5 — length
print(abs(-42)) # 42 — absolute value
print(round(3.7)) # 4 — round to nearest integer
print(round(3.14159, 2)) # 3.14 — round to 2 decimal places
print(int("99")) # 99 — type conversion
print(str(42)) # "42"
print(max(3, 7, 1)) # 7 — maximum
print(min(3, 7, 1)) # 1 — minimum
print(input("Enter: ")) # reads a line from the user
# Library functions — need import
print(random.randint(1, 6)) # random integer 1 to 6 inclusive
print(random.random()) # random float 0.0 to 1.0
using System;
// Math library methods
Console.WriteLine(Math.Abs(-42)); // 42
Console.WriteLine(Math.Round(3.14159, 2)); // 3.14
Console.WriteLine(Math.Max(3, 7)); // 7
Console.WriteLine(Math.Min(3, 7)); // 3
Console.WriteLine(Math.Pow(2, 8)); // 256
// String methods (built into string type)
string s = "Hello";
Console.WriteLine(s.Length); // 5
Console.WriteLine(s.ToUpper()); // HELLO
// Random
Random rng = new Random();
Console.WriteLine(rng.Next(1, 7)); // random 1-6
Imports System
Console.WriteLine(Math.Abs(-42)) ' 42
Console.WriteLine(Math.Round(3.14159, 2)) ' 3.14
Console.WriteLine(Math.Max(3, 7)) ' 7
Console.WriteLine(Math.Min(3, 7)) ' 3
Console.WriteLine(Math.Pow(2, 8)) ' 256
Dim s As String = "Hello"
Console.WriteLine(s.Length) ' 5
Console.WriteLine(s.ToUpper()) ' HELLO
Dim rng As New Random()
Console.WriteLine(rng.Next(1, 7)) ' random 1-6
User-Devised Subprograms
When no built-in solves your specific problem, you write your own. User-devised subprograms use the same call syntax as built-ins — the caller does not need to know how they are implemented.
import random
# User-devised function: roll two dice and return total
def roll_dice():
"""Returns the sum of two random dice rolls."""
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
return die1 + die2
# User-devised procedure: print a formatted score line
def display_score(name, score, max_score):
"""Prints a formatted score — no return value."""
percent = round(score / max_score * 100)
print(name + ": " + str(score) + "/" + str(max_score) + " (" + str(percent) + "%)")
# Using both built-in and user-devised together
total = roll_dice() # user-devised
print("Rolled:", total)
display_score("Alice", 78, 100) # user-devised
display_score("Bob", round(82.7), 100) # round() is built-in
Random rng = new Random();
int RollDice() {
return rng.Next(1, 7) + rng.Next(1, 7);
}
void DisplayScore(string name, int score, int maxScore) {
int percent = (int)Math.Round((double)score / maxScore * 100);
Console.WriteLine(name + ": " + score + "/" + maxScore + " (" + percent + "%)");
}
int total = RollDice();
Console.WriteLine("Rolled: " + total);
DisplayScore("Alice", 78, 100);
DisplayScore("Bob", (int)Math.Round(82.7), 100);
Dim rng As New Random()
Function RollDice() As Integer
Return rng.Next(1, 7) + rng.Next(1, 7)
End Function
Sub DisplayScore(name As String, score As Integer, maxScore As Integer)
Dim percent As Integer = CInt(Math.Round(CDbl(score) / maxScore * 100))
Console.WriteLine(name & ": " & score & "/" & maxScore & " (" & percent & "%)")
End Sub
Dim total As Integer = RollDice()
Console.WriteLine("Rolled: " & total)
DisplayScore("Alice", 78, 100)
DisplayScore("Bob", CInt(Math.Round(82.7)), 100)
Key Takeaways
- Built-in: provided by the language —
len(),abs(),round(),max(),min(),input(),print(). - Library: imported from a module —
random.randint(),math.sqrt(). - User-devised: written by the programmer — called identically to built-ins from the caller's perspective.
- Use built-ins where they exist; write your own only when no built-in solves the specific problem.