L20 Slides - Programming - KS4
L20 Slides - Programming - KS4
KS4 - Programming
Starter activity
Function questions
held in a variable
Starter activity
Function questions
Function questions
6
Activity 1
Scope
Scope
Scope
Scope
Scope
Scope
1 def example():
2 print(number) Here is a subroutine that is accessing a
3 global variable and displaying it as
4
5 number = 5 # global variable output.
6 example()
7 print(number)
Activity 1
Input/output .
>>>
Activity 1
Input/output .
>>>
Activity 1
Input/output .
>>>
Activity 1
Input/output .
>>>
5
Activity 1
Input/output .
>>>
5
5
Activity 1
Scope
1 def example():
2 number = 10 The intention of this subroutine is to
3 print(number) modify the global variable number.
4
5
6 number = 5 However, it doesn’t execute as expected.
7 example()
8 print(number)
Activity 1
Input/output .
>>>
Activity 1
Input/output .
>>>
Activity 1
Input/output .
>>>
Activity 1
Input/output .
>>>
Input/output .
>>>
10
Activity 1
Input/output .
>>>
10
The value of the global variable number has not changed. 5
Activity 1
Scope
1 def example():
2 global number Here is a subroutine that will access and
3 number = 10 modify the global variable.
4 print(number)
5
6 By adding ‘global’ before the variable
7 number = 5 name, it states that this part of the
8 example()
9 print(number) program should access and modify the
global variable.
Activity 1
Input/output .
>>>
Activity 1
Input/output .
>>>
Activity 1
Input/output .
>>>
Activity 1
Input/output .
>>>
Input/output .
>>>
Activity 1
Input/output .
>>>
10
Activity 1
Input/output .
>>>
10
10
Activity 1
Scope
Scope
Constants
Constants
Constants
Constants
Constants
Constants: an example
level_1()
Activity 3
Constants: an example
level_1()
Activity 3
Constants: an example
level_1()
Activity 3
Constants: an example
level_1()
Activity 3
Constants: an example
Constants: an example
level_1()
Plenary
Lesson quiz
Next lesson
Learnt about global scope and local scope Design and create a program that uses
function calls
Learnt how parameter passing can reduce
the need for global variables
48