Functions In Python

Python is an excellent language to learn, and we’ve discussed in long series how to code with this language. As a complete beginner in this article, we will learn using the functions in python, which is used a lot when coding along with programs and when working with object-oriented programming.

1. What Is a Function?

The function in python is a block of code designed to perform something such as calculation, logic, and any task you would like to perform inside your python code.

The idea of using the functions is that the developer sometimes needs to repeatedly use a block of code and multiple times inside his python code so that the function will make this easy. Let’s see how to write a simple function:

def fun(): #Simple function
    print("Python Developer")

You can see that writing a function in python is simple. The first thing is using the keyword def and then specifying the function name, which is fun, and you can choose whatever name you want. 

After that, you need to write close brackets, which will be used later to add arguments and expand the functionalities of this function with double points at the end. 

Inside the function, you need to place the code you want this function to execute, which is the print statement. But when running the code, you won’t see anything happen in your IDE, and this is an example of creating a function, but we didn’t use it yet. Let’s use it in the following example:

def fun(): #Simple function
    print("Python Developer")
fun() #Calling the function


Python Developer

After you execute the previous code, the function will run the code inside it and print “Python Developer” on your IDE screen. Using a function, as you can see, is simple. Just call the name of this function followed by its closed brackets.

2. Function Arguments

The python functions must have a closed bracket at the end of its name, and it can be used for adding arguments to extend the function functionalities. The developer defines these arguments. Let us see an example:

def greeting(name, msg):
    print("Hello", name, msg)
greeting("Python Developer", "Good Morning!")


Hello Python Developer Good Morning!

You can see that we’ve used two arguments inside the greeting function, which are the name & msg arguments, and we’ve used them inside the print() function.

When you call the function, you must add these variables as you see that we’ve specified the name as “Python Developer” and the msg argument as “Good Morning!”.

If you specify two arguments in the function, but you have provided one variable or more than two inside the function when you call it, you will have an error by your python interpreter:

TypeError: greeting() missing 1 required positional argument: 'msg'
TypeError: greeting() takes 2 positional arguments but 3 were given

Let’s see another example using the function and the if statement to verify a condition:

def verify(x):
    if (x % 2 == 0):
        print("Even Number")
        print("Odd Number")


Even Number
Odd Number

The previous code will accept a number, the x variable inside the closed bracket, and perform a calculation using this %, which is used to calculate the remainder of a division sum. If the remainder is 0 after dividing it by 2, this number is an even number. Otherwise, this is an odd number.

3. Default Arguments

As you’ve seen from the previous examples, we need to specify the variables later when we call the function, but did you know that we can define a default argument if the user didn’t set that variable so you won’t get an error? Let me show you an example:

def greeting(name, msg="Good Morning!"):
    print("Hello", name, msg)
greeting("Python Developer")
greeting("Python Developer", "How are you?")


Hello Python Developer Good Morning!
Hello Python Developer How are you?

We’ve specified a default message in the argument msg when we created the function. Later, it will use that default message when we call it without setting the msg variable. 

When you choose your message, it will use it instead. Since the argument name has no default value, we need to specify it every time we call the functions or get an error.

4. Keyword Arguments

When you define arguments inside the functions, you must follow the same order when you specify the values when calling it. Let me show you an example to understand:

def greeting(msg="Good Morning!", name):
    print("Hello", name, msg)
greeting("Python Developer", "How are you?")


SyntaxError: non-default argument follows default argument

You will get an error since you didn’t follow the sequence of the arguments. You can fix that by mentioning the arguments in the function when you call it and assigning the value. See this example:

def greeting(name, msg="Good Morning!"):
    print("Hello", name, msg)
greeting(msg="How are you?", name="Python Developer")


Hello Python Developer How are you?

As you see that it worked without any error, you need to specify the value of every argument, and then the sequence of these arguments doesn’t matter.

5. Arbitrary Arguments

You understood from the previous examples that the number of arguments defined in the function when you create it need to be the same number of values when you call it. Let’s suppose that we don’t know how many values we need once we call the function. Python has a solution in this situation:

def greeting(*names):
    for name in names:
        print("Hello", name, "Developer")
greeting("Python", "Java", "JavaScript")


Hello Python Developer
Hello Java Developer
Hello JavaScript Developer

You can see that we’ve used the asterisk “*” sign behind the argument name to tell python that we don’t know how many values we need for that argument, and everything worked well without raising an error.


Thanks for reading! Functions in python are used a lot, and you need to understand them well to be a python developer. Also, we will require them later when learning object-oriented programming using python.

Scroll to Top