Python Programming
About Lesson

As we already learnt, a function must be defined prior to its calling.  Then how it can be defined?  The definition of a function has two parts.  Header and body.  The following is the syntax of a function definition

 

Syntax of function definition:

def FuncName(Self,arg1,…,argN) as retType:

executable code

            executable code

 

 

The General format of header of the function looks like below

def funcName(Var1, Var2,…, VarN) :

 

where

funcName – The identifier or name of the function.  Choose an appropriate name to indicate its purpose.

Arguments – You can see number of items separated with commas inside parenthesis followed by funcName.  Those items are known as arguments or parameters those acts as holders for the values supplied by the caller function.  In other words, the caller function has to supply values for each every arguments by the time of calling this function.

 

The return keyword

If you want to define function to return some value to its caller then as the last statement of function definition you must use return statement.  You can also use return statement even your function is not returning any to indicate the end point of the function body.  No statement after return statement in the function body can get the chance of execution.

 

Syntax of return statement:

return

return (const)

return const

return (expr)

return expr

 

Types of function definitions

Defining a function is depends upon the logic in developer’s mind.  We also understood a function can accept zero to many number of arguments and can returns zero to one return value.  Based on this we can say, a function can be defined in four ways.

  1. No arguments and No return value
  2. No arguments but with return value
  3. with arguments and no return value
  4. with arguments and with return value

 

#definition of function without arguments and without return value

def add():

    x=int(input(“Enter first number”))

    y=int(input(“Enter second number”))

   

    print(“sum of numbers is :”,x+y)

 

add() #calling function

 

 

#definition of function without arguments but with return value

def add():

    x=int(input(“Enter first number”))

    y=int(input(“Enter second number”))

    return x+y

res=add() #calling function

print(“sum of numbers is :”,res)

You cannot copy content of this page