Intro to CS Nov 29, 2016
Today Functions Understanding scope Arguments Return Value Generator
Function Definition def function-name(): statements-to-be-executed Function-name can not be a keyword or a built-in function
Functions Program flow resumes at the point directly following the function call Functions provide modularity and can be called repeatedly
Scope Variables created outside functions can be referenced by statements inside functions. They have global scope Variables created inside functions can not be referenced from outside the function. They have local scope.
Local Variables Local variables of the same name can appear in different functions without conflict You can coerce a local variable to make it global with the “global” keyword followed by the variable name only Where a global variable and a local variable have the same name, the function uses the local variable.
Scope of Variables global_var = 1 def my_vars(): print('Global variable:', global_var) local_var = 2 print('Local variable:', local_var) global inner_var inner_var = 3 my_vars() print('Coerced global:', inner_var)
Arguments An “argument” name can be specified between the parentheses in the function definition def echo (user): print(‘User:’, user) A value can be specified in the parentheses when the function is called echo(‘Mike’) The function can reference the “passed in” value via the argument name
Multiple Arguments Multiple arguments can specified by a comma-separated list The call to the function must include the same number of values as arguments The passed values must appear in the same order as the arguments Unless the arguments are specified
Multiple Arguments def echo(user, lang, sys): print('User:', user, 'Language:', lang, 'Platform:', sys) echo('Mike', 'Python', 'Windows') echo(lang='Python', sys='Mac OS', user='Anne')
Default Arguments Default value can be specified in the argument Used by the function when no value gets passed by the caller Default value gets overridden when the caller specifies a value for that argument
Default Arguments def mirror(user = 'Carole', lang = 'Python'): print('\nUser:', user, 'Language:', lang) mirror() mirror(lang = 'Java') mirror(user = 'Tony') mirror('Susan', 'C++')
Return Value A function can return a value to the caller using the “return” keyword The returned result may be assigned to a variable by the caller The returned result can be used directly in-line such as in a print statement
Return Value num = input('Enter an integer:') def square(num): if not num.isdigit(): return 'Invalid Entry' num = int(num) return num * num print(num, 'Squared is:', square(num))
Generator A generator is a special function that returns a “generator object” rather than a data value This retains the state of the function when it was last called It will continue from the point when next called
Yield Generator function contains the ‘yield’ keyword Specifies the generator object to be returned to the caller When the ‘yield’ statement gets executed, the state of the generator object is frozen and the current value is retained
Next The generator object returned by the yield statement can be assigned to a variable The built-in next() function can take the generator object to continue execution of the function from the point it was frozen
Generator example def incrementer(): i = 1 while True: yield i i += 1 incr = incrementer() print(next(incr))
Summary Functions Understanding scope Arguments Return Value Generator