In this chapter, you will learn how to create and make use of functions and modules. You will also
learn how to iterate commands that you have issued in your script in order to repeat certain
actions for different elements, and handle errors that you may encounter in your script.
Functions
In Python, a function allows you to create a block of code that will be able to do an action. They
are also reusable, which means that you can provide a name to that statement block and then run
this block using the name that you assigned it anywhere in the program that you are building
without any limit. In Python terms, this is called “calling the function”.
Functions are probably the most important component of a programming language. In Python, they
are usually defined using the keyword def, followed by an identifier name for the function that you
want to use. Take a look at this example:
Save this as function1.py, and then run it at the interpreter prompt. You should see this output after
doing so:
What happened here is that you are able to define the function say hello, which has no
parameters, which is the reason why there is no value stated inside the parentheses. Parameters
are indicated in functions in order to include an input that you can use to pass different values to
the function and get a specific result that you have in mind.
Also notice that you have managed to call the function two times in this exercise, which means
that you did not have to write the entire code again for Python to repeat a particular action.
Function Parameters
Functions are able to take in values that they will be able to use, which are called parameters.
Parameters act similarly to variables, except that you are defining their values whenever you call
the function and that you have already assigned values to them once you run the function.
Parameters are specified within a pair of parentheses when you are defining the function and are
separated using commas. If you need to call the function in your code, you will need to
supply the values in the same way. Also take note that when you are supplying value to your
function while you are naming it, these values are called parameters; but when you are supplying
values as you call the function, these values are called arguments. Take a look at this example:
Save this as function_param.py and then run it at the interpreter prompt. You should get this
output:
Keyword Arguments
There will be instances as you code wherein you have too many parameters in your function – if
you want to specify some of them, then you can use keyword arguments in order to give values for
some of the parameters. Doing so will give you the advantage of easily using the function without
having to worry about the arguments’ order, and that you can assign values to the parameters that
you want to use, especially when the other parameters that are available already contains
argument values that are set in default.
Take a look at this sample code:
Save this code as function_keyword.py, and then run it at the interpreter prompt. You should get
the following output:
The return Statement
If you want to break out of the function, or if you want to return a value from the function, then this
statement will prove to be helpful. Take a look at this example:
Save this code as function_return.py and then run it at the interpreter prompt. You should get the
following output:
DocStrings
Python comes with a cool feature called docstrings, which is a tool that you can use to document
the code that you are creating and make it easier to understand. You can also get a docstring from
a function while the code is already running. Take a look at this example:
Save this code as function_docstring.py and then run it on the interpreter prompt. You should get
the following output:
What happened here is that you are able to view the docstring for the function that you have used,
which is the first string on the initial logical line. Take note that docstrings can also be used in classes and modules.
0 Comments