6.6.2 Functions and procedures

Functions vs Procedures

Subprograms divide into two types based on whether they return a value:

FunctionProcedure
Returns a value?Yes — must have a return statementNo — performs an action; no return
Typical useCalculate and return a result for the caller to usePerform an action (e.g. print, write to file, update a list)
Called in an expression?Yes — total = calculate(x)No — called as a statement: display(name)
Pythondef f(): return valuedef p(): ... (no return)
C#int F() { return value; }void P() { ... }
VB.NETFunction F() As TypeSub P()

Both may have parameters (zero or more) — they are not limited to procedures or functions specifically.

All Four Combinations

# 1. Function WITH parameters — returns a value
def add(a, b):
    """Returns the sum of a and b."""
    return a + b

result = add(3, 5)   # result = 8
print(result)

# 2. Function WITHOUT parameters — returns a value
def get_greeting():
    """Returns a fixed greeting string."""
    return "Welcome to the system!"

msg = get_greeting()
print(msg)

# 3. Procedure WITH parameters — no return value
def print_border(char, width):
    """Prints a border line of the given character and width."""
    print(char * width)

print_border("=", 30)

# 4. Procedure WITHOUT parameters — no return value
def show_menu():
    """Displays the main menu."""
    print("1. New game")
    print("2. Load game")
    print("3. Quit")

show_menu()
// 1. Function with parameters
int Add(int a, int b) { return a + b; }
Console.WriteLine(Add(3, 5));

// 2. Function without parameters
string GetGreeting() { return "Welcome to the system!"; }
Console.WriteLine(GetGreeting());

// 3. Procedure with parameters
void PrintBorder(char c, int width) {
    Console.WriteLine(new string(c, width));
}
PrintBorder('=', 30);

// 4. Procedure without parameters
void ShowMenu() {
    Console.WriteLine("1. New game");
    Console.WriteLine("2. Load game");
    Console.WriteLine("3. Quit");
}
ShowMenu();
' 1. Function with parameters
Function Add(a As Integer, b As Integer) As Integer
    Return a + b
End Function
Console.WriteLine(Add(3, 5))

' 2. Function without parameters
Function GetGreeting() As String
    Return "Welcome to the system!"
End Function
Console.WriteLine(GetGreeting())

' 3. Procedure with parameters
Sub PrintBorder(c As Char, width As Integer)
    Console.WriteLine(New String(c, width))
End Sub
PrintBorder("="c, 30)

' 4. Procedure without parameters
Sub ShowMenu()
    Console.WriteLine("1. New game")
    Console.WriteLine("2. Load game")
    Console.WriteLine("3. Quit")
End Sub
ShowMenu()

 Key Takeaways

  • Function: must have a return statement; its result is used by the caller.
  • Procedure: no return value; called for its side effect (printing, updating, writing).
  • Both can have zero or more parameters — parameters are independent of function/procedure type.
  • Python: both use def; C#: functions have a type, procedures use void; VB.NET: Function vs Sub.