UNIHIVE

Study Hub UNIPORT MTH 280

MTH 280 · Introduction to Computer programming

Functions and subroutines

Why programs are split into modules, defining and calling functions, parameters and return values, local and global variables, and recursion.

Our notes

1 min read

Why subprograms

A subprogram (function, procedure or subroutine) is a named block of code that does one job. Benefits:

  • Reuse: write once, call many times.
  • Easier reading and testing: each part is small.
  • Teamwork: different people write different modules.
  • Top-down design maps straight onto functions.

Defining and calling a function

def area_of_circle(r):
    return 3.14159 * r * r
a = area_of_circle(7)
print(a)
Output: 153.93791 (about 153.94). r is a parameter; 7 is the argument passed when calling.

Functions vs subroutines (procedures)

  • A function returns a value and is used inside an expression: y = sqrt(x).
  • A subroutine/procedure performs an action and is called as a statement; it may return several results through its arguments. In FORTRAN: CALL SWAP(A, B). In Python a function with no return gives None.

Several parameters and results

def stats(values):
    total = sum(values)
    return total, total / len(values)
s, avg = stats([4, 8, 6])
Gives s = 18, avg = 6.0.

Built-in (library) functions

Python: abs(), round(), len(), max(), min(), sum(); math.sqrt(), math.sin() (radians), math.log(). FORTRAN: SQRT, ABS, SIN, COS, EXP, LOG, MOD, INT, REAL.

Local and global variables

A variable created inside a function is local: it exists only while the function runs. A global variable is created outside and can be read everywhere. Prefer passing values in and returning results out instead of changing globals.

Passing arguments

  • By value: the function gets a copy; changing it does not change the caller's variable.
  • By reference: the function works on the original (FORTRAN passes by reference by default; a Python list passed in can be changed inside).

Recursion

A function that calls itself, with a base case that stops it.
def fact(n):
    if n <= 1:
        return 1
    return n * fact(n - 1)
fact(4) = 4 × fact(3) = 4 × 3 × fact(2) = 4 × 3 × 2 × fact(1) = 24.
Without a base case recursion never stops (a "stack overflow").

A GPA function (5-point scale):
def gpa(units, points):
    total_units = sum(units)
    total = 0
    for u, p in zip(units, points):
        total = total + u * p
    return total / total_units
For units [3, 2, 3] and grade points [5, 4, 3]: (15 + 8 + 9)/8 = 32/8 = 4.0.

Exams usually ask: advantages of subprograms; function vs subroutine; write a function (area, factorial, maximum, GPA); local vs global; parameters vs arguments; trace a recursive function.

Common mistake: printing inside a function instead of returning the value, then trying to use the result in a calculation.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All MTH 280 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.