Functions in Python
Functions in Python
Class: M2
PREP SCHOOL
ICT Department
Objectives
Students will be able to:
• Defining a function
• Calling a function
Examples
• input()
• print()
• append()
Benefits of Functions
•Reusability
def my_function():
print(“Hello World!”)
Example: 2
def cube():
a=int(input(“Enter a number”))
b=a**3
print(b)
return b
Calling a Function
•To call a function, use the function name followed by parenthesis.
Example:
def my_function():
print(“Hello World!”)
Calling function:
my_function()
Output:
>>>Hello World!
Passing Arguments
• Information can be passed into functions as arguments.
• Arguments are specified after the function name, inside the parentheses. You can
add as many arguments as you want, just separate them with a comma.
• The terms parameter and argument can be used for the same thing: information
that are passed into a function.
Example:
def addition(x,y):
print(x+y)
Calling function:
addition(2,3)
Output:
>>> 5
Recap
In this lesson, we have discussed:
• Calling a function