Functions in Python

In the context of programming, a function is a named sequence of statements that performs a computation. When you define a function, you specify the name and the sequence of statements. It is common to say that a function “takes” an argument and “returns” a result. The result is called the return value.

It may not be clear why it is worth the trouble to divide a program into functions. There are several reasons:

  • Creating a new function gives you an opportunity to name a group of statements, which makes your program easier to read, understand and debug.
  • Functions can make a program smaller by eliminating repetitive code. Later, if you make a change, you only have to make it in one place.
  • Dividing a long program into functions allows you to debug the parts one at a time and then assemble them into a working whole.
  • Well-designed functions are often useful for many programs. Once you write and debug one, you can reuse it.

Before writing a function, you first have to think that what your function will be doing, what arguments it will take and what will be the returning value. It is a good practice to properly comment your function and have indicative function name that specify the purpose of the function. For example, the below function calculate the area of a triangle:

area

We define the function using a def  keyword, triangle_area is the name of the function and it takes two arguments: base and height. This is called function header and it always ends in colon. The indented part is called the function body and that contains the sequence of statements that the function performs.

The next step after writing a function is to test the function by calling the function to see that it produces the right output as shown in the above example.

In order to ensure that a function is defined before its first use, you have to know the order in which statements are executed, which is called the flow of execution. Execution always begins at the first statement of the program. Statements are executed one at a time, in order from top to bottom. Function definitions do not alter the flow of execution of the program, but remember that statements inside the function are not executed until the function is called.

Python provides a number of important built-in functions that we can use without needing to provide the function definition. The creators of Python wrote a set of functions to solve common problems and included them in Python for us to use. For example, Python has a math module that provides most of the familiar mathematical functions.

Before we can use any  module, we have to import it. In order to use the mathematical functions present in python math module we need to import the math module and it can be done using the below line on the top of the python script.

>>> import math

The module object contains the functions and variables defined in the module. To access one of the functions, you have to specify the name of the module and the name of the function, separated by a dot (also known as a period). This format is called dot notation.

>>> ratio = signal_power / noise_power
>>> decibels = 10 * math.log10(ratio)

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.