Functions in Python

Introduction

A function is a block of code that when called, performs a task it was written for. It is like a mini program within a program.

In Python there are built in functions but one can also create functions for one’s own use. Functions are often created when a group of code is to be executed multiple times.

Components of a function

  • The def keyword to define a function
  • The name of the function and parentheses e.g. def my_func()
  • The parameters, which are variables that store arguments e.g. def my_func(name)
  • The arguments, which are the values of the parameters
  • Return keyword to return a value

Types of Functions in Python

Functions with no argument and no return value.

def my_func():
print(‘hello’)

In the above code, there is no return keyword so there is no return value. The function only prints out hello. hello

Functions with no argument but with a return value

def my_func():
    return “Hi”
my_func()

Here, a return keyword is present andHi will be returned to the console.

Functions with arguments (args) and no return value

def addition(a, b):
        sum = a + b
        print(“the sum of a and b is ”, sum)

addition(5, 6)

In the above function, the parameters, a and b, are assigned arguments, 5 and 6, when the my_func function is called. The purpose of the function is to add the arguments and print out the output.

Functions with Arguments (args) and return value

def addition(a, b):
        sum = a + b
        return sum

addition(5, 6)

In the above code, when the function is called with the arguments passed in, the sum of the two numbers is returned. The sum of the numbers is the return value.

Functions with Arbitrary Arguments (*args)

def my_func(*kids):
        print(“The last kid is ”, kids[-1])

my_func(“John”, ”Jane”, ”Julia”, ”Jude”)

In the code above, the output is: The last kid is Jude

Arbitrary argument -which is an asterisk before the parameter- is used when the number of arguments that will be passed at the call of the function is uncertain

Functions with keyword arguments (kargs)

def myfunc(kid1, kid2, kid3):
        print(“My kid is ”, kid3)

myfunc(kid1 = ”Sharon”, kid2 = “Stephanie”, kid3 = “Hilary”)

In the above code, the arguments are passed with a key = value syntax. The output of the function will be:

My kid is Hilary

Functions with Arbitrary Keyword Arguments (**kargs)

def myfunc(**name):
        print(“My last name is ”, lname)
myfunc(fname = “John”, lname = “Smith”)

An arbitrary keyword –which is double asterisks before the name parameter- is used when the number of keywords to be passed when the function will be called is unknown.

When to use a Function

As I stated earlier, functions are used when code is to be reused. Instead of writing the same lines of code each time you want the program to perform a task, write the lines of code as a function and call the function.

Thank you for reading through. I hope this post was helpful. You can give me feedback so I can know how I am doing with these posts.