Functionlec
Functionlec
Functions in Python are first-class citizens. It means that functions have equal status
with other objects in Python. Functions can be assigned to variables, stored in
collections or passed as arguments. This brings additional flexibility to the language.
Function types
There are two basic types of functions. Built-in functions and user defined ones. The
built-in functions are part of the Python language. Examples are: dir(), len() or abs().
Using the Math Library
Using the Math Library: An Example
#To call a function from the math library, we can use the
#dot operator as follows:
s_root_val = math.sqrt(b*b - 4 * a * c)
root1 = (-b + s_root_val)/(2*a)
root2 = (-b - s_root_val)/(2*a)
print()
print("The solutions are: ", root1, root2)
hello2.py
• Syntax:
# Prints a helpful message. 1
def name(): def hello(): 2
statement print("Hello, world!") 3
4
statement # main (calls hello twice) 5
... hello() 6
hello() 7
statement
def get_final_answer(filename):
def my_function(): “““Documentation String”””
print("Hello ") line1
line2 Colon.
return total_counter
my_function(“zana")
my_function(“ali")
my_function(“ ahmed")
Local Variables
• Consider the following code
func2()
print(x)
x is said to be a global variable since it is defined within the
global scope of the program and can be, subsequently, accessed
inside and outside func2()
Local vs. Global Variables
• Consider the following code
x = 100
def func3():
x = 20 20
Run
print(x) 100
func3()
print(x)
The global variable x is distinct from the local variable x inside
func3()
Parameters vs. Global Variables
• Consider the following code
x = 100
def func4(x):
print(x) 20
Run
100
func4(20)
print(x)
def func5():
global x
x = 20
print(x) 20
Run
20
func5()
print(x)
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
Getting Results From Functions
• We can get information from a function by having it return
a value
>>> def square(x): >>> def cube(x): >>> def power(a, b):
... return x * x ... return x * x * x ... return a ** b
... ... ...
>>> square(3) >>> cube(3) >>> power(2, 3)
9 27 8
>>> >>> >>>
Pass By Value
• Consider the following code
def addInterest(balance, rate):
newBalance = balance * (1+rate)
return newBalance
def test():
amount = 1000
rate = 0.05
nb = addInterest(amount, rate)
print(nb)
test()
1050.0
Pass By Value vs. Returning a Value
• Consider the following code
def increment_func(x):
def increment_func(x): x=x+1
x=x+1 return x
x=1 x=1
increment_func(x) x = increment_func(x)
print(x) print(x)
Run
Run
1 2
Passing a List as a Parameter
You can send any data types of parameter to a
function (string, number, list, dictionary etc.), and
it will be treated as the same data type inside the
.function
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
Default Values for Arguments
You can provide default values for a function’s arguments
These arguments are optional when the function is called
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")