cs 105
play

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


  1. CS 105: FUNCTIONS Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/ June 21, 2020

  2. Video Series Four Topics  Functions in Python – Functions, Codeblocks  Functions in Python – Parameters, Arguments  Functions in Python – Return Values  Functions in Excel

  3. Functions, Codeblocks

  4. Function motivation  Consider making a pizza  Consider the most atomic form – where do we start a pizza?  By making the sauce

  5. Sauce is used in multiple places Pizza Pasta TOMATO SAUCE

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

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

  8. Example A definition def get_input_and_print(): name = input("Your name?\n") print("Hello " + name + "!") A call get_input_and_print()

  9. Code Blocks  Need a way to tell Python "this statements are related"  Python uses indentation

  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(): def test(): def test(): print('first') print('first') print('first') print('second') print('second') print('second') test() test() test()

  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)

  12. Video Question  When writing our own function, what do we call the code that says what the function does?

  13. Functions – Parameters and Arguments

  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

  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)

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

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

  18. Functions – Return Values

  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

  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"

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

  22. Functions in Excel

  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

  24. No video question on this one 

Recommend


More recommend