CSC108H1 Chap.3 Defining and Calling Your Own Functions
Defining and Calling Your Own Functions
From built-in to your own
Week 2 moves from calling functions to writing them. A definition has a header, made of def, a name, parameters in parentheses and a colon, and an indented body that returns a value.
Parameters belong to the definition; arguments are the values in a call.
What a call does
The lecture slides give the rules: work out each argument, leftmost first, place their addresses in the parameters, run the body, and put the returned value where the call was.
A body that never reaches a return statement produces None, which the shell does not display, and a function may call another as a helper.
Where marks slip
Three mistakes recur in function questions. Students confuse the parameter in a header with the argument in a call, they forget that arguments are evaluated before the body starts, and they write a body that computes a value without returning it.
The assignment handouts add a fourth trap: the functions you submit must not call print or input, so a printed answer cannot replace a returned one.
What this chapter covers
- 01
Function headers, bodies and indentation
- 02
Parameters in the definition and arguments in the call
- 03
The four rules for executing a call
- 04
Return statements and the value None
- 05
Helper functions and function reuse
- 06
Writing functions in a file and calling them in the shell
Worked example · free
Trace a helper call that returns a total
- 1Store 40.0 in bill and 0.15 in rate for the outer call.
- 2Call tip with the same values: 40.0 * 0.15 returns 6.0.
- 1Add the returned 6.0 to 40.0 and return 46.0.
Key terms
- Parameter
- A variable named in a function header that receives the address of an argument when the function is called.
- Argument
- A value written in a function call, evaluated before the call runs.
- Return Statement
- A statement that sends a value back to the caller and ends the function at once.
- Helper Function
- A function called by another function to do part of its work.
- Function Call
- An expression that runs a function on given arguments and produces the value it returns.
Defining and Calling Your Own Functions FAQ
What is the difference between a parameter and an argument?
A parameter is a name in the function header, such as bill in total_due(bill, rate). An argument is the value written in a particular call, such as 40.0. During the call the parameter holds the address of the argument's value.
Why does my function seem to return nothing in the shell?
If the body computes a value without a return statement, the call produces None, and the shell does not print None. Add return in front of the expression whose value the function should hand back, then call it again.
Can one function I write call another one?
Yes, and the course encourages it. The calling function only needs to know what the helper returns, not how it works, and Assignment 1 required one function body to reuse the function repaired earlier in the assignment as its helper.
What happens when Python runs a function call?
The course slides give four rules. First every argument is reduced to a value, leftmost first; next the parameters receive the addresses of those values; then the body runs; and once a return statement runs, its value takes the place of the call in the expression that made it.
Why should functions be written in a file rather than the shell?
Code entered at the shell prompt is lost as soon as the shell is restarted. A .py file is saved, so you can edit a function, run the file again and call the function in the shell to test it, which is the workflow the assignments and Wing 101 expect.
Exam move
For every function in the Week 2 worksheets, write out the four call rules as numbered lines for one example call, including the value each parameter receives. Then invent a call whose argument is itself a call and trace it the same way. Finish by deleting the return from one of your functions and predicting what the shell shows. Finally, rewrite one function so that it uses a helper you wrote earlier.
Keep the traces: they are the fastest revision material for the first quiz and the midterm.
Working through Defining and Calling Your Own Functions in CSC108H1? Sia is AskSia’s AI Computer Science tutor — ask any CSC108H1 Defining and Calling Your Own Functions question and get a clear, step-by-step explanation grounded in how CSC108H1 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.