Defining a Function:
Function blocks begin with the keyword "def" followed by the function Name and parentheses ( ).
Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses. The code block within every function starts with a colon (:)
Example 01:
Here, we have defined a Happy Birthday Function.
def happyBirthday(name):
print("Happy Birthday to you!")
print("Happy Birthday to you!")
print("Happy Birthday, dear, " + name + "!")
print("Happy Birthday to you!\n")
happyBirthday('Jyoti')
print("Happy Birthday to you!")
print("Happy Birthday to you!")
print("Happy Birthday, dear, " + name + "!")
print("Happy Birthday to you!\n")
happyBirthday('Jyoti')
and you will see following output.
Example 02:
Same Happy Birthday Function, however, input from user
def happyBirthday(name):
print("Happy Birthday to you!")
print("Happy Birthday to you!")
print("Happy Birthday, dear, " + name + "!")
print("Happy Birthday to you!\n")
def main():
userName = input("Enter the Birthday person's Name: ")
happyBirthday(userName)
main()
print("Happy Birthday to you!")
print("Happy Birthday to you!")
print("Happy Birthday, dear, " + name + "!")
print("Happy Birthday to you!\n")
def main():
userName = input("Enter the Birthday person's Name: ")
happyBirthday(userName)
main()
and you will see following output.
Example 03:
Function with more than one input parameter. Here function sumfunc add two input value, which is called from main function.
def sumfunc(x, y):
sum = x + y
sentence = 'The sum of {} and {} is {}.'.format(x, y, sum) # Remember, no comma
print(sentence)
def main():
a = int(input("Enter an integer: "))
b = int(input("Enter another integer: "))
sumfunc(a, b)
main()
sum = x + y
sentence = 'The sum of {} and {} is {}.'.format(x, y, sum) # Remember, no comma
print(sentence)
def main():
a = int(input("Enter an integer: "))
b = int(input("Enter another integer: "))
sumfunc(a, b)
main()
and you will see following output.
this same code can be written in a slight different way,
def sumfunc(x, y):
sum = x + y
return sum
def main():
a = int(input("Enter an integer: "))
b = int(input("Enter another integer: "))
c = sumfunc(a, b)
print("The sum is:", c)
main()
sum = x + y
return sum
def main():
a = int(input("Enter an integer: "))
b = int(input("Enter another integer: "))
c = sumfunc(a, b)
print("The sum is:", c)
main()
Example 04:
Here, function with global constant are shown. We defined PI as a global constant and calculated the Circle Area and Circumference.
PI=3.14159 # defining global constant
def circleArea(radius):
return PI*radius*radius # Circle Area = PI*r^2
def circleCircumference(radius):
return 2*PI*radius
def main():
r=int(input("Enter tha Radius of the circle in meter:")) # you may use float
cA=circleArea(r)
cC=circleCircumference(r)
print("Circle Area: {} m^2".format(cA)) # Remember, no comma
print("Circle Circumference: {} m ".format(cC))
main()
def circleArea(radius):
return PI*radius*radius # Circle Area = PI*r^2
def circleCircumference(radius):
return 2*PI*radius
def main():
r=int(input("Enter tha Radius of the circle in meter:")) # you may use float
cA=circleArea(r)
cC=circleCircumference(r)
print("Circle Area: {} m^2".format(cA)) # Remember, no comma
print("Circle Circumference: {} m ".format(cC))
main()
and you will see following output.