0% found this document useful (0 votes)
5 views13 pages

Computer Programming - Lecture 6

The document covers key concepts in computer programming, focusing on functions, their types (void and fruitful), and scope. It explains the differences between global and local variables, best practices for using local variables, and how to use default parameters and various function calling methods. Additionally, it introduces functions that accept variable-length arguments.

Uploaded by

Panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

Computer Programming - Lecture 6

The document covers key concepts in computer programming, focusing on functions, their types (void and fruitful), and scope. It explains the differences between global and local variables, best practices for using local variables, and how to use default parameters and various function calling methods. Additionally, it introduces functions that accept variable-length arguments.

Uploaded by

Panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Computer

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.

def greeting(name): def maximum(x,y):


print(‘hi ’,name) if(x > y):
print(x)
greeting(‘Haven’) else:
greeting(‘Robel’) print(y)
maximum(2,4)
Fruitful functions
• A function that returns a value to its caller.
• When a return statement executes, the function terminates without executing any subsequent
statements. The code the could not be reached by the flow execution is called dead code.

def greeting(name): def maximum(x,y):


print(‘hi ’,name) if(x > y):
return x
greeting(‘Haven’) else:
greeting(‘Robel’) return y
max_val = maximum(2,4)
print(max_val)
Scope
• A variable can have something called a ‘scope’. This refers to its
accessibility.
• There are two types of scopes:
• Global: accessible from everywhere.
• Local: only accessible within a function
Scope
name = "Hello" #global variable def my_function():
def my_function():
name = "Hello" #local variable
print(name)
print(name)
my_function()
This program raises an error
This program outputs => Hello
since the variable name is not
accessible outside the function.
Example
name = 'Mary'
def Greeting():
name = 'Mary Poppins' Marry Poppins
Marry
print(name)

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.

def greet(name="World"): Hello, World


print("Hello, " + name) Hello, Abebe
greet()
greet("Abebe")

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 maximum(x,y): x=2


if(x > y): y=4
return x
else:
return y
max_val = maximum(2,4)
print(max_val)
Function Calling: Keyword
In python, you can pass arguments with the key = value syntax. This way the arguments order
doesn’t matter.

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.

def func(*params): Demo: Let us write our own


for i in params: max and min functions.
print(i)
func(1,2,3,4)
func(1)
func(1,2,3)

You might also like