Creating Your Own Functions
Creating functions and modules
Please download the lecture files...
1
Outline
2
Outline
3
Using functions
What do you have to know to be able to use a function?
4
max()�???
1, 99, 123, 3
123
print()�???
‘Hello', ‘There’, end='!’
None
Outputs to screen
(side effects)
Using functions
5
Activity 1: Using only built-functions, write some programs...
Using the operator functions from the operator module, write a program to calculate the hypotenuse of a right triangle where side a=5 and side b=12.
On your own, try to complete the following:
Don’t look at the answers yet!
6
What if you needed to do it for 1,000 triangles?
7
Outline
8
Recall: The Big Ideas with Functions...
9
So how do I make my own?
10
Functions: header and body
def name_of_function(parameters):
statement 1
statement 2
...
return some_value
11
HEADER�Specifies the name of the function and the data that it needs. Also called the SIGNATURE
BODY�One or more indented statements that the function will execute when called
def name_of_function(parameters):
statement(s)
return some_value
12
Keyword def marks the start of function header
A function name to uniquely identify it (snake case)
parameters (the way we pass data to a function)
colon (:) marks end of function header
function body has 1 or more statements, which have same indentation level (usually 4 spaces)
An optional return statement to return a value from the function
Functions must be invoked in order to execute
name_of_function(arguments)
13
arguments (specific data values that are passed into a function)
Let’s take a look at some examples...
14
Function with no parameters, no return value
# define the function
def say_hello():
print('Hello there!')
print('How are you this morning?')
# call(invoke) the function
say_hello()
say_hello()
15
lectures/lecture06/03_function_no_parameters_no_return.py�Visualize: https://goo.gl/eqxY3b
Function with a required parameter, no return value
# define the function
def say_hello(name: str):
print('Hello there,', name, end='!')
print('How are you this morning?')
# call(invoke) the function
say_hello('Varun')
say_hello('Grace')
16
arguments
Parameter of type string
lectures/lecture06/04_function_parameters_no_return.py | Visualize
Function with a no parameters, returns a value
# define the function
def get_todays_date():
from datetime import datetime
return datetime.now().date()
# call(invoke) the function
todays_date = get_todays_date()
print('Today\'s date is', todays_date)
17
return statement
assigning return value to variable
lectures/lecture06/05_function_no_parameters_return.py | Visualize
Function with parameters, returns a value
# define the function
def add_em(num_1: float, num_2: float):
return num_1 + num_2
# call(invoke) the function
result = add_em(33, 44)
print('The sum is:', result)
result = add_em(3, 55)
print('The sum is:', result)
18
return statement
assigning return value to variable
lectures/lecture06/06_function_parameters_return.py | Visualize
Parameters of type float
Outline
19
Activity 2: Make your first function...
20
lectures/lecture06/07_activity.py
Activity 2: Make your first custom function
Inside of the lectures/lecture06/07_activity.py file, create three custom functions:
When you’re done, invoke that function using the following triangle dimensions:
side_a = 5, side_b = 12
side_a = 3, side_b = 5
side_a = 4, side_b = 4
If you get done early, try lectures/lecture06/08_activity.py
21
Outline
22
What is the difference between a parameter and an argument?
23
Parameters & Arguments
parameter :: variable in function definition�A name, used inside a function, to refer to the data value (argument) passed into it. Parameters are variables.
argument :: data specified when function invoked�A value provided to a function when the function is called/invoked. This value is assigned to the corresponding parameter in the function. The argument can be the result of an expression which may involve operators, operands and calls to other functions.
24
2 Types of Parameters
Positional parameters
Keyword parameters
25
Function with 1 positional parameter
# defining the function
def say_hello(name: str):
print('Hello there,' + name + '!')
print('How are you this morning?')
# calling (invoking) the function
say_hello('Varun')
say_hello('Grace')
26
Arguments
Parameter (positional)
Function with 2 positional parameters
# defining the function
def say_hello(name: str, time_of_day: str):
print('Hello there,' + name + '!')
print('How are you this ' + time_of_day + '?')
# calling (invoking) the function
say_hello('Varun', 'morning')
say_hello('Grace', 'evening')
27
2 positional parameters
Arguments
Function with 1 keyword parameter
# defining the function
def say_hello(time_of_day: str='morning'):
print('How are you this ' + time_of_day + '?')
# calling (invoking) the function
say_hello()
say_hello(time_of_day='evening')
28
keyword parameter
No keyword argument (uses default)
Keyword argument overrides default
Function with positional and keyword parameters
# defining the function
def say_hello(name: str, time_of_day: str='morning'):
print('Hello there,' + name + '!')
print('How are you this ' + time_of_day + '?')
# calling (invoking) the function
say_hello('Varun')
say_hello('Grace', time_of_day='evening')
29
positional & keyword parameters
No keyword argument (uses default)
Keyword argument overrides default
Fruitful and non-fruitful functions
Fruitful
A function that returns a value
def get_area_of_rect(side1: float, side2: float):� return side1 * side2
�Non-fruitful
Does something but does not return a value
def say_hello(name):� print('Hello there,' + name + '!')
30
Fruitful functions (have return values)
# defining the function
def get_hypotenuse(side_a: float, side_b: float):
sum_of_squares = side_a** 2 + side_b ** 2
return sum_of_squares ** 0.5
# calling (invoking) the function
hypotenuse1 = get_hypotenuse(55, 25)
hypotenuse2 = get_hypotenuse(515, 467)
print(hypotenuse1)
31
Visualize: https://goo.gl/eqxY3b
return value
return value stored in variable
Quiz
What’s an example of a function we used in HW1 that does not return a value?
What’s an example of a function we used in HW1 that does return a value?
32
Outline
33
Things that your interpreter doesn’t care about (but that you should): Type Hints
def say_hello(name, time_of_day='morning'):
...
�def say_hello(name: str, time_of_day: str='morning'):
...�
34
Things that your interpreter doesn’t care about (but that you should): Docstrings
35
Things that your interpreter doesn’t care about (but that you should): Docstrings
def add_em(num_1: float, num_2: float):
'''
Adds two numbers together.
Args:
num_1(float): the first number in the addition operation.
num_2(float): the second number in the addition operation.
Returns:
float: the sum of the two numbers
'''
return num_1 + num_2
36