How to Create a Function in Python
A function is a reusable block of code that performs a certain functionality. Functions are fundamental building blocks of a programming language as they introduce modularity and reusability in the code.
In this article, we will learn how to create and call a function in python. We will also go through the important concepts like function arguments and return statements to make full use of python functions.
Creating and Calling a function
In Python, we define a function using the def keyword followed by the name of the function, parentheses, and a colon.
Most programming languages enclose function bodies inside curly braces, but python works around the indentation. So, the function body starts with 4 spaces or a tab space.
def my_function(): print("Hello World")
Write the name of the function with parenthesis to call it.
def my_function(): print("Hello World") my_function() Output: Hello World
Function arguments allows to pass information to the function. The scope of these arguments is restricted to the function body means we cannot access the arguments outside the function body.
Arguments are specified inside the function parentheses, separated by a comma. You can pass as many arguments as you want.
def my_function(first_name): print(first_name + " Holland" my_function("Jill") my_function("Bill"
Jill Holland Bill Holland
Arguments & Parameters
The terms argument and parameter refer to the same thing that is the information passed into a function. However, from the perspective of a function, they are different in the following manner.
|An argument is a value that is passed to the function when it is called.||A parameter is a variable passed inside the function parentheses while defining it.|
Note: By default, you have to pass an exact number of arguments to the function while calling it. For instance, if a function expects 3 arguments, you have to call it with exactly 2 arguments.
Arbitrary Arguments – *args
Arbitrary arguments are generally shortened to *args in the official documentation. If we don’t know how many arguments will be passed to the function while calling it, we can use * before the name of the parameter.
def my_function(*fruits): print("My favourite fruit is " + fruits) my_function("Apple", "Mango", "Grapes")
My favorite fruit is Mango
*args pass a tuple of parameters to the function. So, we can access the tuple by specifying the index.
We can also pass arguments in the form of key = value. In this way, the order of arguments while calling the function does not matter.
def my_function(month1, month3, month2): print("The coldest month is " + month3) my_function(month1 = "November", month2 = "December", month3 = "January")
The coldest month is January
Arbitrary Keyword Arguments – **kwargs
Just like we studied arbitrary arguments before, arbitrary keyword arguments are also use when we don’t know the exact number of keyword arguments the function will receive.
In the following example, we place two asterisks ** before the parameter name to pass arbitrary keyword arguments.
def my_function(**person): print("Person’s last name is " + person["last_name"]) my_function(first_name = "James", last_name = "Madison")
Person's last is Madison
Passing a list as Function Argument
You can pass argument of any data type in python function like string, number, dictionary, list etc. The type of argument will persist inside the function. For example, if we pass a list as an argument, then we can loop through the list inside the function body.
def my_function(fruits): for x in fruits: print(x) fruits_list = ["Grapes", "Mango", "Apple"] my_function(fruits_list)
Return from a function
Use return keyword to return value from the function.
def my_function(y): return 3 * y print(my_function(2)) print(my_function(4)) Output: 6 12
Tip: Python does not allow to define function without body or empty definition. But, for some use cases, you have to create a function with an empty body, you can use the pass keyword to avoid the error.
def my_function(): pass
Wrapping up the guide, we have learned the python functions and their associated material. Python also accepts function recursion means that a function can call itself inside its body. However, recursion is a mathematical and programming concept, you can learn this after getting grip on this guide.