Back to 课程

Computer Science GCES AQA

0% Complete
0/0 Steps
  1. Representing Algorithms Aqa
    4 主题
  2. Efficiency Of Algorithms Aqa
    1 主题
  3. Searching Algorithms Aqa
    3 主题
  4. Sorting Algorithms Aqa
    3 主题
  5. Data Types Aqa
    1 主题
  6. Programming Concepts Aqa
    5 主题
  7. Arithmetic Relational And Boolean Operations Aqa
    1 主题
  8. Data Structures Aqa
    3 主题
  9. String Manipulation Aqa
    1 主题
  10. Random Number Generation Aqa
    1 主题
  11. Structured Programming Aqa
    2 主题
  12. Robust And Secure Programming Aqa
    4 主题
  13. Number Bases Aqa
    2 主题
  14. Converting Between Number Bases Aqa
    3 主题
  15. Units Of Information Aqa
    9 主题
  16. Hardware And Software Aqa
    4 主题
  17. Boolean Logic Aqa
    3 主题
  18. Programming Languages And Translators Aqa
    2 主题
  19. Cpu Architecture Performance And Embedded Systems Aqa
    4 主题
  20. Memory Aqa
    2 主题
  21. Secondary Storage Aqa
    3 主题
  22. Fundamentals Of Computer Networks Aqa
    8 主题
  23. Fundamentals Of Cyber Security Aqa
    1 主题
  24. Methods Of Preventing Cyber Security Threats Aqa
    1 主题
  25. Relational Databases Aqa
    2 主题
  26. Ethical Legal And Environmental Impacts Aqa
    2 主题
课 Progress
0% Complete

Exam code:8525

Using Subroutines

Why do we use subroutines?

  • Subroutines are used to be able to decompose a big problem into smaller parts to make it easier to solve

  • Subroutines are split into 2 types:

    • Functions

    • Procedures

  • Functions and procedures are types of sub-programs, which are a sequence of instructions that perform a specific task or set of tasks

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

  • The advantages of using subprograms are:

    • 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

Functions and Procedures

What is the difference between a function and procedure?

  • A Function returns a value whereas a procedure does not

Concept

Pseudocode

Python

Creating a function

SUBROUTINE squared(number)

squared ← number^2

RETURN squared

ENDSUBROUTINE

def squared(number):

squared = number^2

return squared

Calling a function

SquNum ← squared(4)

print(SquNum)

OR

OUTPUT(SquNum(4))

SquNum = squared(4)

print(SquNum)

OR

print(SquNum(4))

Creating a procedure

SUBROUTINE ageCheck(age)

IF age > 18 THEN

OUTPUT("You are old enough")

ELSE

OUTPUT("You are too young")

ENDIF

ENDSUBROUTINE

def ageCheck(age):

if age > 18:

print("You are old enough")

else:

print("You are too young")

Calling a procedure

ageCheck(21)

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

def area(length, width): # Function definition, length and width are parameters
area = length * width # Calculate area
return area # Return area

# Main program #1
length = int(input("Enter the length: ")) # Asks the user to enter the length
width = int(input("Enter the width: ")) # Asks the user to enter the width
print(area(length, width)) # Outputs the result of the function

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

  • 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

def main_menu(): # Function definition
print("1. Addition") # Outputs the option
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Exit")
choice = int(input("Enter your choice: ")) # Asks the user to enter their choice
if choice == 1: # If the user chooses 1
addition() # Calls the addition function
elif choice == 2:
subtraction()
elif choice == 3:
multiplication()
elif choice == 4:
division()
elif choice == 5:
exit()

def addition(): # Function definition
num1 = int(input("Enter the first number: ")) # Asks the user to enter the first number 
num2 = int(input("Enter the second number: ")) # Asks the user to enter the second number
print(num1 + num2) # Outputs the result of the addition

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 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 either:

  • Pseudocode, or

  • 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 either:

  • Pseudocode, or

  • 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

Pseudocode

Python

A

SUBROUTINE flightCost(passengers, type)

IF type == "economy" then

cost ← 199 * passengers

ELSE IF type == "first" then

cost ← 595 * passengers

ENDIF

RETURN cost

ENDSUBROUTINE

def flightCost(passengers, type):

if type == "economy":

cost = 199 * passengers

elif type == "first":

cost = 595 * passengers

return cost

B

OUTPUT(flightCost("economy", 3)

OR

x ← flightCost("economy", 3)

OUTPUT(x)

print(flightCost("economy", 3)

OR

x = flightCost("economy", 3)

print(x)

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注