Python - Writing functions - basic

Featured image

This article is a part of the Python - 101 series, you can access the full version of the series here:

Welcome to the third article of the Python - Coding 101 series, after reading this article, you will learn:

In the last article, we have talked about basic coding, including data types and some basic functions and methods in Python. In this article, we will talk about Python functions, its definition and applicability. I also show you how to write your own function in the last part of this article.

1. What is a function?

So what the heck is Python functions, you may ask.

In case you missed it, we have already talk about the definition of function in the last article (Coding 101).

For example.

a = [21,12,98]
print(type(a))

In this example, type() is a function in Python. You find the data type of an object by calling type() on that object explicitly (a in this case). Such object (a) is a parameter of the function called. In this specific example, the function type() does return data, which is the type of the object.

2. Types of function?

There are 3 types of function in Python, including:

3. Why we need user-defined function (UDF)

Firstly, of course, it will give you your desired result.

Furthermore, using UDF will allow you to reuse the code in your function.

It also helps for dividing the program into a subprogram that makes it easier to debugging and testing. It is difficult to find an error in a huge program rather than a small program and function makes program small.

4. Function declaration

Function Declaration is a declaration statement that identifies a function with its name, a list of parameters and the type of data it returns. Function’s name and parameters can be set by any aliases.

In Python, you can declare a function by this syntax:

def my_function(<param1>, <param2>, <param3>, ...):
  <your_code_here>
  return <result>

For example:

def print_this(first_element, second_element):
  result = str(first_element) + ' ' + str(second_element)
  return result

The structure of the function is:

Now, as an exercise, can you write a function that take a list and return the reversed version of it. Comment below so that I can give it a view.

5. Function calling

Once the basic structure of a function is finalized, it can be executed by calling it.

When a function is invoked, the program control jumps to the called function to execute the statement that is a part of that function.

There are two types of function calling

Remember the function we declared above? We can call it by:

def print_this(first_element, second_element):
  result = str(first_element) + ' ' + str(second_element)
  return result

# positional
res1 = print_this('Hey ho', 'Satan')

# keyword
res2 = print_this(first_element='Hey ho', second_element='Satan')

print(res1)
print(res2)

In the above program, we called a function and stored its value into a variable. Then we print it to the screen by calling print().

What if you want your function to directly print the result to the screen, without calling print() explicitly?

6. Wrap up and exercises

Today we have talked about what function is, type of functions, how to make and call your own function.

And the exercise after learning about function are: