0% found this document useful (0 votes)
13 views

L20 Slides - Programming - KS4

Uploaded by

21zarsh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

L20 Slides - Programming - KS4

Uploaded by

21zarsh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Lesson 20: Scope

KS4 - Programming
Starter activity

Function questions

1 def multiply(a, b): Question .


2 answer = a * b
3 return answer What will be the output of this program?
4
5
6 print(multiply(2, 4)) 1A. Multiply 2, 4
▹ 2B. 8
3C. 6
4D. Nothing, because the return value has not been

held in a variable
Starter activity

Function questions

1 def divide(a, b): Question .


2 answer = a / b
3 return answer What will be the output of this program?
4
5
6 divide(8, 2) 1A. Divide 8,2
7 print(answer)
2B. 8 divided by 2 is 4
3C. 4
▹ 4D. An error message will occur as the variable

answer only exists inside the function


Starter activity

Function questions

1 def add(a, b): Question .


2 answer = a + b
3 return answer What will be the output of this program?
4
5
6 solution = add(8, 4) ▹ 1A. 12
7 print(solution)
2B. add 8, 4
3C. solution
4D. An error message will occur because the value

returned has been held in solution instead of


answer
Objectives

Lesson 20: Scope


In this lesson, you will:
● Describe the scope of variables
● Describe how parameters can reduce the need for global variables
● Identify when to use global variables
● Describe a constant

6
Activity 1

Scope

The scope of a variable is the section of


the program where the variable can be
accessed and modified.
Activity 1

Scope

Variables can either have global scope or


local scope.

Global scope refers to a variable that can


be accessed and modified from anywhere
in the program, even from inside
subroutines.
Activity 1

Scope

Local scope refers to variables that can


only be accessed and modified within the
code block that they were declared.
Activity 1

Scope

Not all programming languages handle


scope in the same way.

Python has been designed to discourage


the modification of global variables,
you’ll find out why during this lesson.

Variables are local unless otherwise


declared.
Activity 1

Scope

A variable declared in the main part of


the program can be accessed globally by
all subroutines.

It cannot be modified unless you


explicitly state that the program should
modify the global variable.
Activity 1

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

Scope: step-by-step execution


State .
1 def example():
2 print(number) Global
3
4 number 5
5 number = 5
6 example()
7 print(number)

Input/output .
>>>
Activity 1

Scope: step-by-step execution


State .
1 def example():
2 print(number) Global
3
4 number 5
5 number = 5
6 example()
7 print(number)

Input/output .
>>>
Activity 1

Scope: step-by-step execution


State .
1 def example():
2 print(number) Global
3
4 number 5
5 number = 5
6 example()
7 print(number)

Input/output .
>>>
Activity 1

Scope: step-by-step execution


State .
1 def example():
2 print(number) Global
3
4 number 5
5 number = 5
6 example()
7 print(number)

Input/output .
>>>
5
Activity 1

Scope: step-by-step execution


State .
1 def example():
2 print(number) Global
3
4 number 5
5 number = 5
6 example()
7 print(number)

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

Scope: step-by-step execution


State .
1 def example():
2 number = 10 Global
3 print(number)
4 number 5
5
6 number = 5
7 example()
8 print(number)

Input/output .
>>>
Activity 1

Scope: step-by-step execution


State .
1 def example():
2 number = 10 Global
3 print(number)
4 number 5
5
6 number = 5
7 example()
8 print(number)

Input/output .
>>>
Activity 1

Scope: step-by-step execution


State .
1 def example():
2 number = 10 Global
3 print(number)
4 number 5
5
6 number = 5
7 example()
8 print(number)

Input/output .
>>>
Activity 1

Scope: step-by-step execution


State .
1 def example():
2 number = 10 Global
3 print(number)
4 number 5
5
6 number = 5
7 example() Local
8 print(number)
number 10

Input/output .
>>>

A new variable is declared with local scope. A global variable


cannot be modified unless it is explicitly stated in the program.
Activity 1

Scope: step-by-step execution


State .
1 def example():
2 number = 10 Global
3 print(number)
4 number 5
5
6 number = 5
7 example() Local
8 print(number)
number 10

Input/output .
>>>
10
Activity 1

Scope: step-by-step execution


State .
1 def example():
2 number = 10 Global
3 print(number)
4 number 5
5
6 number = 5
7 example() Local
8 print(number)
number 10

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

Scope: step-by-step execution


State .
1 def example():
2 global number Global
3 number = 10
4 print(number) number 5
5
6
7 number = 5
8 example()
9 print(number)

Input/output .
>>>
Activity 1

Scope: step-by-step execution


