Functions
LECTURE 4
Functions
2 Types:
1. Standard library functions
◦ built-in functions in Python that are available to use
2. User-defined functions
◦ functions that we define ourselves to do certain
specific task
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Function Declaration
▪syntax to declare a function:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Function Declaration
▪ex:
def greet():
print('Hello World!')
def add_numbers(num1, num2):
sum = num1 + num2
print('Sum: ',sum)
def add_numbers(num1, num2):
sum = num1 + num2
return sum
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Calling a Function
▪necessary to use a function
def greet():
print('Hello World!’)
function call greet()
def add_numbers(num1, num2):
sum = num1 + num2
print('Sum: ',sum)
function call add_numbers(2,5)
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Calling a Function
▪necessary to use a function
def add_numbers(num1, num2):
sum = num1 + num2
return sum
add_numbers(2,5)
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Exercises:
1. Write a ▪sum()
Python function ▪takes one argument, an iterable
to sum all the whose items are numbers, and
returns the sum of the numbers
numbers in a print(sum([5,3,2,1]))
list.
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
2. Write a Python function to multiply all
the numbers in a list.
3. Write a Python function to reverse a
string.
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
4. Write a Python function calc() that accepts two
variables and returns both the sum and difference in a
single return call.
5. Write a Python function that accepts two arguments,
min and max, and generates a Python list of all the even
numbers between min and max (min and max are
excluded).
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
6. Write a Python ▪isupper()
▪returns True if all the
function that accepts a characters in a string are in
upper case, otherwise
string and counts the False
number of upper and str1 = 'HELLO'
[Link]()
lower case letters.
stringCaseCounter("Hello Python World!") ▪islower()
▪returns True if all the
Output:
characters are in lower
case, otherwise False
No. of upper case characters: 3 str1 = 'h'
No. of lower case characters: 13
[Link]()
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Variable Scopes
1. local scope
◦ scope of variables declared inside a function
2. global
◦ scope of variables declared inside a function
3. nonlocal scope
◦ used in nested functions whose local scope is not defined
◦ variable can be neither in the local nor the global scope
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
def outer():
message = 'local'
# nested function
def inner():
# declare nonlocal variable
nonlocal message
message = 'nonlocal'
print("inner:", message)
inner()
print("outer:", message)
outer()
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Global keyword
▪allows user to modify a variable outside of its
current scope
c = 1
c = 1 c = 1
def add(): def add():
print(c) def add(): global c
c = c + 2 c = c + 2
add() print(c) print(c)
add() add()
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Function Recursion
Recursion
▪process of defining
something in terms of
itself
Function Recursion
▪function calling itself
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
def factorial(x):
if x == 1: return 1
else: return (x * factorial(x-1))
num = 3
print(factorial(num))
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
▪By default, maximum depth of recursion is
1000. If the limit is crossed, it results in
RecursionError
def recursor():
recursor()
recursor()
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Lambda/Anonymous Function
lambda function
▪a special type of function without the function
name
▪syntax: lambda argument(s) : expression
▪ argument(s) - any value passed to the lambda function
▪ expression - expression is executed and returned
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Lambda/Anonymous Function
greet = lambda : print('Hello World’)
greet()
sum = lambda x,y : x + y
Print(sum(2,9))
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Modules
Module
▪a file that contains code to perform a specific task
▪may contain variables, functions, classes etc
def add(x,y): import arithmeticFunc
return x+y
[Link](2,9)
def subtract(x,y): [Link](2,9)
return x-y
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
References
▪Python Functions (Programiz)
▪[Link]
programming/function?fbclid=IwAR1nM218JGMOgovIszWMR0LTtzv9hZzQ
QYEvRsoQrha3L08qjpTcU9JuGFU
▪Python Functions (W3 Schools)
▪[Link]
_jdar-tGRblRrVsCJ1sC6nOK2OLMhmjTeauHYQjuvVQhWyTFmfqqnE
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova