A Smooth Refresher on Python's Functions

In Python, you may have come across things like file(), print(), open(), range(), etc. Those are called built-in functions. That is, functions already provided by the language itself which you can execute by referencing (calling) to them. But, what is a function anyway? This is what we are going to learn in this tutorial, the Python way!

Functions

Functions are composed of a set of instructions combined together to get some result (achieve some task), and are executed by calling them, namely by a function call. Results in Python can either be the output of some computation in the function or None. Those functions can be either built-in functions (mentioned above) or user-defined functions. Functions defined within classes are called methods.

Defining Functions

Now that we know what is meant by a function, let's see how we can define functions in Python. In order to do that, we use the def statement, which has the following syntax: 

The parameters in the function definition are optional, as some functions do not require parameters to be passed at the time of the function call. If more than one parameter is passed, the parameters are separated by commas, and they are bound to the arguments in the function that correspond to the parameters passed. The statements (function body) are executed when the function is called.

The return statement is an optional statement which serves as the exit point of the function where an expression could be returned to the caller, or if no expression is identified, it will be like returning a None value.

Examples

Let's go through some examples to grasp the idea of functions more. You will notice that functions save us from repeating ourselves, as they provide a block of reusable code to call whenever we want to perform some regular task we ought to perform.

Say that we would like to display the name of any employee entered in the system. This can look as follows:

As you can see, if we want to call a function, we simply identify the following:

  • function name (i.e. print_name)
  • parameters (i.e. employee_name)

If you type print_name(employee_name) before the function definition, Python will complain as follows:

So you should define the function before calling it.

Let's take another example. This time we will use lists. Assume we have the following list:

numbers_list = [1,2,3,4,5]

Let's say now we want to insert new numbers using this function:

Notice that the output of this Python script will be:

What can we conclude from this? We can conclude that the parameters are passed by reference. That is, the parameters in the called function are the same as the passed arguments (variable/identity) by the caller, as opposed to passed by value where the called function parameters are a copy of the caller passed arguments.

A Simple Calculator

Let's use our knowledge of functions to slightly build a more interesting application. Let's build a simple calculator. This calculator will allow the user to enter two numbers, and perform addition, subtraction, multiplication, and division on the two numbers.

Go ahead, try it out and see what output you get.

Lambda Functions

Lambda functions are anonymous functions Python creates at runtime using the lambda construct. So far, we have learned that functions are defined using the def statement. Lambda functions are, however, defined in a different manner. Let's take an example to clarify how Lambda functions work.

Say we want to write a function that returns the double value of the passed argument. Using the def statement, we would do the following:

The Lambda way of writing this function is as follows:

There are different ways you can use Lambda in. Check this tutorial for more information on Lambda functions.

As we can see, functions are considered an important feature in Python, allowing you to reuse code rather than reinventing the wheel.

Tags:

Comments

Related Articles