CCD Module15-PL101 01
CCD Module15-PL101 01
1
“Python Functions”
In this lesson, we will learn about the Python function and function expressions with the
help of examples.
References:
• Python Programming for Beginners
INFORMATION SHEET PL 101-15.1.1
“Python Functions”
Suppose, you need to create a program to create a circle and color it. You can create two functions to
Dividing a complex problem into smaller chunks makes our program easy to understand and reuse.
Types of function
Standard library functions - These are built-in functions in Python that are available to use.
User-defined functions - We can create our own functions based on our requirements.
Here,
def - keyword used to declare a function
def function_name(arguments):
# function body
return
def greet():
print('Hello World!')
Here, we have created a function named greet(). It simply prints the text Hello World!.
This function doesn't have any arguments and doesn't return any values. We will learn about arguments
def greet():
print('Hello World!')
print('Outside function')
Output
Hello World!
Outside function
When the function is called, the control of the program goes to the function definition.
As mentioned earlier, a function can also have arguments. A arguments is a value that is accepted by a
If we create a function with arguments, we need to pass the corresponding values while calling them.
For example,
Here, add_numbers(5, 4) specifies that arguments num1 and num2 will get values 5 and 4 respectively.
# Output: Sum: 9
We can also call the function by mentioning the argument name as:
add_numbers(num1 = 5, num2 = 4)
In Python, we call it Keyword Argument (or named argument). The code above is equivalent to
add_numbers(5, 4)
A Python function may or may not return a value. If we want our function to return some value
to a function call, we use the return statement. For example,
def add_numbers():
...
return sum
Note: The return statement also denotes that the function has ended. Any code after return is
not executed.
# function call
square = find_square(3)
print('Square:',square)
# Output: Square: 9
In the above example, we have created a function named find_square(). The function accepts a
# Output: Sum: 9
In Python, standard library functions are the built-in functions that can be used directly in our program.
For example,
print() - prints the string inside the quotation marks
These library functions are defined inside the module. And, to use them we must include the module
Output
import math
Since sqrt() is defined inside the math module, we need to include it in our program.
1. Code Reusable - We can use the same function multiple times in our program which makes our code
for i in [1,2,3]:
# function call
result = get_square(i)
print('Square of',i, '=',result)
Output
Square of 1 = 1
Square of 2 = 4
Square of 3 = 9
In the above example, we have created the function named get_square() to calculate the square
of a number. Here, the function is used to calculate the square of numbers from 1 to 3.
2. Code Readability - Functions help us break our code into chunks to make our program readable and
easy to understand.
Python Excercises
Exercise 1: Create a function in Python
Write a program to create a function that takes two arguments, name and age, and print their
value.
# call function
demo("Ben", 25)
Note: Create a function in such a way that we can pass any number of arguments to this function, and
the function should process them and display each argument’s value.
def func1(*args):
for i in args:
print(i)
Solution 1:
Solution 2:
It should accept the employee’s name and salary and display both.
If the salary is missing in the function call then assign default value 9000 to salary.
show_employee("Ben", 12000)
show_employee("Jessa")
Exercise 5: Create an inner function to calculate the addition in the following way
Create an outer function that will accept two parameters, a and b
Create an inner function inside an outer function that will calculate the addition of a and b
At last, an outer function will add 5 into addition and return it
# outer function
def outer_fun(a, b):
square = a ** 2
# inner function
def addition(a, b):
return a + b
def addition(num):
if num:
# call same function by reducing number by 1
return num + addition(num - 1)
else:
return 0
res = addition(10)
print(res)
Exercise 7: Assign a different name to function and call it through the new name
Below is the function display_student(name, age). Assign a new name show_tudent(name, age)
to it and call it using the new name.
PRECAUTIONS:
Do not just copy all your output from the internet.
Use citation and credit to the owner if necessary.
ASSESSMENT METHOD: WRITTEN WORK CRITERIA CHECKLIST
STUDENT NAME: _____________________________ SECTION: __________________
CRITERIA SCORING
Did I . . .
1 2 3 4 5
1. Focus - The single controlling point made with an awareness of a
task about a specific topic.
2. Content - The presentation of ideas developed through facts,
examples, anecdotes, details, opinions, statistics, reasons, and/or
opinions
3. Organization – The order developed and sustained within and
across paragraphs using transitional devices and including the
introduction and conclusion.
4. Style – The choice, use, and arrangement of words and sentence
structures that create tone and voice.
5. .
6. .
7. .
8. .
9. .
10. .
TEACHER’S REMARKS: QUIZ RECITATION
PROJECT
GRADE:
5 - Excellently Performed
4 - Very Satisfactorily Performed
3 - Satisfactorily Performed
2 - Fairly Performed
1 - Poorly Performed
_______________________________
TEACHER
Date: ______________________