Computer Programming - Lecture 6
Computer Programming - Lecture 6
Programming: Lecture
6
Outline
• Functions
• Scope
Void functions
• Until now we have been writing void functions.
• Void functions doesn’t return value to their caller.
•If you try to assign the result to a variable, you get a special value called None.
Greeting()
print(name)
Example
num = 6
def multi():
num = 6
num = num * 3 Local variable: 18
Global: 6
print(‘Local variable:', num)
multi()
print('Global: ', num)
Best programming practices
Avoid use of global variables in favour of local variables.
Default Parameters
Default parameters are those arguments that take default values if no explicit values are passed
to these arguments from the function call.
Default parameters needs to be defined at the end of the parameter list. They shouldn’t come before
any parameters without default values.
Function calling: Positional
The method of calling is what we have been using through out the class. The first argument is for
the first parameter, and the second argument is for the second parameter..
def print_numbers(a,b,c): a1
print("a",a) b3
print("b",b) c2
print("c",c)
print_numbers(b = 3,c = 2,a=1)
Functions: variable arguments
We can write functions that can accept variable length arguments like
the built-in functions such as max and min.