One undeniable fact in Python is that after a coding program, there must be a need to execute the code blocks in order to achieve its purpose. Picture this. A bus is fully loaded and ready for take-off, however, it’ll only start a trip when the driver enters and drives off. Similarly, the ready bus is compared to a code block that is ready and waiting to be executed. Which we’ll delve into throughout this article.
By the end of this page, you’ll vividly understand the following:
- What is Python Function?
- Functions syntax explanation
- Python function Docstrings
- The function return statement
- Built-in functions
- User-defined Functions
- Advantages of user-defined Functions
- Operations on python function
Afterward, you’ll be to put the knowledge you’ve acquired to work
What are Python Functions?
Python Functions are a set of organized and reusable blocks of codes that are used to perform a defined task in Python. There’s a need for program organization as it grows larger, and the python function does just that. Also, it breaks down complex codes into understandable pieces. Functions are sure to deliver when it concerns information hiding, in conclusion, it checks code duplication.
Function Syntax: Explanation
The python function syntax is recognized by the code below, with components explanation below syntax.
Function syntax:
def function_name(parameters): """docstring""" statement(s)
From the code above, the components are explained below
- The def keyword is the begging of the Functions syntax
- Function-underscore-name is to identify the function to be carried out
- Parameters or argument is used to give value to the Functions, however, these are optional
- Colon(:) marks the end of the Function header
- The docstring(documentation strings) is used to describe the job of the Function
- The statement contains at least one valid statement. Keep in mind that statement must have the same indentation level which is usually 4 spaces
- One more is a return Keyword that returns a value from a function
Before we delve into putting the Function into practice, let’s see a general example of the python function
Example:
def greet(name):
“””
This function greets to
the person passed in as
a parameter
“””
print(“Hello, ” + name + “. Good morning!”)
The above example is how a Function is entered, however, running the code will raise an error because the (name) is not defined and we haven’t called out the Function. Let’s see how to call out the Function without raising an error
Python Input
def greet(name): """ This function greets to the person passed in as a parameter """ print("Hello, " + name + ". Good morning!") greet('Simon')
Output: Hello, Simon. Good morning!
Now the (name) is defined at [greet(‘Simon’)] and that gives us an error-free output.
That is cleared, now we progress to the next component of the Python Function
Python Function Docstrings
Python function Docstrings is the first String after the colon(:) of the Function header. It is used to describe the job of the Function. Docstrings is short for document strings. Though the docstring is optional, I strongly advise you to always document your code programming because you might forget vital reasons. A docstring is enclosed in triple Quotes because it spans multiple lines
From our code above, our Docstring is in the green: “””Docstrings”””
However, they are optional. See the example of the same code program without docstrings in it
Python Input
def greet(name): print("Hello, " + name + ". Good morning!") greet('Simon')
Output: Hello, Simon. Good morning!
The results are the same, it agrees that the Docstrings are optional. However, it is very important.
The Function Return Statement
The function return statement is used to exit from a Function to the point of computation. The return statement is expressed by the syntax: return [expression_list].
Example
Python Input
def absolute_value(num): """This function returns the absolute value of the entered number""" if num >= 0: return num else: return -num print(absolute_value(4)) print(absolute_value(-8))
Output: 4 8
With a God wrap up on python’s Function components, it’s of essence you know the major types of Functions in Python
Types of Python Functions
There are two major types of Functions in Python, they are: built-in functions and User-defined Functions (UDFs)
Python Built-in Function
The python built-in functions are the predefined Functions in Python. You’re not defining it to enable it to run. The table below shows some important built-in functions in Python.
Function Name | Description |
Len() | It states the length of a value or object. |
list() | It outputs a list. |
max() | It states the maximum value from a sequence. |
min() | It states the minimum value from a sequence. |
open() | It opens a file. |
print() | It prints statements. |
str() | It returns a string object or value. |
sum() | It sums up the values inside a sequence. |
type() | It returns the type of object. |
tuple() | It returns a tuple. |
See more built-in Functions here
An example of built-in function codes below
Python Input
x = [1, 2, 3, 4, 5, 6, 7] print(len(x)) print(type(x))
Output: 7 <class 'list'>
User-defined Functions
The user-defined Functions are the functions designed by the programmer to achieve his coding purpose. It helps in reducing the complexity of big problems. The def keyword is used to call out the user-defined function. The syntax for User-defined Function is def function_name():
statements
See an example below:
Python Input
# Example to illustrate # the user-defined functions def add_numbers(x,y): sum = x + y return sum num1 = 105 num2 = 6.78 print("The sum is", add_numbers(num1, num2))
Output: The sum is 111.78
The add_numbers() function is the user-defined project. It’s important to keep in mind that the user-defined function is best for larger projects
Advantages of the User Function
The User-defined Functions have their cons which are stated below:
- User-defined Functions helps to breakdown bulky program which makes them easy to easy, debugging, maintenance and easy to understand
- Division of labor among programmers as different functions can be shared with different programmers at once
- It is used to execute and call out execute codes Functions when needed
Summary
Python Functions are a set of organized and reusable blocks of codes that are used to perform a defined task in Python.
Python function Docstrings is the first String after the colon(:) of the Function header. It is used to describe the job of the Function. Docstrings is short for document strings.
The function return statement is used to exit from a Function to the point of computation. The return statement is expressed by the syntax: return [expression_list].
The python built-in functions are the predefined Functions in Python. You’re not to define it, to enable it to run.
The user-defined Functions are the functions designed by the programmer to achieve his coding purpose
The User-defined Functions have their cons which are stated below:
- User-defined Functions helps to breakdown bulky program which makes them easy to easy, debugging, maintenance and easy to understand
- Division of labor among programmers as different functions can be shared with different programmers at once
- It is used to execute and call out execute codes Functions when needed
On this page you’ve learned:
- What is Python Function?
- Functions syntax explanation
- Python function Docstrings
- The function return statement
- Built-in functions
- User-defined Functions
- Advantages of user-defined Functions
- Operations on python function
It’s at your disposal to put into practice what you’ve read for a real result. For a head start, see the Python Tutorials. Good luck Coding!