State .
1 def example():
2 global number Global
3 number = 10
4 print(number) number 5
5
6
7 number = 5
8 example()
9 print(number)

Input/output .
>>>
Activity 1

Scope: step-by-step execution


State .
1 def example():
2 global number Global
3 number = 10
4 print(number) number 5
5
6
7 number = 5
8 example()
9 print(number)

Input/output .
>>>
Activity 1

Scope: step-by-step execution


State .
1 def example():
2 global number Global
3 number = 10
4 print(number) number 5
5
6
7 number = 5
8 example()
9 print(number)

Input/output .
>>>

The program is explicitly stating that it should modify the


global variable number.
Activity 1

Scope: step-by-step execution


State .
1 def example():
2 global number Global
3 number = 10
4 print(number) number 10
5
6
7 number = 5
8 example()
9 print(number)

Input/output .
>>>
Activity 1

Scope: step-by-step execution


State .
1 def example():
2 global number Global
3 number = 10
4 print(number) number 10
5
6
7 number = 5
8 example()
9 print(number)

Input/output .
>>>
10
Activity 1

Scope: step-by-step execution


State .
1 def example():
2 global number Global
3 number = 10
4 print(number) number 10
5
6
7 number = 5
8 example()
9 print(number)

Input/output .
>>>
10
10
Activity 1

Scope

Global variables are generally seen as


bad practice by programmers.

This is because it can be difficult to track


the value of the global variable if it is
modified in other areas of the program.

It is also harder to test a function in


isolation if it accesses global variables.
Activity 1

Scope

If you are tempted to use a global


variable, it is a good idea to try and think
carefully whether parameter passing
would be a better approach.
Activity 2

Not ‘going global’

Use the worksheet to modify the


programs.

Convert the programs that use global


variables into functions that pass values
via parameters.
Activity 3

Constants

Another method for holding a value in


your programs is called a constant.

A constant is a fixed value that doesn’t


change through the execution of the
program.
Activity 3

Constants

Constants can be helpful in programming


when you need to ensure that a value
stays the same throughout execution.

One example of this might be the value of


pi.
Activity 3

Constants

Not all programming languages deal with


constants in the same way.

In C for example, you need to declare a


constant:

const float pi=3.14;

If the program is instructed to change the


value of a declared constant, then it will
produce an error.
Activity 3

Constants

In Python it is different. PI = 3.14

Python does not have a specific feature


for declaring constants.

If you wish to use a constant, then you


follow a naming convention.

All constants are declared using capital


letters.
Activity 3

Constants

The Python language will still treat this as PI = 3.14


a variable and it will not produce an error
message if the value is changed during PI = PI + 1
execution.

The naming convention is used to let


programmers know that this variable is to
be used as a constant.
Activity 3

Constants: an example

A game designer wants to ensure that all INITIAL_SPEED = 5

new levels begin with the background def level_1():


moving at the same speed. speed = INITIAL_SPEED
print("Go faster? Y/N")
This start speed should be the same for all answer = input()
new levels. if answer == "Y":
speed = speed + 5

level_1()
Activity 3

Constants: an example

At the start of each new level, the speed is INITIAL_SPEED = 5

set by assigning the constant SPEED to def level_1():


the variable speed. speed = INITIAL_SPEED
print("Go faster? Y/N")
answer = input()
if answer == "Y":
speed = speed + 5

level_1()
Activity 3

Constants: an example

As each level gets harder, the speed will INITIAL_SPEED = 5

increase at different rates. def level_1():


speed = INITIAL_SPEED
The speed at the beginning of each level print("Go faster? Y/N")
will always be the same. answer = input()
if answer == "Y":
speed = speed + 5

level_1()
Activity 3

Constants: an example

The programmer could just assign 5 to def level_1():


speed = 5
speed at the beginning of each level. print("Go faster? Y/N")
answer = input()
This would remove the need for the if answer == "Y":
constant. speed = speed + 5

level_1()
Activity 3

Constants: an example

Question . def level_1():


speed = 5
What would happen if the programmer print("Go faster? Y/N")
answer = input()
decided that the start speed should be if answer == "Y":
10 instead? speed = speed + 5

They would need to go to every part of level_1()


the program that referenced the start speed
and change each individual value to 10.
Activity 3

Constants: an example

By having a constant, the programmer INITIAL_SPEED = 5

can simply change the value once and it def level_1():


will change the value everywhere. speed = INITIAL_SPEED
print("Go faster? Y/N")
answer = input()
if answer == "Y":
speed = speed + 5

level_1()
Plenary

Lesson quiz

Complete the lesson quiz in silence.


Summary

Next lesson

In this lesson, you… Next lesson, you will…

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

Learnt about constants

48

You might also like