6 min to read
Python - Writing functions - basic
This article is a part of the Python - 101 series, you can access the full version of the series here:
-
Coding 101
-
Writing functions in Python
- Basic (you are here!)
- Scope of variables
- More on arguments and lambda functions
- Best practices
-
Control flows in Python
-
Errors and exceptions
-
Classes
Welcome to the third article of the Python - Coding 101 series, after reading this article, you will learn:
- What is a function and why do we need our own functions
- How to declare a customized function in Python
- Different ways to call a function in Python
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?
- 2. Types of function?
- 3. Why we need user-defined function (UDF)
- 4. Function declaration
- 5. Function calling
- 6. Wrap up and exercises
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).
- Function is block of code that is called by its name.
- The function can have different parameters or may not have any at all. If any data (parameters) are passed, they are passed explicitly.
- It may or may not return any data.
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:
-
Built-in functions
The Python interpreter has a number of functions and types built into it that are always available, which you can call at any time. They are listed here.
-
Functions imported from external library
For this type of functions, you need to explicitly import the library containing it. For example, if you want to create a NumPy array using the
array()
function, you need to import the NumPy library first. It can be shown in the below examplea=[21,12,98] np.array(a) # this will raise an error, because python does not know what array() is import numpy as np np.array(a) # this will succeed.
-
User-defined functions
What if the function which give your desired result cannot be found in both the two types above? In such case you will need to refer to user-defined function, which you will need to write your own function.
In other words, functions which are created by the user or programmer are known as user-defined functions.
In this article, we will focus on this type, about why we need it, and how to write it.
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:
- name: print_this
- parameters: first_element, second_element
- output: return result
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
- Function calling with keyword arguments
- Function calling with positional arguments
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:
- Write a function that take a list and return the reversed version of it
- Modified the
print_this()
function above to directly print result to your screen - Write a function that take a list and return the appearing frequency of each element. Tips: the result should be in the form of a dictionary. Can you guess why? For example:
['abc', 'abc', 'bcd']
-> output:{'abc':2, 'bcd':1}
- Write a function that return True if a number if a prime number, False otherwise. **
Comments