3_2_10 Subroutines (procedures and functions)

You should be able to:

  • Understand the concept of subroutines.
  • Explain the advantages of using subroutines in programs.
  • Describe the use of parameters to pass data within programs.
  • Use subroutines that return values to the calling routine.
  • Know that subroutines may declare their own variables, called local variables, and that local variables usually:

•• only exist while the subroutine is executing

•• are only accessible within the subroutine.

  • Use local variables and explain why it is good practice to do so.

REVISE:

What is a subroutine?

Revisit 8_1_7 and 8_1_8.

Advantages of Subroutines

The key advantages of subroutines are:

  • It makes it easier to spot errors and fix them
  • It makes it easier to re-use code
  • The subroutine created can be used with other applications
  • It allows you to decompose the problem in a structured format
  • A large programming problem can be divided between programmers

Parameters

Parameter - The variable name for a value that is passed to a subroutine

Argument - The actual value that is passed to a subroutine

def add(a,b):
    
    c = a + b
    return (c)

print (add(5,3))

  • a and b are the parameters
  • 5 and 3 are the arguments

Arguments are passed through the parameters to solve a problem. In this case, 5 and 3 are passed through the parameters and are added together.

Returning a Value (using functions)

We have returned a value in the example above. If I wanted to use that value then I could store it into a variable.

def add(a,b):
    
    c = a + b
    return (c)

c=(add(5,3))
print (c)

Local and Global Variables

When we look at scope of variables, there are two main types:

  • LOCAL - can only be viewed from within a sub-routine
  • GLOBAL - can be viewed by the entire application

The example function above contains a local variable called c. This variable can only be accessed from within the subroutine. If you deleted the line of code that reads c=(add(5,3)) then it would say that c doesn't exist.

PROGRAMMING CHALLENGE:

A menu of programs

Create a menu for all of the programs that you have developed so far. These could be:

  • Database created for 3_2_6
  • Password Storage program for 3_2_7
  • Password Encryption program for 3_2_8
  • Password Generator Machine for 3_2_9

The user should see a menu of the programs and be able to select which one they wish to run.

After running the program they should then return to the menu or quit.

TEST:

  1. Download and print the test paper here: https://drive.google.com/open?id=0B5fLtQ0Xgr2Pd3JmZVJhd1JNaUU
  2. Try the mock test yourself.
  3. Use the 3.2.10 Walking Talking Mock below to guide you through answering the questions.

SOURCE RECOGNITION - PLEASE NOTE: The examination examples used in these walking talking mocks are samples from AQA from their non-confidential section of the public site. They also contain questions designed by TeachIT for AQA as part of the publicly available lesson materials.