Presentation is loading. Please wait.

Presentation is loading. Please wait.

Teaching London Computing

Similar presentations


Presentation on theme: "Teaching London Computing"— Presentation transcript:

1 Teaching London Computing
FUNCTIONS in Python Margaret Derrington KCL Easter 2014

2 Let’s start with some revision…
What will these instructions do? print (“Hello World”) print (2+3) input (“2+3 = “) int(input (“type a number “)) len(“Margaret”) hex(42) bin(15) If you type them into python shell, what will happen? Check with your neighbour that you agree

3 These are actually all FUNCTIONS
print (“Hello World”) print (2+3) input (“2+3 = “) int(input (“type a number “)) len(“Margaret”) hex(42) bin(15) print, input, len, input, hex, bin …. …………..Are all FUNCTIONS What do they have in common…? besides being PINK!

4 Functions We CALL a function It has a name, and brackets
PASS PARAMETER RETURN We CALL a function It has a name, and brackets and inside the brackets are PARAMETERS Parameters are PASSED to a function The function does something with those parameters, and returns an output or result

5 procedures, subroutines,
BUILT – IN FUNCTIONS Python has lots of build in functions We have already learnt and used several of them But today we are going to learn to CREATE OUR OWN NEW FUNCTIONS We will DEFINE functions by giving them a NAME, saying what the parameters will be And what the result will be when we CALL the function and pass it the necessary parameters Also called procedures, subroutines, methods Also called DECLARING

6 Which is now one more than it was
We can call a function and use the result afterwards or do one function inside another Defining a function Which is now one more than it was def AddOne (number): number = number+1 return number answer= AddOne(7) print (answer) print (AddOne(4)) def repeat (text): return text*2 print (repeat(“hello”)) print (repeat(“2”)) print (repeat (2)) What happens? What happens?

7 Defining a function def NameOfFunction (parameters) :
Don’t forget the colon def NameOfFunction (parameters) : explain what happens and how you work out the return RESULT <~~~stop indenting at end of definition INDENT Parameters are like variables; names that can have lots of different values The syntax is important and must be correct

8 More than one parameter
A function may have more than one parameter…. def AreaOfRectangle (length, width) return length*width This function with multiply the length by the width to give the area of a rectangle It has TWO parameters.

9 Exercise Create some simple functions to add three to a number
To triple a number To get a user input name and print it in capital letters To get a username and print a string “Hello username” Remember if the function does not include printing the result, you will have to do that in a separate function…. print. Define a new function ShoutName which uses these functions to make a function which gets a user’s name and shouts (in capital letters) “HELLO USERNAME!!!” Functions aren’t actually programs, but a program which defines and uses functions is much easier to follow.

10 SCOPE (of a variable) GLOBAL and LOCAL are used to describe variables which exist throughout a program (global) or just within a small part of it (local)… like inside a function These are important concepts, because they can cause a lot of confusion if they get mixed up def doubler (number): answer = number*2 return answer Here answer is a local variable, it only exists and has a meaning inside the function It will cause confusion if answer is used elsewhere in the program with a different meaning.

11 GCSE Syllabus USE of functions is required Creating functions is on AS
But creating them helps you undertsand how to use them And it also helps to break problems into separate parts that can get reused. And to use functions to fix those separate parts


Download ppt "Teaching London Computing"

Similar presentations


Ads by Google