Learn How to write user defined functions in Python

0
1862
Learn-How-to-write-user-defined-functions-in-Python

Learn-How-to-write-user-defined-functions-in-Python

Functions are nothing but a block of re-usable code to perform a specific task. It helps in code re-usability and modular application design. In every language ‘function’ is an integral part; sometimes they are also called methods. Similarly, Python has two types of functions, broadly categorized as ‘built-in’ and ‘user-defined’. Built-in functions are a part of Python package, whereas user-defined functions are created by the developers as per requirement.

In Python, functions get equal importance like other objects. So, they can be used as objects in the application. It gives Python additional flexibility and advantages compared to the other high-level languages.
In this article, we will mainly focus on the user-defined functions and how they can be written in Python.

Why we need user-defined functions?
In programming, we generally write code to solve some problem or implement some functionality. These functionalities can be generic or specific to applications. Now, for generic problems, built-in functions are sufficient, as they are made to solve issues which are common to any application. But, for specific requirement, we need to write our own code, which can be modularized as functions, popularly known as user-defined functions.

In this context, we must have a look at some of the advantages of user-defined functions.

  • Users can split large programming code into small logical chunks and put it into blocks, which are nothing but user-defined functions. It helps to read, maintain and debug application code in a better way.
  • User-defined functions are very useful for repetitive code usage. It reduces development time and effort.
  • User-defined functions are re-usable components. So it can be used across application code.
  • These functions can be modified or changed as per requirement, without changing the application code.

In short, user-defined functions are very useful for making custom applications in any language like Python.

Steps to write user defined functions in Python?
In this section we will explain how to write a user-defined function in Python. Following are steps to be followed.

  • First, you need to write the declaration part of the function. In Python, it starts with a keyword ‘def’ and then the function name.
  • Second, function in Python can take argument(s) or it can be without any argument. Arguments are separated by commas within the opening and closing parentheses of the function. Function name ends with a colon.
  • Third, after the declaration part, program statements are written and they should be indented.
  • Lastly, functions can or cannot have a return statement.

Following is the syntax of a sample user-defined function.

Listing1: Syntax of a sample user defined function

Note: In Python language program statements are not enclosed within parentheses {}

After this we need to call the function. Following is the syntax to call the above function. Please note that the calling of a function in Python does not end with a semicolon.

Now, let’s have a look at different types of arguments in Python.

What are the function arguments?
In Python, different types of arguments can be passed to a user-defined function. Following are the types with explanation.

Required arguments: These are the arguments which should match the function signature. It means, arguments should have same sequence and number as defined in the declaration part.

Keyword arguments: These are used to pass the parameter value to a function along with the keyword. Python interpreter can identify the values based on the keyword.

Variable length arguments: These are useful when you are not sure how many arguments will be passed to a function at the time of declaration. It makes the function flexible to take any number of arguments as per requirement.

Default arguments: It is important when you want to have a value passed to your function, even if not mentioned at the time of call. So the default value is set at the time of declaration.

In the next section we will have sample applications for all the above types.

Let’s try some examples
In this section we will check some examples of user-defined functions for each of the argument types mentioned above

Required arguments example: Here an argument has to be passed to the function; otherwise it will throw an error.

Listing2: Required argument example

The output would be as mentioned below

Now, call the function without an argument as shown below.

The output will show an error.

Keyword arguments example: In this type of argument, parameter name is matched to get the value passed to the called function

Listing3: Keyword argument example

The output would be as shown below.

Variable length arguments example: In this type of argument any number of argument values can be passed to the user-defined function. 

Listing4: Variable length arguments example

The output would be as shown below.

Default arguments example: In this type of argument default value is mentioned in the function declaration. So, if the value is not passed to the called function, then default value is taken.

Listing5: Default arguments example

The output would be as shown below. Please note that when the value of addr variable is not passed, it takes the default value ‘’kolkata”.

Conclusion:
User defined functions are very important in any programming languages. Python, as a scripting language also have the support for user defined functions. We have already discussed about the syntax and implementation details above. We have also touched some of the differentiators available in Python compared to other languages. Overall, Python provides a lot of flexibility in defining and using user-defined functions. Hope the above details and code samples will help the users to implement custom functions in Python.

LEAVE A REPLY

Please enter your comment!
Please enter your name here