DEV Community

Cover image for Python Functions: A Beginner’s Guide - Part 1.
Ashutosh Vaidya
Ashutosh Vaidya

Posted on • Updated on • Originally published at Medium

Python Functions: A Beginner’s Guide - Part 1.

Introduction

As a programmer, I have always found functions very useful. They are a powerful tool that can improve both the clarity and expressiveness of the code. By the end of this article, you will get to know what is a function, how to write a function in the Python programming language, and get a brief overview of functions.

What is a Function?

A function is a block of organized and related statements that can take inputs and perform a specific task. A function allows us to use code repeatedly without worrying about duplication and maintenance if any changes are required in code. Functions are also called methods, procedures, subprograms or routines, etc.

There are two types of functions in Python: built-in and user-defined.

  • Built-in functions are provided by python and are immediately available. For example, print(), len() to name a few.

  • User-defined functions are created by programmers and are the main focus of this article.

Creating the first function

In Python, a function is defined using the def keyword. See the below example for reference,

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

Here, the first line is a def statement, which defines a function named hello_world(), and the second line onwards is a body of a function. The code will be executed when the function is called not when it's defined which we have done right now.

Calling the function

The function is called as shown below and it will print Hello World

    hello_world()

    #Output:
    #Hello World
Enter fullscreen mode Exit fullscreen mode

A function with a parameter and a return value

Now that we understand how to create a basic function, let’s create one function which takes the user’s name as input and greets the user.

    def greetings(name):
        print(f"Hello, {name}")

    greetings("Jon")

    #Output:
    #Hello, Jon
Enter fullscreen mode Exit fullscreen mode

Here we have passed the user name as a parameter to the function and the function print’s the greeting. What will happen if we use the function calling enclosed in the print statement, let's try it out.

    print(greetings("Jon"))

    #Output:
    #Hello, Jon
    #None
Enter fullscreen mode Exit fullscreen mode

What just happened? we got two outputs, the first Hello, Jon which was expected and the second line is None, where did it come from?

Well, the function can return the value to a Caller, in our example, we have not returned anything from the function hence it is returning the special type None. If we want to return something from the function then we will need to use the return statement.

Let’s modify our function to return from it.

    def greetings(name):
        print(f"Hello, {name}")
        return "It's nice to meet you!"

    print(greetings("Jon"))

    #Output:
    #Hello, Jon
    #It's nice to meet you!
Enter fullscreen mode Exit fullscreen mode

In the above example, we have returned a string type of value. A function can return any python object. It can return integer, float, list, set, dictionary, etc. If there is no value to return then as we observed earlier function will return None

Terminologies

You must be thinking Define the function, call the function, pass the value, and return the value, so many things to remember. It can feel like a daunting task at first. So let’s explore them using a simple example by writing a function that accepts a number and returns its factorial.

Simple python program to calculate factorial

  1. To define a function means to create it. When the keyword def is used in the statement, it defines and names the function. In the above code example, line number 1 defines the function factorial().

  2. num enclosed in parenthesis in line 1, is a parameter.

  3. From Lines 2 onwards till line number 8 is a function body, here we write the logic we wanted to perform.

  4. Line numbers 3 and 8 are a return statement, which returns the value of the factorial calculated in the function to the caller.

  5. Line number 10 is a caller, who calls the function by passing a value 5 to parameter num. The value being passed is called an argument.

  6. And finally, the variable fact holds the value returned by the function.

It’s easy to mix up these things, but hopefully, this will make them easier to understand and remember.

Conclusion

In this article, we understand the basics of functions, how to define them, and what are different terminologies used. In the next part, we will explore different types of arguments (Keyword, Positional, default, etc.), learn about variable scope, and also understand Lambda functions. So don’t forget to follow and stay tuned for the notification.

Top comments (0)