University of Glasgow · FACULTY OF COMPUTER SCIENCE

COMPSCI4039 Chap.3 Functions and Return Values

- one subject, every graph, every model, every mark
7 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 3 of 10 · COMPSCI4039

Functions and Return Values

Functions and Return Values introduces the first building block that changes how a program executes. A function packages a repeated or self-contained piece of work under a name, so the code lives in one place and the main program reads at a higher level.

The definition, made with def, a name, parameters in brackets and a colon, is skipped when Python first reaches it; the body runs only when the function is called, and control then returns to the point of the call. Parameters are the placeholder names in the header, while arguments are the actual values supplied by each call, and a function may take several.

The most important distinction in this chapter is between returning and printing. return sends a value back so that the caller can store it, combine it or pass it on; print merely displays text. A function with no return statement hands back None, which is why printing the result of such a call shows None.

Functions can call other functions, which is the start of functional decomposition, and a return statement also ends the call immediately.

In this chapter

What this chapter covers

  • 01

    Why repeated code belongs in a function

  • 02

    The def header and the indented body

  • 03

    Execution flow through calls and returns

  • 04

    Parameters and arguments

  • 05

    Return versus print, and the value None

  • 06

    Functions that call other functions

  • 07

    Early return and testing with known inputs

Worked example · free

Return a discounted price from a function

Q [4 marks]. Given def discount(price, pct): return price - price * pct / 100, what does print(discount(80, 15)) display?
The header line is the keyword def, the function name, a bracketed parameter and a colon.The header line is the keyword def, the function name, a bracketed parameter and a colon. The indented return line is the body, and vat(50) is a call that passes 50 in as the argument.defvat(price):return price * 1.2vat(50)keywordfunction nameparameterindented bodycall with argument 50
The header line is the keyword def, the function name, a bracketed parameter and a colon. The indented return line is the body, and vat(50) is a call that passes 50 in as the argument.
  • 1The call binds price to 80 and pct to 15. The mark weighting used here is not an official university allocation.
  • 1price * pct / 100 is 80 * 15 / 100, which is 12.0 because true division gives a float.
  • 1The return expression is 80 - 12.0, which is 68.0.
  • 1The returned value replaces the call inside print, so 68.0 is displayed.
The program prints 68.0, the price after a 15 percent reduction.
Sia tip — Replace each call with the value it returns, then finish evaluating the line that contained it.
Glossary

Key terms

Function call
An instruction that runs a named function's body, written with round brackets and any arguments.
Argument
An actual value supplied to a function when it is called.
None
The special value a function returns when its body has no return statement.
Functional decomposition
Breaking a large task into smaller functions that each do one job.
Function header
The first line of a definition, giving def, the name, the parameters and a colon.
FAQ

Functions and Return Values FAQ

Why does my function print the right value but return None?

Printing inside a function only displays text; it does not hand anything back. Without a return statement the call evaluates to None, so storing or printing its result shows None. Replace the inner print with return when the caller needs the value.

What is the difference between a parameter and an argument?

A parameter is the name written in the function header, such as size, and it behaves like a local variable. An argument is the actual value written in the call, such as 6, which is copied into that parameter when the call starts.

Does Python run a function as soon as it reads the definition?

No. Python reads the definition and remembers it, but the body runs only when the function is called. A body may therefore run many times, or never, depending entirely on the calls the program makes.

Can one function call another?

Yes, and the lab encourages it. A summary function can call an area function and a circumference function, while the main program calls only the summary. Each call runs to completion and returns before the caller continues.

Why should a function return a value instead of printing it?

A returned value can be stored, combined in an expression, tested or passed to another function, so the function becomes a reusable building block. A function that prints can only display its result once. The lecture's box function returns its finished string precisely so that the main program can join several boxes before printing them together.

Study strategy

Exam move

Learn this chapter by tracing calls on paper with arrows. Write the main program in one column and each function body in another, then draw an arrow into the body at each call and a return arrow carrying the value back. Keep a running list of everything printed, in order.

Rework the classic confusion deliberately: take any function that prints its result, rewrite it to return the result instead, and compare what the caller sees in each version. Practise writing functions from a brief in four steps, as the lecture suggests: choose a name and parameters for the header, write the body, add the return statement, and finally write a call that tests it.

Give each function one job and a name that says what it returns. For revision, collect short programs in which a returned value is ignored, stored, printed or passed to another function, and state for each what reaches the screen. Those four outcomes cover most questions on this topic.

Working through Functions and Return Values in COMPSCI4039? Sia is AskSia’s AI Computer Science tutor — ask any COMPSCI4039 Functions and Return Values question and get a clear, step-by-step explanation grounded in how COMPSCI4039 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 26 of your subjects at University of Glasgow, plus 1,000+ more Bibles across the AskSia library.
Sia - your COMPSCI4039 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works