SDF-Week13
SDF-Week13
Note:
• Some of the slides of the course are based on the material provided
by the College of Technological Information, Zayed University, UAE.
• The copyrighted material belongs to their respective owners.
1
11/28/23
Topics of Discussion
• Functions
• Types of Functions
• Built-in functions
• User-defined functions
Functions
Modular Programs
2
11/28/23
Functions
• A Function is a group of related statements
that perform a single specific task.
Functions
• f(x) = 15x2 + 4x + 2
• f(3) gives 149
3
11/28/23
Types of Functions
We can divide functions into the following two types:
• Built-in functions -
• Functions that are built into Python.
• User-defined functions -
• Functions defined by the users themselves.
4
11/28/23
User-defined Functions
•Functions defined by the users themselves.
•For example:
• A function to find an even number
• isEven()
• A function to find the greatest of 3 numbers
• greatestOfThree()
10
5
11/28/23
Example of a function
# Name: greet
# Desc: This Function greets a person
# Parameters: name - string
# Return: None
11
11
Function Call
• Once we have defined a • Function call:
function, we can call it from: greet(“Amna”)
• Another function
• The program
• The Python prompt
• Output
• To call a function: Hello, Amna. Good morning!
• type the function name with
appropriate parameters.
12
12
6
11/28/23
Function Arguments
• A function can have one or more arguments.
• In the above function, name & msg are the formal arguments
# Function Call
greet("Mariam", "Good morning!")
• Output
Hello Mariam, Good morning!
13
13
Class Work
# Name: sumThreeNums
# Desc: Add 3 numbers
# Parameters: num1 - int, num2 - int, num3 - int
# Return: int
# Function Call
print(sumThreeNums(4, 5, 6))
14
14
7
11/28/23
15
Class Work
# Name: absVal
# Desc: Returns Absolute Value
# Parameters: num - int
# Return: int
def absVal(num):
if num >= 0:
return num
else:
return –num
# Function Call
print(absVal(-5))
print(absVal(-100))
print(absVal(-15))
16
16
8
11/28/23
Class Work
# Name: greatestOfThree
# Desc: Finds greatest of 3 numbers
# Parameters: num1 - int, num2 - int, num3 - int
# Return: int
# Function Call
print(greatestOfThree(4, 5, 6))
print(greatestOfThree(6, 4, 5))
17
17
Class Work
# Name: greatestOfThree
# Desc: Finds greatest of 3 numbers
# Parameters: num1 - int, num2 - int, num3 - int
# Return: int
# Function Call
print(greatestOfThree(6, 4, 5))
18
18
9
11/28/23
Class Work
# Name: isEven # Function Call
# Desc: Finds Even or Not n = int(input("Enter a number: "))
# Parameters: num - int
# Return: Boolean if isEven(n):
print(n, " is even")
else:
def isEven(num): print(n, " is not even")
if num%2 == 0:
return True
else:
return False
19
19
Class Work
• Write a python function sumN() def sumN(n)
that takes an integer N as # Write Loop to add numbers from 1 to n
parameter and returns the sum
of all numbers from 1 to N.
# Function Call
# Name: sumN
result = sumN(10)
# Desc: Adds numbers 1 to n
# Parameters: n - int print(“ The sum of first N nums: “, result)
# Return: int
20
20
10
11/28/23
21
21
Class Work
Question 1:
Write a program with a method named
getTotal that accepts two integers as an
argument and return its sum. Call this
method and print the results.
Question 2:
Write a function, reverseDigit, that
takes an integer as a parameter and
returns the number with its digits
reversed. For example, the value of
reverseDigit(12345) is 54321. Also, write
a program to test your method.
22
22
11
11/28/23
Class Work
• Write program that acts as simple # Hints
calculator. def add(x, y):
return()
def subtract(x, y):
• Consider calculation for simple
math operations ( + - * /). return()
def multiply(x, y):
return()
• Write the functions with good def divide(x, y):
documentation return()
23
Class Work
Write a function dot_product(List1,List2) that takes two Lists of numbers of
the same length. The function returns the sum of the products of the
corresponding elements of each list.
# Function Call
dot_product([1, 2], [1, 4]) # Output: 9
24
24
12
11/28/23
25
25
Recursion
26
26
13
11/28/23
Recursion
27
27
Recursion
28
28
14
11/28/23
Disadvantages
1.Sometimes the logic behind recursion is hard to follow through.
2.Recursive calls are expensive (inefficient) as they take up a lot of memory and
time.
3.Recursive functions are hard to debug.
29
29
Summary
-Functions
-Types of Functions
• Built-in functions
• User-defined functions
- Check the following link:
https://www.programiz.com/python-programming/examples
30
30
15