Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to CS Nov 29, 2016.

Similar presentations


Presentation on theme: "Intro to CS Nov 29, 2016."— Presentation transcript:

1 Intro to CS Nov 29, 2016

2 Today Functions Understanding scope Arguments Return Value Generator

3 Function Definition def function-name(): statements-to-be-executed
Function-name can not be a keyword or a built-in function

4 Functions Program flow resumes at the point directly following the function call Functions provide modularity and can be called repeatedly

5 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.

6 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.

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

8 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

9 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

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

11 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

12 Default Arguments def mirror(user = 'Carole', lang = 'Python'):
print('\nUser:', user, 'Language:', lang) mirror() mirror(lang = 'Java') mirror(user = 'Tony') mirror('Susan', 'C++')

13 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

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

15 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

16 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

17 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

18 Generator example def incrementer(): i = 1 while True: yield i i += 1
incr = incrementer() print(next(incr))

19 Summary Functions Understanding scope Arguments Return Value Generator


Download ppt "Intro to CS Nov 29, 2016."

Similar presentations


Ads by Google