Functions are subprograms in the main program that contain a bundle of related statements of code that only run when they are called. They are written in order to perform particular tasks. Functions help us break our code into smaller chunks and avoid repetitiveness in code. They make the code more organized and increase its readability.
Most programming languages have the following two types of functions:
- Pre-Built/Built-in Functions
- User defined Functions
Python also has another small anonymous function called lambda function which will also be discussed in the following sections.
What is Built-in Functions
All major programming languages have built-in functions which are defined in the framework of the language. These functions can be called to perform tedious tasks by using just one line of code. For example,
print() are some of the examples of built-in functions in Python.
What is User-defined Functions
User-defined functions are custom functions created by the programmer to perform certain tasks in the code.
If a programmer needs to perform a task repetitively in the code e.g. outputting a welcome text whenever someone logs in or performing any arithmetic operation on numbers, then he/she can write a function and call it whenever there is a need to perform that task.
How to declare a function in Python
In Python, the def keyword is used to declare a function. It is followed by the name of the function along with the arguments/parameters in parenthesis. Then we use a colon (:) which marks the end of the header.
The body of the function is indented and all the statements inside the body have the same level of indentation. The return statement marks the end of a function in Python; It is however optional.
def function_name(parameters/arguments): statement(s)
Now let’s declare a function that prints a welcome message whenever it is called:
How to call a function in Python
In a program, a function needs to be called in order to execute the code present inside it. A function can be called by just using its name:
Using Return statement in Python Function’s
The return is an optional statement which can be used to return a value to the main program from the function. It returns a value to where the function was called:
How to pass arguments to a function in Python
Information can be passed from the main program to a function through variables as arguments. Any number of arguments can be passed to a function; they are specified in parenthesis and are separated by commas:
The number of arguments should be the same when defining and calling a function, otherwise, the program will give an error:
Output:Traceback (most recent call last): File "
", line 6, in TypeError: welcome() missing 1 required positional argument: 'second_name' >
Once you provided the correct number of arguments, the function will work properly as below:
Output:Welcome John Snow!
We can also pass a default parameter value. If the function is called without any arguments then it will use the default value:
Output:Welcome John Doe!
What is a lambda function in Python?
Lambda are anonymous functions (they have no name) that are declared and defined on a single line. They are great for simple single-line operations like mathematical operators. They follow a simple syntax:
lambda arguments: expression
They are declared by using the keyword lambda followed by the arguments which are separated from the expression using a colon (:). Now we will make a simple lambda function that takes two numbers and adds them together:
Functions are a collection of statements of code that are called whenever there is a need to perform a specific task. Functions allow programmers to divide programs into subprograms and enable them to just reuse code rather than rewriting it.
Python has three different types of functions i.e Built in, Custom and Lambda. The built in functions are already defined in the framework/libraries of the language. They enable us to easily perform complex tasks in a single step. The custom functions are written by the programmers themselves according to their needs. Lambda is small anonymous function in python.
In this post we have learned about Python functions. We learned to declare functions and call them from our main program.