Function of a Python Program

Before we get to learn how to code, let’s understand the foundational unit of a computer program.

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 atomEven though subatomic particles exist, they don’t act as a functional unit for matter.. The basic unit of living beings is a cellEven though subcellular elements exist, they don’t act as a functional unit for life.. As far as this course is concerned, the foundational unit of a computer program is known as a function. As its name implies, each function is designed to execute a specific and crucial task, providing a modular and organized approach to constructing and maintaining code. As we delve into this course, understanding the significance of functions will be key to mastering the art of procedural programming in Python.

A function has three components: input, processing, and output.

Function as a black box
Function as a black box
  1. Input: The data that the function has to work on

  2. Processing: The steps and operations necessary to achieve the function’s objectives

  3. Output: The result or the information that the function computes after processing the input

Using functions

Python provides its users with built-in functions These are functions that come packaged with Python. that carry out some useful tasks. The processing part of built-in functions is hidden from us, as Python takes care of that on our behalf. We are only concerned with what we feed them as input and what they give us as the output. Let’s take Python’s built-in function 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 inputTry 3.14159265 or any number with a decimal point in it. and observe the output it produces when you press the round() button.

The round() function

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., 102=10010^2=100.

The pow() function

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. Press the “Run” button.

round(3.14159)

Congratulations, you just executed your first Python code successfully!

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 computer work for usComputers are super fast, but we need to learn some (or a) programming language to communicate what we want them to do for us. Over here, we wanted the computer to work out what 2 raised to the power 3 amounts to, so we communicated with it using Python.. But how do we verify if they are doing what we expected them to do? Is there a way for us to see their output?