DEV Community

Cover image for Python Functions – How to write, call and the best ways to write a function
Michael Abraham Wekesa
Michael Abraham Wekesa

Posted on • Updated on

Python Functions – How to write, call and the best ways to write a function

Functions are a set of statements bundled together to perform a specific task. A function can accept arguments that are manipulated to cause about a specific output or effect as intended.
Functions are often used in order to organize code, including getting rid of repetitive code, thus helps greatly in achieving the DRY principle in software engineering. DRY stands for 'Don't Repeat Yourself'.

Functions can be seen as executable code blocks and can be called once or more.

In Python we define functions using the def keyword followed by the function's name and finally the parenthesis. Parameters to the function are defined within the parenthesis.

When calling a function that accepts some arguments, we pass them in the parenthesis.

An example of a function:


def print_hello_world():
    print("Hello World!")
Enter fullscreen mode Exit fullscreen mode

When calling the function:


print_hello_world()
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World!
Enter fullscreen mode Exit fullscreen mode

Functions can have parameters, parameters are used as placeholders or variables for inputs that will be passed as inputs during the calling of a function. Arguments on the other hand are values that are passed in a function as inputs to the function.

Functions with parameters

Declaring parameters when defining a function:

def sum_two_numbers(a, b):
  sum = a + b
  return sum
Enter fullscreen mode Exit fullscreen mode

When calling the function we pass in the arguments, which in this case are a and b. For a = 3 and b = 4;

sum_two_numbers(3, 4)
Enter fullscreen mode Exit fullscreen mode

Output:

7
Enter fullscreen mode Exit fullscreen mode

When you take a look at the sum_two_numbers function, there is the keyword return. The return keyword is used to output the results back to the caller. It is also used as the exit point of a function.

Lambda functions

Lambda functions are small and anonymous(unlike the normal functions, they're defined without a name) functions, they take any number of arguments and return a result, however the can only have one expression.

A simple example of a lambda function that squares a value

x = 4
# print a the square of x
square_fun = (lambda x:x **2)(x)
print(square_fun)
Enter fullscreen mode Exit fullscreen mode

Output:

16
Enter fullscreen mode Exit fullscreen mode

Docstrings in functions

A docstring is a string literal that is used in a function, class or when defining a class method. They give a brief description about the class or method, or a function in this case. It is not necessary to have a docstring but it is recommended.

The docstring of a function can be accessed using the __doc__ attribute.

Example of a docstring in functions:

def mul_number(a, b):
    """ multiply  to values, a and b"""
    return a*b
Enter fullscreen mode Exit fullscreen mode

Getting the docstring of a function:

print(mul_number.__doc__)
Enter fullscreen mode Exit fullscreen mode

Output:

multiply  to values, a and b
Enter fullscreen mode Exit fullscreen mode

It is important to note that with functions or blocks of code in python, indentation is very crucial, it is easy to break code or have semantic errors, with unintended indentation.

This is a simple tutorial to give a brief introduction to functions from which you can lay a basis of understanding functions in python.

There is more to functions, being an explorer is one way to find out!

Top comments (1)

Collapse
 
citronbrick profile image
CitronBrick • Edited

Could have added a section about keyword arguments & default argument values.
Not all languages have keyword arguments.