Function in python



Function is a block of statement,It will be executed whenever it is called in the program.








Types of function:



  1. No argument no return type
  2. With argument no return type
  3. No argument with return type
  4. With argument with return type







1. No argument no return type



First we had declare the function name as science. Then inside the function,we print the "physics". Next we had call the function name as "science()",we have get the output as "physics" .




Example
def science():

   print("physics")

science()


Output



physics







2. With argument no return type



First we had declare the function name as "number".With in the function we assum the argument name as "n1". Then inside the function,we print the value of n1. Next we had call the function name as "number",with argument value for "9".



Example
def number(n):

   print(n)

number(9)


Output



9







3. No argument with return type



First we had declare the function name as "number()". Then inside the function,we return the value 7. Next we had call the function name as "number" inside the print function. Final output is 7.




Example
def number():

    return 7;

print(number())


Output



7







4. With argument with return type



First we had declare the function name as "add_num".With in the function we assum the two argument n and m. Next we had add the n and m,then store the value in the sum.Then return the sum. we had assume the n1=25,n2=55.Inside the print function,we have call the functin name as "add_num(n1,n2)". So The final output is 80.




Example
def add_num(n,m):

    sum = n + m;

    return sum;


print(add_num(25,55))


Output



80