Functions in Python
Write functions to create a modular code
In Python, a function is a block of code that performs a specific task and may return a result. Functions help you organize and reuse your code, making it more modular and easier to understand and maintain.
Defining and calling functions
Here is the general syntax for defining a function in Python:
def function_name(parameters):
# code to be executed return result
Here is an example of a simple function that takes a single parameter and returns the square of that number:
def square(x):
return x * x
To call a function in Python, you simply need to use the function’s name followed by a set of parentheses, and any required parameters inside the parentheses. For example, you can call the square() function like this:
result = square(3)
print(result) # Output: 9
Functions can also have default values for their parameters, which means that you can call the function with fewer arguments than there are parameters. If you do not provide a value for a parameter with a default value, the default value will be used.
Here is an example of a function with default values for its parameters:
def power(x, exponent=2):
return x ** exponent
You can call this function with one or two arguments. If you provide two arguments, the first will be raised to the power of the second. If you only provide one argument, it will be squared (raised to the power of 2). Here are some examples of calling the power() function:
result = power(3)
print(result) # Output: 9
result = power(3, 3)
print(result) # Output: 27
* and ** Operators
In Python, the * operator can be used to call a function with a variable number of arguments. When used in a function call, the * operator is used to pass a tuple or a list of arguments as separate positional arguments to a function.
For example, you have a function that takes 3 arguments, and you have a tuple or list that contains these 3 arguments. You can use the * operator to unpack the tuple/list and pass its elements as separate positional arguments to the function. Here's an example:
def my_function(a, b, c):
print(a, b, c)
args = (1, 2, 3)
my_function(*args) # prints 1 2 3
This way the tuple args was unpacked as 3 separate arguments and passed to the function.
It’s also used to pass keyword arguments to function using ** operator to unpack the key-value pairs from a dictionary.
def my_function(a, b, c):
print(a, b, c)
kwargs = {"a": 1, "b": 2, "c": 3}
my_function(**kwargs) # prints 1 2 3
In the above example the dictionary kwargs was unpacked to separate keyword arguments and passed to the function.
It’s often used in a combination of both * and ** when calling a function, in this way:
def my_function(a, b, c, *args, **kwargs):
print(a, b, c, args, kwargs)
pos_args = (1, 2, 3)
key_args = {"d": 4, "e": 5}
my_function(*pos_args, **key_args) # prints 1 2 3 () {'d': 4, 'e': 5}
This allows a function to accept a variable number of arguments and keyword arguments, it’s also used in functional programming libraries like functools.partial and functools.wrap.
It’s important to note that when using * and ** operators to call a function, the arguments passed to them must be iterable (list, tuple) for * operator and a dictionary for ** operator, also the order of passing these arguments matters, *args needs to be passed before **kwargs.
Course index:
Have you spent your learning budget for this month, you can join Medium here: