PowerPoint Presentation Algorithms Programming with Python Python Level 2 – Lesson 3 4 Nguyễn Chí Thức gt “Function” in programming 2 • A.PowerPoint Presentation Algorithms Programming with Python Python Level 2 – Lesson 3 4 Nguyễn Chí Thức gt “Function” in programming 2 • A.
Algorithms & Programming with Python Python Level – Lesson 3-4 Nguyễn Chí Thức gthuc.nguyen@gmail.com 0986636879 “Function” in programming • A function is a block of organized, reusable code that is used to perform a single, related action • Functions allow us to order our code, make it more readable, reuse it and save some time Python built-in functions • Python gives you many built-in functions that we have learnt: • • • • print() abs() round() len() Python built-in functions • Functions can have some arguments, and can return some value (or nothing) • print(“hello”, 12) • • • abs(-123) • • • • argument: a string and an integer Return nothing argurment – number Return a number – its absolute value round() len() User-defined functions • You can create your own function These functions are called “user-defined function” Define a function • Define a “max” function that take numbers and return bigger one def max(a, b): if a > b: return a else: return b Calling a function def max(a, b): if a > b: return a else: return b print(max(1, 2)) Define a function • Using the “max” function above, write a “max3” function that take numbers and return the biggest one def max(a, b): if a > b: return a else: return b Define a function • Using the “max” function above, write a “max3” function that take numbers and return the biggest one def max3(a, b, c): Define a function • Using the “max” function above, write a “max3” function that take numbers and return the biggest one def max3(a, b, c): return max(a, max(b, c)) print(max3(1, 2, 4)) 10 Pass by reference vs value def changeme( mylist ): # formal param mylist = [1,2,3,4]; # local print "Inside the function: ", mylist return mylist = [10,20,30]; # global changeme( mylist ); # real param print "Outside the function: ", mylist [1, 2, 3, 4] [10, 20, 30] 31 Function Arguments You can call a function by using the following types of formal arguments : • • • • Required arguments Keyword arguments Default arguments Variable-length arguments 32 Required Arguments • Passed to a function in correct positional order • The number of arguments in the function call should match exactly with the function definition def round(a, n): round(1.2) 33 Required Arguments • • Passed to a function in correct positional order The number of arguments in the function call should match exactly with the function definition def round(a, n): round(1.2) TypeError: round() takes exactly arguments (1 given) 34 Required Arguments • Passed to a function in correct positional order • The number of arguments in the function call should match exactly with the function definition def round(a, n): round(1.2, 0) 35 Default Arguments • An argument that assumes a default value if a value is not provided in the function call def round(a, n = 0): round(1.2) 36 Default Arguments • An argument that assumes a default value if a value is not provided in the function call def round(a, n = 0): round(1.2) 37 Keyword Arguments def printinfo( name, age ): print("Name: ", name) print("Age ", age) return; printinfo( 50, "miki" ) Name: Age: 50 miki 38 Keyword Arguments def printinfo( name, age ): print("Name: ", name) print("Age ", age) return; printinfo("miki", 50 ) Name: Age: miki 50 39 Keyword Arguments def printinfo( name, age ): print "Name: ", name print "Age ", age return; printinfo( age=50, name="miki" ) Name: Age: miki 50 40 Variable-length arguments print("hello") print("hello", 1) print("hello", 1, 2, 3) print() How many arguments can print function take? 41 Variable-length arguments print("hello") print("hello", 1) print("hello", 1, 2, 3) print() print function any number of arguments!!! Variable-length arguments 42 Variable-length arguments def printinfo( arg1, *vartuple ): print "Output is: " print arg1 for var in vartuple: print var return; printinfo( 10 ) printinfo( 70, 60, 50 ) 43 Anonymous Functions (lamda functions) Later! 44 Recursion Later! 45 ... mylist = [10 ,20 ,30]; changeme( mylist ); print "Outside the function: ", mylist Inside the function: [10, 20 , 30, [1, 2, 3, 4]] Outside the function: [10, 20 , 30, [1, 2, 3, 4]] 29 Pass by reference... = f() print(a) 22 Local and global variables def f(): global a a = print(a) a = f() print(a) 1 1 23 Local and global variables def factorial(n): global f res = for i in range (2, n + 1): res... [1 ,2, 3,4]; # local print "Inside the function: ", mylist return mylist = [10 ,20 ,30]; # global changeme( mylist ); # real param print "Outside the function: ", mylist [1, 2, 3, 4] [10, 20 ,