Download presentation
Presentation is loading. Please wait.
Published byMagdalen Short Modified over 8 years ago
1
Functions
2
What is a Function? We have already used a few functions. Can you give some examples? Some functions take a comma-separated list of arguments / parameters Some functions have return values But not all… Functions.pptx2 of 19
3
What is a Function? So, what is a function? A function groups a set of statements together so that they can be run more than once in a program. Why is this useful? Code reuse – we don’t have to copy and paste code that we use more than once. Imagine if you had to write the print function every time you needed it! Decomposition – designing our programs using functions allows us to break the problem down into smaller chunks Functions.pptx3 of 19
4
Writing your own Functions You can write your own functions which will behave in the same way as built-in functions We define a function with the keyword, def followed by the function name The body of the function is a code block so we need a colon and indentation Type this code in and run it. Notice that nothing happens…! You must call the function... Functions.pptx4 of 19
5
A First Function We could write a function to format and print something e.g. We can call this function like this: Functions.pptx5 of 19
6
A First Function What is causing the error here? Try this Functions.pptx6 of 19
7
The main( ) function We got an error because the function was used before it was defined… We could solve this by putting the function at the top of the program but that would make a long program difficult to read A better idea is to define a function that controls the program Conventionally, this function is called main() Functions.pptx7 of 19
8
The main() Function Try this… Functions.pptx8 of 19
9
The main() function What is the difference? Now, when you run the program, Python reads and remembers the function definitions It does not run them until you call them e.g. by typing main() Functions.pptx9 of 19
10
Function Parameters Just like built-in functions, our own functions can have parameters Functions.pptx10 of 19
11
Function Parameters We define the parameters by listing them in brackets after the function name: Here we are creating a function called printDetails() that takes two parameters, name and amount These parameters automatically become variables in the function You can think of them as named letter boxes Functions.pptx11 of 19
12
Function Parameters We call this function like this: The function creates name and amount variables and puts the value of: salesman in name amountSold in amount Functions.pptx12 of 19
13
Be careful… You cannot access salesman or amountSold in the printDetails() function. That is why you must pass them as parameters This is called variable scope This code block cannot directly access variables from the main code block Functions.pptx13 of 19
14
Return Values A function can also send a value back to where the function was called It uses the keyword return Functions.pptx14 of 19
15
Return Values We can get this value in exactly the same we do with a built-in function like input() The value returned by getCommission() function is put in the commission variable sales.py Functions.pptx15 of 19
16
Challenge 1: Area of a Circle In the main() function, ask the user to enter a value for the radius of a circle Write a function that returns the area of that circle Display the area The formula to calculate the area of a circle is π r 2 Assume π is 3.14 Functions.pptx16 of 19
17
Challenge 2: Dice Rolling Game Write a dice rolling game The program should ‘roll two dice’ The user wins when both dice show a six The program should show how many rolls it took to get the double six Functions.pptx17 of 19
18
Using Function Return Values v1 Functions.pptx18 of 19
19
Using Function Return Values v2 Here, we are using the rollDice() function to control our loop This is the boolean AND operator Functions.pptx19 of 19
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.