That Blue Square Thing

Computer Science GCSE

Functions - decomposing problems

Functions are used to break a problem into smaller sections. This is helpful for two basic reasons:

  1. to break a complex problem down into smaller pieces which can be dealt with one at a time;
  2. to code a section of code that needs to be used several times just once

Using functions is essential if you want to get the higher marks on controlled assessment as well. So you'd better learn! (it's also really good programming style - which I guess is why they give you marks for using them).

Basic functions

The key rules with functions are:

So, a basic program using a function might look like this:

# function to print user details
def printDetails():
print()
print("Name: " + userName)
print("Age: " + userAge)

# main program
userName = input("Enter your name: ")
userAge = input("Enter your age: ")

printDetails() # calls the function called printDetails()

print()
check = input("Are these details correct? (Y/N): ")

Functions like this can be used all over a program if a job needs to be done several times.

A Problem with Functions - Local and Global Variables

Changing the value of a variable inside a function can cause problems. Inputting values inside functions cause even more.

This is because variables inside a function behave as you tell them to only inside the function. You can change their value inside a function, but the value will switch back again once you leave the function.

This code demonstrates the problem.

# code fragment 1

# function to add 10 years to age
def addTen():
age = age + 10
print("Inside function")
print(age)
print()

# main program
age = int(input("Enter your age in years: "))

print("Input values")
print(age)
print()

addTen() # call the function addTen

print("After function")
print(age)
print()

The first problem is that the function doesn't know that age exists! To solve this we have to send the variable age to the function as a parameter or argument. This goes inside the brackets.

This code shows how that's done.

# code fragment 2

# function to add 10 years to age
def addTen(age):
age = age + 10
print("Inside function")
print(age)
print()

# main program
age = int(input("Enter your age in years: "))

print("Input values")
print(age)
print()

addTen(age) # call the function addTen

print("After function")
print(age)
print()

But the code still doesn't change the value of age permanently. As soon as we go back to the main program the value of age has reverted back to whatever we entered.

We have to solve that by returning a value from the function. We add a return statement at the end of the function code and then assign the value which is returned to a variable. This is really important - don't forget to assign the return value to something when you call the function!

# code fragment 3

# function to add 10 years to age
def addTen(age):
age = age + 10
print("Inside function")
print(age)
print()
return age # return age back to main program

# main program
age = int(input("Enter your age in years: "))

print("Input values")
print(age)
print()

age = addTen(age) # call addTen and assign value returned

print("After function")
print(age)
print()

Note that you can send more than one variable to a function - just divide them with commas. They need to be listed in the same order in the function header as well.

area = calcArea(height, width)

goes with

def calcArea(ht, wd):

Note that you actually don't need to use the same names inside the function as you do outside. So long as you refer to ht and wd inside the function you're good. In fact, it's better not to use the same names - it's less confusing!

You could try and write a program using a calcArea() function. It would be good practise.

We say that variables inside a function are local variables. They have local scope - i.e. the changes only occur inside the function. As soon as you leave the function the variable changes back to it's value in the main program - where it is a global variable which has global scope.

Using Functions for Inputting Values

If you use a function to input values (which is a fine thing to do) then you need to make sure you return the values to the main program. With a single input that's easy, but if you need multiple inputs then you need to return more than one value.

Here's a solution to that problem.

# function to enter values
def enterDetails():
userDetails = [ ] # blank list
userDetails.append(input("Enter your user name: "))
userDetails.append(input("Enter your password: "))
userDetails.append(input("Enter your age: "))
return userDetails

details = enterDetails() # call function

userName = details[0] # assign values to variables
userPassword= details[1]
userAge = details[2]

print()
print(userName)
print(userPassword)
print(userAge)

Using lists is a little tricky - I think I need another page on this in itself! For now it works, which is the key thing.

Help files on functions

Some useful bits and bobs.

PDF iconDecomposition - an introduction

PDF iconAdvantages and disadvantanges of decomposition - resource sheet

PDF iconDice function - how to develop a basic function to roll dice

PDF iconFunction syntax - basic syntax summarised

PDF iconFunctions in pseudo code - for the exam