Python Function

Python Function :


  • Function is a block of reusable code or program  which can be called whenever required.
  • python allows us to divide a large program into the basic blocks of code known as function.
  • A function called multiple times.
  • python provide us various inbuilt function like print() , range() etc.which can called user defined function.
  • in python we can use def( ) keyword to define function.

Syntax :

     def function_name():
          statements....
          return  expression

Examples :

  • A simple Function without parameters 

     >>> def hello_world():
         print("Hello World")

     >>> hello_world()
     Hello World
     >>> 

  • function with one parameter.
     >>> a = 5
     >>> def num(a):
         print(a)


     >>> num(a)
     5
     >>> 

  • function with two parameter
     >>> def sum(a,b):
         return a + b

     >>> print(sum(10,20))
     30
     >>> 

Exercise : Comment your answer....Write a program to display number from 1 to 10 using function.

Comments

Post a Comment