Function of a Python Program
Before we get to learn how to code, let’s understand the foundational unit of a computer program.
We'll cover the following...
What is a computer program made of?
There is a basic structural and foundational unit of everything that exists. The basic unit of matter is an
A function has three components: input, processing, and output.
Input: The data that the function has to work on
Processing: The steps and operations necessary to achieve the function’s objectives
Output: The result or the information that the function computes after processing the input
Using functions
Python provides its users with round
as an example. It takes in a fractional value (e.g. 3.1415) and rounds it off to the nearest whole number. The widget below helps us visualize how this function works. Provide it with an round()
button.
The round()
function is the type of function that must take a single input, but some functions may require multiple inputs or no inputs at all. An example of a function that takes two inputs is the pow()
function that calculates the power of a number. The two inputs are the base and the exponent, and it returns the result of raising the base to the power of the exponent, e.g.,
Let’s get our hands dirty with Python
To use any function in Python, we must make a call to it. This can be done by writing the name of the function, followed by a set of parentheses ()
. Inside the parentheses, we can provide the inputs to the function; this much has been done for you. Press the “Run” button to execute this function, and the widget should show us a message of success.
round(3.14159)
Congratulations, you just executed your first Python code successfully!
But it did not show you any output. There is a reason for that, which we will know later.
To give multiple inputs to a function in Python, the values must be separated by commas. The code below shows how it’s done for pow()
.
pow(2, 3)
The order in which we provide the inputs is important. In this example, the first input is automatically considered as the base, while the second input is considered as the exponent.
What’s next?
We pressed the “Run” button, and it executed our code successfully in both cases, but it did not show us any output that we were expecting to see. Since no error was encountered, we managed to make the