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.
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.
Want structured Python lessons? Python Coach covers functions and 26 other lessons with 195 challenges โ free 60 teaching day trial.
Start learning →