Programming Functions & Procedures (Edexcel GCSE Computer Science)

Revision Note

Programming Functions & Procedures

What are functions and procedures?

  • Functions and procedures are a type of sub program, a sequence of instructions that perform a specific task or set of tasks

  • Sub programs are often used to simplify a program by breaking it into smaller, more manageable parts

  • Sub programs can be used to:

    • Avoid duplicating code and can be reused throughout a program

    • Improve the readability and maintainability of code

    • Perform calculations, to retrieve data, or to make decisions based on input

  • Parameters are values that are passed into a sub program

    • Parameters can be variables or values and they are located in brackets after the name of the sub program

    • Example: function taxCalculator(pay,taxcode) OR def taxCalculator(pay,taxcode)

  • Sub programs can have multiple parameters

  • To use a sub program you 'call' it from the main program

What's the difference between a function and procedure?

  • A Function returns a value whereas a procedure does not

Concept

Python

Creating a function

def squared(number):

squared = number^2

return squared

Calling a function

SquNum = squared(4)

print(SquNum)

OR

print(SquNum(4))

Creating a procedure

def ageCheck(age):

if age > 18:

print("You are old enough")

else:

print("You are too young")

Calling a procedure

ageCheck(21)

Examples

  • A Python program using a function to calculate area and return the result

  • Two options for main program are shown, one which outputs the result (# 1) and one which stores the result so that it can be used at a later time (# 2)

Functions

# Function definition, length and width are parameters

def area(length, width):
  area = length * width # Calculate area
  return area # Return area

# Main program #1
length = int(input("Enter the length: "))
width = int(input("Enter the width: "))
print(area(length, width))

# Main program #2
length = int(input("Enter the length: ")) length
width = int(input("Enter the width: "))
area = area(length, width) # Stores the result of the function in a variable
print("The area is " + str(area) + " cm^2")

  • A Python program using procedures to display a menu and navigate between them

  • Procedures are defined at the start of the program and the main program calls the first procedure to start

  • In this example, no parameters are needed

Procedures

# Procedure definition

def main_menu():

# Outputs the option
  print("1. Addition")
  print("2. Subtraction")
  print("3. Multiplication")
  print("4. Division")
  print("5. Exit")

 # Asks the user to enter their choice

choice = int(input("Enter your choice: "))
  if choice == 1:
    addition()
  elif choice == 2:
    subtraction()
  elif choice == 3:
    multiplication()
  elif choice == 4:
    division()
  elif choice == 5:
    exit()

# Procedure definition

def addition():
  num1 = int(input("Enter the first number: "))
  num2 = int(input("Enter the second number: "))
  print(num1 + num2)

def subtraction():
  num1 = int(input("Enter the first number: "))
  num2 = int(input("Enter the second number: "))
  print(num1 - num2)

def multiplication():
  num1 = int(input("Enter the first number: "))
  num2 = int(input("Enter the second number: "))
  print(num1 * num2)

def division():
  num1 = int(input("Enter the first number: "))
  num2 = int(input("Enter the second number: "))
  print(num1 / num2)

# Main program
main_menu() # Calls the main_menu procedure

What is a global variable?

  • A global variable is a variable declared at the outermost level of a program. This means that they are declared outside any modules such as functions or procedures

  • Global variables have a global scope, which means they can be accessed and modified from any part of the program

Python example

  • In this python code, you can see that the globalVariable (with the value 10) is declared outside of the function printValue

  • This means that this function and any other modules can access and change the value in the global variable

Global variables

globalVariable = 10 # Defines a global variable

def printValue():
    global globalVariable
    print("The value into the variable is:", globalVariable)

printValue() # Call the function

What is a local variable?

  • A local variable is a variable declared within a specific scope, such as a function or a code block

  • Local variables are accessible only within the block in which they are defined, and their lifetime is limited to that particular block

  • Once the execution of the block ends, the local variable is destroyed, and its memory is released

Python example

  • In this python code, you can see that the localVariable (with the value 10) is declared inside of the function printValue

  • This means that only this function can access and change the value in the local variable

  • It cannot be accessed by other modules in the program

Local variables

def printValue():
    localVariable = 10  # Defines a local variable inside the function
    print("The value of the local variable is:", localVariable)

printValue()  # Call the function

Worked Example

An economy-class airline ticket costs £199. A first-class airline ticket costs £595.

(A) Create a function, flightCost(), that takes the number of passengers and the type of ticket as parameters, calculates and returns the price to pay.

You do not have to validate these parameters

You must use :

  • a high-level programming language that you have studied [4]

(B) Write program code, that uses flightCost(), to output the price of 3 passengers flying economy.

You must use :

  • a high-level programming language that you have studied [3]

How do I answer this question?

(A)

  • Define the function, what parameters are needed? where do they go?

  • How do you calculate the price?

  • Return the result

(B)

  • How do you call a function?

  • What parameters does the function need to return the result?

Answers

Part

Python

A

def flightCost(passengers, type):

if type == "economy":

cost = 199 * passengers

elif type == "first":

cost = 595 * passengers

return cost

B

print(flightCost("economy", 3)

OR

x = flightCost("economy", 3)

print(x)

Last updated:

You've read 0 of your 10 free revision notes

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.

Lucy Kirkham

Author: Lucy Kirkham

Expertise: Head of STEM

Lucy has been a passionate Maths teacher for over 12 years, teaching maths across the UK and abroad helping to engage, interest and develop confidence in the subject at all levels.Working as a Head of Department and then Director of Maths, Lucy has advised schools and academy trusts in both Scotland and the East Midlands, where her role was to support and coach teachers to improve Maths teaching for all.