CS 105: FUNCTIONS Max Fowler (Computer Science) - - PowerPoint PPT Presentation
CS 105: FUNCTIONS Max Fowler (Computer Science) - - PowerPoint PPT Presentation
CS 105: FUNCTIONS Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/ June 21, 2020 Video Series Four Topics Functions in Python Functions, Codeblocks Functions in Python Parameters, Arguments
Video Series Four Topics
Functions in Python – Functions, Codeblocks Functions in Python – Parameters, Arguments Functions in Python – Return Values Functions in Excel
Functions, Codeblocks
Function motivation
Consider making a pizza Consider the most atomic form – where do we start
a pizza?
By making the sauce
Sauce is used in multiple places TOMATO SAUCE Pasta Pizza
https://www.delish.com/cooking/recipe- ideas/a19660462/easy-crockpot-spaghetti- recipe/
User-defined functions
Sequence of operations for use (and REUSABLE)
elsewhere
Function definition says what the function does:
def <name>(): <body>
Function calls/invocations RUN the functions
<name>()
Example
def get_input_and_print(): name = input("Your name?\n") print("Hello " + name + "!")
get_input_and_print()
A definition A call
Code Blocks
Need a way to tell Python "this statements are related" Python uses indentation
Indentation
In other prog. languages, indentation is just good style In Python, it is syntactic and semantic These three programs are all different
Text is same, white space and behavior is different
def test(): print('first') print('second') test() def test(): print('first') print('second') test() def test(): print('first') print('second') test()
Functions vs. Methods
Methods are functions that are part of an object/type They use dot notation For example:
my_list = [0, 1, 2, 3] my_list.append(22)
Functions, in contrast:
len(my_list)
Video Question
When writing our own function, what do we call the
code that says what the function does?
Functions – Parameters and Arguments
What are Parameters?
def price_calc(cost, count): # function body here … price_calc(5.5, 10)
Passing parameters is like an assignment
Arguments are bound to their respective parameter
The variables disappear when the function ends
Parameter names are the same, but args can differ!
In PythonTutor, let's define price_calc and then try:
price_calc(5.5, 10) price_calc(8.25, 15) price_calc(2.34, 8)
Parameter order matters!
In price_calc, what happens if we do:
price_calc(10, 5.5)
What about:
def mult_str(name, num):
print(name * num)
mult_str("Max", 3) mult_str(3, "Max")
Video Question – What value is printed here?
def do_thing(var1): var1.append(4) var1 = [1, 2, 3] do_thing(var1) print(len(var1))
Functions – Return Values
Return Values
The keyword return Ends the function Replaces the function call with the returned value in the previous 'frame'
Function calls can be part of arbitrary expressions
x = sum_num(2,5) x = sum_num(2,5) + sum_num(5,5) x = sum_num(sum_num(2,2), sum_num(3,4))
All functions technically RETURN – if we do not have the keyword