Function In Python
1. DEFINING PYTHON’s FUNCTION
Let’s first write a very simple Python function
and then use it as an example to define the elements of a function
a. Keyword Definition
to define the function, we will use the keyword
def
followed by the name of the functiondisplay_message
if the function do need some information to do its job, this piece of information is provided within the parenthesis
( )
the function ends with a colon
:
sign
b. Body of the function
all indented lines follow the colon sign
:
represents the body of the functiontext within the triple commas is called docstring and it give us the opportunity to tell the reader of code, what the function does. It is essentially the comments, Python uses it to generate documentation for the functions in your programs
the
print()
statement runs any time we call the function
c. Calling a function
to execute the function, we just need to write the name of function (and provide any information, within parenthesis, if function needs it) This call essentially tells Python to execute the code in the function
1.1. Passing Information (arguments) to a Function
Let’s move a step further and write a function that accepts some information to work inside the function. This is called argument
a. Parameter
a parameter is a piece of information the function needs to do its job
the variable
title
in the definition of functionfavorite_book
is an example of parameter
b. Argument
an argument is a piece of information that is passed from a function call to a function
The value
harry_potter
infavorite_book(title)
is an example of an argument
2. PASSING ARGUMENTS
In the above example, the function definition only include one parameter and therefore one argument is supplied in the function call. However, function can have multiple parameters and arguments. You can pass arguments to your functions in a number of ways:
** positional arguments** — which need to be in the same order as the parameters;
keyword arguments — where each argument has a variable name and a value, that is, name-value pair;
and lists and dictionaries of values
2.1. Positional Arguments
When we call a function, Python match each argument in the function call with each parameter in the function definition. Therefore the values matched up this way (based on the order of the arguments ) are called positional arguments. Let’s write a function with positional arguments:
we defined the
function
namedmake_shirt
that requires two parameters —size
andmessage
we then write the body of function with a
print
statement using the given values of parameterslastly, we call a function by providing the arguments —
xl
andI code in Python
Important Points
Multiple function call: You can call the function as many times as you want with new set of arguments. This makes the function very powerful — write code once and use it several times
Order matters in positional arguments: Make sure to provide the arguments in same order as the parameters in function definition
2.2. Keyword Arguments
A keyword argument is essentially name-value pair that we pass to a function, where
name
is the parameter name . Therefore, as we explicitly provides the name of parameter in the argument, the order of arguments doesn’t matter. We will rewrite the above example using keyword arguments:
As you can see that when we call the function, we used the name-value pair, where name is the name of parameter in the function definition. In this case, the order of arguments doesn’t matter.
2.3. Default Values
When writing a function, we can define ** default value** for each parameter. In this case, if an argument for a parameter is provided in the function call, Python uses the argument value, otherwise, it uses the parameter’s default value (as provided in function definition) In the following example, we will copy the code from
make_shirt
function and will provide the default value for parametermessage
Note that the parameter with default value must be the last parameter(s) In this example, we will overwrite the default value of the parameter:
3. RETURN VALUES
A function doesn’t always have to display its output directly (like printing some message, as we have done so far). Instead, we can write a function that process some data and then return a value or set of values. The value the function returns is called a return value. This** return value** can be used anyway we want. NOTE: When you call a function that returns a value, you need to provide a variable where the return value can be stored.
3.1. Returning A Simple Value
Let’s write a simple function that returns the string:
when we call the function city_country
we stored its returned value in separate variable called country_1
and then print
that variable
3.2. Making an Argument Optional
We can use empty default values for a parameter to make an argument optional. As a rule, all (empty) default value parameter(s) are moved to the end of the parameters list
Let’s write a function which stores a dictionary containing artist name and album title. However, we also would like to store an optional parameter — i.e, no of tracks in the album. Here is how we deal with this situation:
we have used the parameter
tracks
in the function definition as an optional argumentin the body of function, we made a dictionary with required parameter values, then,
we used
if
statement to check whether the user provided optional argument,tracks
. If yes, we concatenated that value to the dictionarywe used
return
statement to output the dictionarywe call the function two times — one with
tracks
and one without itwe then print the variables
album_1
andalbum_2
that stored the function output value
3.3. Using a Function with a while Loop
We will first write the function as we have done in above example, and then get the argument values once
input
statements
the above example only takes a pair of response
both user
inputs
were mandatory and loop breaks after thatwe used the input values, stored inside the variables,
a_name
andalb_name
to call the functionlastly, we print the
album_info
4. PASSING A LIST IN FUNCTION
Python’s functions are very flexible to be used with different data types. In the following example, we will use as a list
parameter of a function:
In the above example, we passed a single list
. However, we are free to supply a series of list
as argument
4.1. Modifying a List in a Function
In following example, we are going to use two lists in one function with purpose of moving the list items from one list to another (i.e, modifying the list)
we used two lists,
ordered_items
andshipped_items
as parameters in the definition of the functionwe used
while
loop which keep running until the list,ordered_list
is emptywe
pop
each item fromordered_list
, store it in the variableordered_item
, print it and finally append it to the listshipped_items
4.2. Working with the Copy of a List
Let suppose, we want to keep the list ordered_items
unmodified. Here is the trick in python on how to stop the list to be modified — when call the function, use the following syntax:
The slice notation
[:]
makes a copy of the list.
5. PASSING AN ARBITRARY NUMBER OF ARGUMENTS
Sometimes function need to process the unknown number of parameters. To pass these arbitrary number of arguments, we need to add an asterisk
*
before a parameter name in the function definition. This asterisk*
in the parameter name, tells Python to make an empty tuple and load whatever values it receives into this tuple. NOTE: Python loads the arguments into a tuple, even if the function receives only one value
we wrote the function to print all the items in a sandwich
as we don’t know in advance that how many arguments does user supply, so we added an asterisk before a parameter name. In this way, our function can handle arbitrary number of arguments
5.1. Mixing Positional and Arbitrary Arguments
If we want a function to accept different kinds of arguments, the parameter that accepts an arbitrary number of arguments must be placed last in the function definition.
Python first matches the positional and keyword arguments and then process any remaining arguments in the final parameter. Let’s modify the
sandwich
function that takes thesize
of the sandwich as parameter butitems
parameter will be arbitrary:
5.2. Using Arbitrary Keyword Arguments
Until now, functions with arbitrary number of arguments, all of them are of one type (for example, sandwich items in the above example). However, if they are not of same type, we can use the name-value pair in the argument.
The double asterisks
**
before the parameter tells Python to create an empty dictionary and load whatever name-value pairs it receives (in arguments) into this dictionary
we wrote two functions, first,
build_profile
to build the profile (in form of dictionary) using arbitrary number of (keyword) arguments and another,print_profile
to print the key-value pair of the dictionary returned from the first functionwe first call the
build_profile
function and store its value (dictionary) inside a variableprofile_dexter
then we called the second function,
print_profile
by providing the variableprofile_dexter
as an argument
6. STORING YOUR FUNCTIONS IN MODULES
We can go a step further by storing our functions in a separate file called a module and then importing that module into our main program using
import
statement.
An
import
statement tells Python to make the code in a module(file) available in the currently running program file.
A module is a file ending in
.py
that contains the code we want to import into our program
6.1. Importing an Entire Module
a. General syntax to import:
The above line import module_name
tells Python to open the file module_name.py
and copy all the functions from it into this program.
b. General syntax to call the function:
Enter the name of the module we imported, module_name
, followed by the name of the function, function_name()
, separated by a dot .
EXAMPLE: Let suppose we store the function to build the profile in a separate file called build_profile_module.py
then import the module and call this function:
6.2. Importing Specific Functions
Rather than importing all the functions inside a file, we can import a specific function from a module.
a. General syntax to import
Importing a single function:
Importing series of function: We can import as many functions as we want from a module by separating each function’s name with a comma
b. General syntax to call the function:
As you can see, in this way, we don’t need to provide the name of module whenever we call a function
6.3. Importing All Functions in a Module
We can tell Python to import
every function in a module by using the asterisk *
operator. But you might be questioning that we can import all the function using the method described in 6.1, then we need this method? Using this (asterisk) way, we don’t need to use the module name anymore when calling the function
a. General syntax to import:
b. General syntax to call the function:
6.4. Give Function an Alias
A couple of reasons to use alias for a function name:
name of a function we are importing might conflict with an existing name in our program
function name is long, so we can use a short one
a. General syntax to import:
b. General syntax to call the function:
6.5. Give Module an Alias
In section 6.1, we used import module_name
and then we need to use the module name every time we call the function (Remember the syntax?) Therefore to avoid using the long module name, we can import module using alias.
a. General syntax to import:
b. General syntax to call the function:
Last updated
Was this helpful?