User Defined Functions in Python

Functions are a block of code that does a specific task. It saves the time of the developer as a full block of code can be executed by just calling the function. Functions can be broadly classified into two groups i.e. Built-in Functions, example print(), round(), etc. and User-defined Functions in Python which are defined to work as per user’s needs.

User-Defined Functions

In Python, we can declare a user-defined function by the keyword def followed by the function name. The function may take arguments as input within the opening and closing parentheses, just after the function name followed by a colon. Once we have defined the function name and arguments, a block of program statement starts at the next line and these statements must be indented.

Syntax

User Defined Function Syntax

Call a Function

In Python, calling a function is pretty simple, using the function name, parenthesis (opening and closing) and parameter(s) if required. See the

Syntax

          function_name(arg1, arg2)

Example

Output

Hello World
How are you ?

In the above example,

We have defined a function Hello without parameters. This function is programmed to print “Hello World. How are you?” whenever called.

Advantages of user-defined functions

  • User-defined functions help to decompose a large program into small segments which make the program easy to understand, maintain and debug.
  • These functions are very useful, from writing common utilities to specific business logic. These functions can also be modified per requirement.
  • If repeated code occurs in a program. A function can be used to include those codes and execute when needed by calling that function.
  • Large project workload can be divided by making different functions.

Functions with arguments

In Python, user-defined functions can take four types of arguments. The argument types and their meanings, however, are predefined and can’t be changed. A developer has to follow these pre-defined rules to make their own custom functions. Below are the 4 types of arguments

  1. Default Arguments
  2. Required Arguments
  3. Keyword Arguments
  4. Variable length Arguments

Default Arguments

In Python, the function arguments can have default values. If the Default Argument function is called without passing any argument, the argument takes its default value. The default value for the Function is assigned by assignment (=) operator.

Example

Output

Hello Harry
Welcome Harry




In the above example, we have defined a function def_func with the default argument greeting as Hello. This function is programmed to print “Hello ”, whenever called and no parameter is passed into greetings. When we called the function the second time we passed “Welcome” into argument greetings, this time greetings argument will take the passed parameter.

Required Arguments

In Python, the required arguments are the mandatory arguments of a function. while calling the required argument function we have to pass the correct number of arguments along with their correct order. If we do not pass all the arguments or pass arguments in the wrong order, the function will fail.

Example

Output

My name is Harry
My age is  12

 

Keyword Arguments

In Python, Keyword Argument Function in Python is the same as a required argument, the difference is we mention the keyword during the function call along with the corresponding values. We do not need to maintain the order of the arguments. Below we will consider the same example we used for required arguments.

Example


Output

My name is Harry
My age is  12


Variable length Arguments

In Python, Variable-length argument is used when we do not know the number of arguments to be passed. to create a variable length function we have to pass *args as an argument while creating the Function.

Example

Output

Hello
World!!!
this is Var len Function

The Return Statement

The return statement is used to return the required variable to the code and exit a function and go back to the place from where it was called. In all the above example we have not used a return statement. Therefore we can conclude that the return statement is not mandatory. The values returned by the function can be used later in the code.

Syntax

          Return(expression)

It is possible that a return statement contains an expression that gets evaluated and the value is returned. If there is no expression in the statement, then the function will return the None object.

Example

Output

product of x & y is 20



Please comment your question related to this topic.

To Stay in touch, follow us on Twitter and Facebook.

View previous post IF, IF ELSE, ELIF Statements

About the author

Gaurav Tiwari

My Name is Gaurav Tiwari. I am working in the IT industry for over 3.5+ years. I completed my B.E. from Mumbai University in 2015, Since then I’m working with Accenture Solutions PVT. LTD. as data Analyst.
I’ve started writing blogs as hobby.

View all posts

Leave a Reply

Your e-mail address will not be published. Required fields are marked *