CS 105: FUNCTIONS Max Fowler (Computer Science) - - PowerPoint PPT Presentation

cs 105
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS 105: FUNCTIONS

Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/

June 21, 2020

slide-2
SLIDE 2

Video Series Four Topics

Functions in Python – Functions, Codeblocks Functions in Python – Parameters, Arguments Functions in Python – Return Values Functions in Excel

slide-3
SLIDE 3

Functions, Codeblocks

slide-4
SLIDE 4

Function motivation

Consider making a pizza Consider the most atomic form – where do we start

a pizza?

By making the sauce

slide-5
SLIDE 5

Sauce is used in multiple places TOMATO SAUCE Pasta Pizza

slide-6
SLIDE 6

https://www.delish.com/cooking/recipe- ideas/a19660462/easy-crockpot-spaghetti- recipe/

slide-7
SLIDE 7

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>()

slide-8
SLIDE 8

Example

def get_input_and_print(): name = input("Your name?\n") print("Hello " + name + "!")

get_input_and_print()

A definition A call

slide-9
SLIDE 9

Code Blocks

 Need a way to tell Python "this statements are related"  Python uses indentation

slide-10
SLIDE 10

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()

slide-11
SLIDE 11

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)

slide-12
SLIDE 12

Video Question

When writing our own function, what do we call the

code that says what the function does?

slide-13
SLIDE 13

Functions – Parameters and Arguments

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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)

slide-16
SLIDE 16

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")

slide-17
SLIDE 17

Video Question – What value is printed here?

def do_thing(var1): var1.append(4) var1 = [1, 2, 3] do_thing(var1) print(len(var1))

slide-18
SLIDE 18

Functions – Return Values

slide-19
SLIDE 19

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

return, the function returns None at the very end

slide-20
SLIDE 20

Why does None matter?

Largely, something like this on accident:

x = print("hi there!" my_list = my_list.append(5)

In these cases, the variable will become None! None can also be a 'flag' – i.e, return None if a

function "fails"

slide-21
SLIDE 21

Video Question – Does a function NEED the keyword return to return a value?

slide-22
SLIDE 22

Functions in Excel

slide-23
SLIDE 23

Functions in Excel

Excel provides many useful "built-in" functions:

E.g., SUM(), AVERAGE(), MIN()

Take arguments: cells, cell ranges Produce return values Can be part of expressions & assignments

slide-24
SLIDE 24

No video question on this one 