functions. a function is a block of code… …that performs a specific task
TRANSCRIPT
![Page 1: Functions. A function is a block of code… …that performs a specific task](https://reader035.vdocuments.us/reader035/viewer/2022072010/56649dce5503460f94ac27b6/html5/thumbnails/1.jpg)
Functions
![Page 2: Functions. A function is a block of code… …that performs a specific task](https://reader035.vdocuments.us/reader035/viewer/2022072010/56649dce5503460f94ac27b6/html5/thumbnails/2.jpg)
A function is a
block of code…
…that performs a specific task
![Page 3: Functions. A function is a block of code… …that performs a specific task](https://reader035.vdocuments.us/reader035/viewer/2022072010/56649dce5503460f94ac27b6/html5/thumbnails/3.jpg)
Python already has lots of built in functions…
print() range () type() sleep()
We can declare our own functions to do what we want! These are called user-defined functions.
![Page 4: Functions. A function is a block of code… …that performs a specific task](https://reader035.vdocuments.us/reader035/viewer/2022072010/56649dce5503460f94ac27b6/html5/thumbnails/4.jpg)
Let’s create a new user-defined function that will output a greeting.
![Page 5: Functions. A function is a block of code… …that performs a specific task](https://reader035.vdocuments.us/reader035/viewer/2022072010/56649dce5503460f94ac27b6/html5/thumbnails/5.jpg)
Understand the different parts…
def Greeting(name): print('Hello ' + name)
Greeting(‘Sue’)
DefineFunction Name
Parameter List i.e. the inputs to the function!
Function CallHere is the ‘argument(s)’ passed to the function
![Page 6: Functions. A function is a block of code… …that performs a specific task](https://reader035.vdocuments.us/reader035/viewer/2022072010/56649dce5503460f94ac27b6/html5/thumbnails/6.jpg)
Why won’t this compile?
def Message(name): print(‘How are you today' + name)
Message(‘Danny’)
![Page 7: Functions. A function is a block of code… …that performs a specific task](https://reader035.vdocuments.us/reader035/viewer/2022072010/56649dce5503460f94ac27b6/html5/thumbnails/7.jpg)
Why won’t this compile?
Def Message(name): print(‘How are you today' + name)
Message(‘Danny’)
def
![Page 8: Functions. A function is a block of code… …that performs a specific task](https://reader035.vdocuments.us/reader035/viewer/2022072010/56649dce5503460f94ac27b6/html5/thumbnails/8.jpg)
Understand the different parts…
def sum(num1,num2): return num1+num2
sum(12,10)
>> 22
By default, all functions should return a value.
In this case, we want our sum function to return the sum of any two numbers.
parameters
call
arguments
Function name
Function output
![Page 9: Functions. A function is a block of code… …that performs a specific task](https://reader035.vdocuments.us/reader035/viewer/2022072010/56649dce5503460f94ac27b6/html5/thumbnails/9.jpg)
Another example of a function…
Note, use print here…to output the result of the function on screen…eventhough the function has already worked it out!
![Page 10: Functions. A function is a block of code… …that performs a specific task](https://reader035.vdocuments.us/reader035/viewer/2022072010/56649dce5503460f94ac27b6/html5/thumbnails/10.jpg)
TASK Celsius to Fahrenheit•Prompt the user for a temperature in CELSIUS.
To convert a temp from Celsius to Fahrenheit, use the following formula:
°C x 9/5 + 32 = °F
(Remember BEDMAS from your mathematics lessons!)
•Create a function that, when called, will calculate the temperature in Fahrenheit and return it to the main program (from where the function call was made) e.g.
Temperature conversion complete - the reading is 42 Fah
Check whether your program is accurate by checking it against the following:
Enter 30 Cel Program should output 86 Fah
![Page 11: Functions. A function is a block of code… …that performs a specific task](https://reader035.vdocuments.us/reader035/viewer/2022072010/56649dce5503460f94ac27b6/html5/thumbnails/11.jpg)
TASK Fahrenheit to Celsius•Prompt the user for a temperature in Fahrenheit.
To convert a temp from Fahrenheit to Celsius use the following formula:
(°F - 32) x 5/9 = °C
(Remember BEDMAS from your mathematics lessons!)
•Create a function that, when called, will calculate the temperature in Celsius and return it to the main program (from where the function call was made) e.g.
Temperature conversion complete - the reading is 18 Cel
Check whether your program is accurate by checking it against the following:
Enter 86 Fah Program should output 30 Cel