Download - Programming for GCSE Topic 8.1: Functions
![Page 1: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/1.jpg)
Programming for GCSE
Topic 8.1: Functions
Teaching London Computing
William MarshSchool of Electronic Engineering and Computer Science
Queen Mary University of London
![Page 2: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/2.jpg)
Outline
• Functions• What's the big idea?• Syntax
• Variables in functions: scope• Name clashes
• Functions that make changes• Global variables
![Page 3: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/3.jpg)
What's a Function?
• A part of a program with• A name• Parameters• Result
this function does a
calculation
ResultInputs
![Page 4: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/4.jpg)
BIG IDEA
• This IS A BIG IDEA
• Building blocks of a program• Big programs cannot be made in one piece• Use 'blocks' from another programmer
(library)
• Naming parts of a program• Name the function behaviour
![Page 5: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/5.jpg)
SIMPLE FUNCTION EXAMPLE
Defining and calling a function
![Page 6: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/6.jpg)
Definition of a Function
• A function is a NOT a complete program
def double(num): result = num * 2 return result
Key word
Name
Key word
Parameter
![Page 7: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/7.jpg)
Calling a Function – I
• Call the function
def double(num): result = num * 2 return result
anum = int(input("A number:"))anum = double(anum)anum = double(anum)print("Now doubled twice:", anum)
Function call
![Page 8: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/8.jpg)
Calling a Function – II
• Call the function
def double(num): result = num * 2 return result
anum = int(input("A number:"))anum = double(double(anum))print("Now doubled twice:", anum)
Function call
![Page 9: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/9.jpg)
Program Order
• Write the functions first• One function can call
another (providing it is defined first)
• Do not put one function inside another
• The 'main' program calls the functions
Function def
Function def
Function def
Main program• Initialise
variable• Call functions
![Page 10: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/10.jpg)
Words, Words …• You define (or declare) a function• A function has parameters• You call a function• You pass a value to a function• … it returns a result• The function creates a new scope• Functions are also called• Procedures• Subroutines• Methods• … and more
![Page 11: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/11.jpg)
Example
• Create a function that is passed a name and prints the string "Hello XXXX"• Choose a suitable name
• Change the function to capitalise the name• Choose a new name
![Page 12: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/12.jpg)
Example Solution
def greetMe(name): print("Hello", name)
def greetMeLoudly(name): print("Hello", name.upper())
![Page 13: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/13.jpg)
VARIABLES IN FUNCTIONS
The idea of 'scope'
![Page 14: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/14.jpg)
Variable Scope
• Function create a 'box'
• Variable 'result' is a 'local' variable• It only exists inside the box
• 'num' can be used like a variable• It is given a value in the call
def double(num): result = num * 2 return result
![Page 15: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/15.jpg)
Scope: Simple Version
• The variables used inside a function are totally separate from other variables• Appear when function is called• Disappear afterwards
• Name clash: confusing variables inside and outside a function• Use different names
![Page 16: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/16.jpg)
FUNCTIONS THAT MAKE CHANGES
Some more complex and less essential ideas
![Page 17: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/17.jpg)
What is the Effect of a Function?
No effect
• Return a value• Nothing changes!
Effect
• Print something• File output too
• Change value of a variable outside the function• How is this possible?
this function does a
calculation
ResultInputs
Global variable
s
![Page 18: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/18.jpg)
Global Variables
• Local: inside a function• Global: outside a function• Variable inside (local) and outside (global)
not totally separate
def double() : global num num = num * 2
num = 10double()double()print(num)
Key word
![Page 19: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/19.jpg)
Using a List as a Parameter
• When a list is used as a parameter, you can change it
def addZero(mylist): mylist.append(0)
herList = [1,2,3]addZero(herList)print(herList)
>>> [1, 2, 3, 0]>>>
![Page 20: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/20.jpg)
Parameters and Assignment
• There is a close parallel between
• Parameter passing is like assignment
def myFunction(param) : ... statements
num = 10myFunction(num) num = 10
param = num ... statements
![Page 21: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/21.jpg)
SYLLABUS AND TEACHING ISSUES
![Page 22: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/22.jpg)
Syllabus – Functions
• Writing functions is AS not GCSE (OCR)• … but lots of related ideas
• So why learn functions?• Using functions e.g. 'len'• Planning solutions: breaking down a
problem in parts• … some students will teach themselves
![Page 23: Programming for GCSE Topic 8.1: Functions](https://reader035.vdocument.in/reader035/viewer/2022062309/56815783550346895dc51a5b/html5/thumbnails/23.jpg)
Summary
• Programming is problem solving• Problems are solved in steps• Functions are for step-by-step
programming
• Defining functions is not essential for GCSE• Using them is!