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 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.
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:
def function_name(parameters): statement(s) return expression
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.
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
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:
employee_name = 'Abder' def print_name(name): print name print_name(employee_name)
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:
Traceback (most recent call last): File "test.py", line 3, in <module> print_name(employee_name) NameError: name 'print_name' is not defined
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:
numbers_list = [1,2,3,4,5] def insert_numbers(numbers_list): numbers_list.insert(5, 8) numbers_list.insert(6, 13) print 'List \"inside\" the function is: ', numbers_list return insert_numbers(numbers_list) print 'List \"outside\" the function is: ', numbers_list
Notice that the output of this Python script will be:
List "inside" the function is: [1, 2, 3, 4, 5, 8, 13] List "outside" the function is: [1, 2, 3, 4, 5, 8, 13]
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.
def add(x,y): return x+y def subtract(x,y): return x-y def multiply(x,y): return x*y def divide(x,y): return x/y x = 8 y = 4 print '%d + %d = %d' % (x, y, add(x, y)) print '%d - %d = %d' % (x, y, subtract(x, y)) print '%d * %d = %d' % (x, y, multiply(x, y)) print '%d / %d = %d' % (x, y, divide(x, y))
Go ahead, try it out and see what output you get.
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:
def double(x): return x*2 x = 10 print double(x)
The Lambda way of writing this function is as follows:
x = 10 double = lambda x: x*2 print double(x)
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.