GCSE Computer Science
Python functions โ GCSE Computer Science
What is a function?
A function is a block of code with a name. You write it once. You call it as many times as you need. Every time you find yourself writing the same code in two different places, that's a function waiting to be written.
There's a deeper reason functions matter beyond avoiding repetition. A program made of well-named functions reads like a list of instructions. A program without them reads like one long thing that does everything at once. Examiners can tell the difference. So can the person maintaining the code six months later.
Defining and calling a function
def greet():
print("Hello")
greet()
def tells Python a function is being defined. The name comes next, then parentheses, then a colon. Everything indented beneath it is the function body โ it doesn't run until the function is called. greet() is the call. Nothing happens before that line.
Parameters and arguments
def greet(name):
print("Hello, " + name)
greet("Alice")
greet("Bob")
A parameter is a variable the function expects. An argument is the value you pass in. name is the parameter. "Alice" is the argument. The same function, two different results, depending on what you pass in. That's the point.
Returning a value
def square(n):
return n * n
result = square(5)
print(result)
return sends a value back to wherever the function was called from. This is the distinction that costs most students marks โ a function that prints something does one thing; a function that returns something does something different. Print shows output on screen. Return passes a value back into the program. They are not interchangeable.
Writing print inside a function when the question asks for a value to be returned. The function appears to work when tested but fails silently when its return value is used elsewhere.
What examiners actually test
Three things appear repeatedly: writing a function with parameters that returns a value, tracing a function call and stating what gets returned, and explaining the difference between a function that prints and one that returns. The last one is where the marks go. Practice writing functions that return values, not just functions that print them.
Marking forty scripts to find the three students who used print instead of return isn't how I want to spend a Tuesday evening. Python Coach catches it automatically, as part of 195 challenges across 27 lessons with progress tracking for every student โ free for 60 teaching days, no payment details required to get your class started.