User-defined Functions in Python

User-defined Functions

 

Functions that we define ourselves to do certain specific task are referred as user-defined functions.

Functions that readily comes with Python are called built-in functions. Python provides built-in functions like print(),input(), etc. but we can also create our own functions.

User-defined Functions in Python

Advantages :

  • User-defined functions help to decompose a large program into small segments.
  • Function can be used to include repeated codes and execute when needed by calling that function.
  • Large project can be divided by making different functions.

Syntax :


def functionName ():
    statements…



#start function that will print value
def start():
    print(“Start”)

#add func that will return the added value
def add(x,y):
    return x+y

#function that prints the argument
def myPrint(p):
    print(p)

    
#driver code
#main
#calling start func
start()

a=15
b=20

#calling add() and the returned value will be stored in result variable
result=add(a,b)

#calling myPrint func
myPrint(result)


Output :

Start
35