CSC 280 · Introduction to Computer Programming
Our notes
1 min readWhy 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
-
Best pick Introduction to Python Programming, 6.1 Defining functions
(opens openstax.org in a new tab)
OpenStax, Introduction to Python Programming, 6.1 Defining functions, CC BY-NC-SA 4.0 · licence
-
4. More Control Flow Tools — Python 3.15.0 documentation
(opens docs.python.org in a new tab)
-
Introduction to Python Programming, 6.4 Parameters
(opens openstax.org in a new tab)
OpenStax, Introduction to Python Programming, 6.4 Parameters, CC BY-NC-SA 4.0 · licence
-
Introduction to Python Programming, 6.5 Return values
(opens openstax.org in a new tab)
OpenStax, Introduction to Python Programming, 6.5 Return values, CC BY-NC-SA 4.0 · licence
-
Function (computer programming)
(opens en.wikipedia.org in a new tab)
Wikipedia, Function (computer programming), CC BY-SA 4.0 · licence
Watch
-
Programming Basics: Statements & Functions: Crash Course Computer Science #12
12 min · ≈ 54 MB · about ₦30
(opens youtube.com in a new tab)
-
Functions - CS50P Shorts
6 min · ≈ 27 MB · about ₦15
(opens youtube.com in a new tab)
-
Python Tutorial for Beginners 8: Functions
22 min · ≈ 99 MB · about ₦56
(opens youtube.com in a new tab)
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.