Ad Code

Responsive Advertisement

Python User-Defined Functions


Python User-defined Functions



 


We will learn what are user-defined functions in Python what are its advantages and calling a function.



What will you learn in this tutorial?

In this tutorial you will learn the following topics like-:

  • What are the two types of functions in Python?

  • What are User-defined functions?

  • What are Built in Functions in Python?

  • Calling a Function in Python?

  • What is return in Python?

  • Advantages of Functions?

  • Programs on Functions?



What is user-defined function in Python?


Functions which are defined by the user to perform a specific task in a large system are called User-Defined Functions.


The functions which we use like print() are built in functions which are already present in python libraries.


All the functions that we write ourselves are user-defined functions which would be library for other person.


What are the advantages of user defined functions?


  • User defined function to compress a large program into small segments which makes the program easy to understand.

  • If you want to use one method again and again we can use user-defined functions which make program easy to write.

  • If you want to make a large project you can divide the workload by making different functions.

How to write a user defined function in Python?

  • User-defined function started with def and followed by function_name and arguments.

  • Arguments are written inside parenthesis () followed by colon [ : ]

  • After this we have to give four spaces to write the statements inside our functions (which are known as indentation in Python).

  • After writing the statements we call the function.

They are used as-:

def function_name():
    statements
function_name()

Parameterized Functions-:

Parameterized Functions are those function which takes arguments.

They are used as-:


def function_name(arguments,arguments_2):
    statements
function_name(x,y)


Let us see a example on user defined function in detail -:


Write a Python Program to add two Number [ by User-Defined Functions ].


def add ( n , m ):
    print( n + m )
x=add(3,4)

Output -: 7



  • Here we have defined the function add() which adds two numbers and prints the result.

  • Here actual arguments pass their values to formal arguments.

  • And then it will print the answer.

Questions for you-:


____ function_name():
    __________
function_name()
Top of Form


For any problem comment below.