Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Functions in Python Functions What is a function? Definition Parameters Scope A function is something you can call (possibly with some parameters, i.e., the things in parentheses), which performs an action and returns a value. Return Values Recursion Functions in Python Functions in Python Functions Parameters Scope Return Values Recursion Example def h e l l o ( name , g r e e t i n g ) : r e t u r n g r e e t i n g + ” , ” + name p r i n t ( h e l l o ( ’ Markus ’ , ’ p r i v e t ’ ) ) L435/L555 Dept. of Linguistics, Indiana University Fall 2014 Define first, then call! In python, a function must be defined before you can call it. e.g., define it on line 10, call it on line 15. (http: //www.greenteapress.com/thinkpython/html/thinkpython004.html) 1 / 18 Function Calling Functions in Python 2 / 18 Why Use Functions? Functions Functions Parameters We’ve seen functions many times before: I Built-in functions: int(), type(), ... I Module-based functions: math.log(), random.choice(), ... Parameters Scope Scope Return Values Return Values Recursion Recursion Functions are extremely useful because: I They make code reusable I They make a program more structured & more readable, especially when it gets longer I They make it is easier to work with several programmers We have also seen function composition I int(input("Enter a number:")) I The output of the inner function (e.g., input()) is passed as the input to the outer function (e.g., int()) Functions in Python 3 / 18 Functions Calling Functions Functions in Python 4 / 18 Parameters (Arguments) Functions Functions Parameters def p r i n t l y r i c s ( ) : p r i n t ( ” Cause t h e p l a y e r s gonna p l a y . . . ” ) p r i n t ( ” And t h e h a t e r s gonna hate . . . ” ) Functions in Python Parameters Scope Scope Return Values Return Values Recursion Recursion Definition Parameters (also known as arguments) are inputs to functions. def r e p e a t l y r i c s ( ) : print lyrics () print lyrics () Example When you use the min() function, you pass the function a list as a parameter I repeat lyrics () 5 / 18 e.g., min([8,6,7]) returns 6 6 / 18 Local Scope Functions in Python Functions Local scope Functions in Python Functions Parameters Variables and parameters in functions have local scope. Parameters Scope Scope Return Values Return Values Recursion Mutable types def change name ( name ) : name = ’ The Thamesmen ’ name = ’ The New O r i g i n a l s ’ change name ( name ) p r i n t ( name ) # the value of t h i s Local Scope (2) Recursion Mutable data structures change in functions. def change ( l i s ) : l i s . extend ( [ ’ shake ’ , ’ i t ’ , ’ o f f ’ ] ) ’ name ’ i s unchanged def again ( ) : mypi = 3.11 p r i n t ( mypi ) # t h i s g i v e s an e r r o r : m y l i s t = [ ” I ’m” , ’ j u s t ’ , ’ gonna ’ , ’ shake ’ ] change ( m y l i s t ) print ( mylist ) ’ mypi ’ i s u n d e f i n ed 7 / 18 3 Types of Parameters Functions in Python 8 / 18 Parameter Types Functions Functions Parameters positional Positional parameters must be entered in the correct order hello (name, greeting) Functions in Python Definition Scope Return Values Any kind of variable can be passed to a function (string, integer, float, list, dict, tuple, object). Your function must use these as the right type though. Recursion keyword Keyword parameters can be entered in any order hello (greeting=’Ni Hao’, name=’Taylor’) Parameters Scope Return Values Recursion Example collected Parameters can also be collected by a function, allowing the user to input any number of parameters to the function def s o r t P e o p l e ( people ) : r e t u r n sorted ( people ) def h e l l o 2 ( * c ol l e ct e d P a r a m s ) : print ( ” Intermediate value : ” , collectedParams ) r e t u r n ’ ’ . j o i n ( [ s t r ( x ) f o r x i n c o l l e c t e d P a r am s ] ) p r i n t ( h e l l o 2 ( ’ shake ’ , ’ x ’ , 4 ) ) s p i n a l T a r p = ’ N i g e l , David , and Derek ’ print ( sortPeople ( spinalTarp ) ) s p i n a l T a p = [ ’ N i g e l ’ , ’ David ’ , ’ Derek ’ ] print ( sortPeople ( spinalTap ) ) 10 / 18 9 / 18 Parameter Types (2) Functions in Python Default Values Functions Functions Parameters Comment your code! You must know/remember which types work for a function, so it makes sense to add comments that specify the types of the parameters and of the return value. Functions in Python Parameters Scope Scope Return Values Return Values Recursion Recursion Parameters can be assigned a default value, used only if no value is passed in def myadd (D, key , v a l u e = 1 ) : i f key i n D: D[ key ] += v a l u e else : D[ key ] = v a l u e Example # f u n c t i o n sortPeople s o r t s the i n p u t & r e t u r n s i t # i n p u t : people − l i s t o r s t r i n g # output : l i s t # ( l i s t of characters i f input= s t r i n g ) def s o r t P e o p l e ( people ) : r e t u r n sorted ( people ) 11 / 18 12 / 18 Return Values Functions in Python Tip on Printing Functions Functions Parameters Definition Parameters are inputs to functions. Return values are outputs. Functions in Python Parameters Scope Scope Avoid the following Return Values Recursion Printing out stuff in functions (unless debugging) Return Values Recursion def h e l l o ( ) : print ( ” hello , world ” ) Multiple return values To return more than one value, put them in a tuple Instead, do the following def rhymes ( ) : x= ’ break ’ y= ’ f a k e ’ return ( x , y ) f o o =rhymes ( ) one , two=rhymes ( ) Returning stuff in functions and printing later def h e l l o ( ) : return ( ” hello , world ” ) print ( hello ( ) ) 13 / 18 Recursion Functions in Python 14 / 18 Fibonacci numbers Functions in Python Iterative version Functions Functions Parameters Parameters def f i b i t e r ( n ) : f minus2 = 0 f minus1 = 1 Scope A function call (other) functions I Return Values Recursion Recursion is when a function calls itself def countdown ( n ) : i f n <= 0 : print ( ’ Blastoff ! ’ ) else : print (n) countdown ( n − 1) Scope Return Values Recursion i f n == 1 : f i = 0 e l i f n == 2 : f i = 1 else : f o r i i n range ( 2 , n ) : f i = f minus2 + f minus1 countdown ( 5 ) f minus2 = f minus1 f minus1 = f i return f i 16 / 18 15 / 18 Fibonacci numbers Functions in Python Recursion notes Functions in Python Iterative version Functions Functions Parameters Parameters Scope Scope Return Values Return Values Recursion Recursion A few points regarding recursion: def f i b r e c u r ( n ) : i f n == 1 : return 0 e l i f n == 2 : return 1 else : r e t u r n f i b r e c u r ( n − 1) + f i b r e c u r ( n − 2) 1. Whatever parameters are passed must move towards some completion, e.g., integers get smaller (n-1) 2. Recursive functions have two parts: 2.1 Base case(s): what to do when you reach the “bottom” (e.g., if n == 1) 2.2 Recursive case: what to do in moving from one value to another 17 / 18 18 / 18