Functions in PythonGroup3
Functions in PythonGroup3
Group 3
Functions are blocks of code that only execute when they are called
For example
def adding_numbers(x, y):
total = x + y
return total
This function, when called, will add numbers x and y and return the value called
total
• After declaring a Function the next step is to Call it
• This is done because the function will not be executed unless its Called
def adding_numbers(x,y):
total = x + y
return total
print(adding_numbers(2,5)) #this is call statement
• When calling the function we had to pass (2,5) as arguments
• The number of arguments passed at declaration of the function must equal the
number of arguments passed when the Function is called
EXAMPLES OF FUNCTIONS
Output:
What is your name: Taku
Welcome home Taku we have missed you
Hope you stay a while
EXAMPLES OF FUNCTIONS
"""creating a function that checks for temperature and returns a text. The program
terminates if input is zero""“
x = int(input("Enter the temperature: "))
def weather_report(x):
if x < 60:
print("Its too cold")
elif x > 100:
print("Its too hot")
else:
print("its just right")
while x != 0:
print(weather_report(x))
Benefits of Using Functions