Python Functions 2

Python Functions – Arguments and Parameters

On Python Functions 2 page, more built-in functions and User Defined Functions are introduced. We discuss Arguments and Parameters and their use in functions.

Python Functions are what other programming languages refer to as procedures. As mentioned in Python Functions 1 chapter, functions are ready-to-use blocks of code that perform a certain function. Python distribution comes with many built-in functions. You have already come across a few built-in functions like print(), input(), range(), and len() in Python Functions 1 chapter.

We will discuss a lot more Python Built-in functions on this page.

Why Use Functions

The advantage of the functions is that you do not have to write the block of code that is already available as a ready-to-use block of code in the function.  For example, the function print() embeds a lot of lines of code that you did not have to write to view the result of your program on the computer screen. Functions make the program modular, easy to understand and reduce typing.

Python Functions – User Defined Functions (UDF)

In most programs, you need to perform the same calculations on a different set of data at several different places in the program. The Python built-in functions can be used to achieve this task.

However, if your program has a specific need for functionality that does not exist in the Python built-in functions list, you could write this computation block of code separately and give it a unique name.

Then, your main program can call this function by its unique name. Let us name this My_func.

When My_func is called by your main program, My_func will perform the computation and return the result of the computation to your main program.

These functions that you define yourself are called User Defined Functions (UDF).

Once you have written a User-Defined Function and saved it, the main program then just writes one line in the program specifying the unique name of the User-Defined Function. The User Defined Function will do the computations and return the result to the main program (the calling program). This operation is referred to as calling the function.

The above approach is what is known as the write-once-use-many-times approach. It reduces the amount of code typing you have to do. It makes your program smaller, modular, and easier to read and understand.

How does the Function Get The Data To Do The Computations ON

The calling program would like the computation to be done on a different set of data each time the function is called. Where does the called function get this set of data on which the computation needs to be done?

When your main program (calling program) calls a function, it provides the data on which your main program desires the computation to be done by the function.

The main program provides this data by enclosing it in the parenthesis of the function call, as in the statement below.

My_func(data1, data2,…

The data1, data2,… are called arguments.

The calling program may pass zero, one, two, or more arguments to the function when it calls the function. The function uses these arguments in the execution of the function’s code. The function may return none, one, or more values back to the calling program.

Thus, a function is a block of code that performs the computations on the arguments passed by the calling program to the function. The function returns the results of the computation to the calling program.

More About Arguments and Parameters in Functions

Recapping, an argument is a value that is passed to the function when it is called by the calling program (the main program).  On the function side, it is called a parameter.

Multiple arguments may be passed by the calling program to the function. When the main program calls the function, it passes values enclosed in the parentheses, referred to as argument(s) or parameter(s) of the function call. The argument(s) supply the actual data to be used by the function to do the computation.

So how does the function associate the parameters in the function definition to the arguments passed by the main program while calling the function?

The order of parameters in the function definition is the same as the order of the arguments passed by the main program to the function. The function reads the argument values and associates them with the parameters in the function definition.

This process is called passing the arguments to the function.

In summary, an argument is a value passed to a function (or method) by the main program when calling the function. We will introduce methods in the Python Modules chapter a little later.

Example of How Argument Are Used

Let us explain functions by using an example of tasks you do every day after school.

  1. Play a sport
  2. Have dinner
  3. Do home work

We will define a function and name it after_school() as follows:

>>> after_school(sport, dinner, home_work)

“after_school” is the name of the function. The items enclosed in the parenthesis, “sport“, “dinner“, and “home_work” are the parameters.

How will the main program call the function “after_school”?

The following paragraphs explain this calling process.

Perhaps, you play a different sport each day of the week, have a different dinner menu item each day of the week, and do a different home work for Monday to Friday.

The calling program would call this function “after_school” for each day of the week, providing the arguments in parenthesis separated by commas as below:

If on Monday after school, you play soccer, have pizza for dinner, and do math homework, the calling program will have the following statement.

>>> after_school(soccer, pizza, math)

If on Tuesday, you play basketball, have mac&cheese for dinner, and do history homework, the calling program will have the following statement.

>>> after_school(basketball, mac&cheese, History)

For Wednesday, the calling program will call the function with the statement below:

>>> after_school(tennis, lasagna, Science)

And so on.

So, for whichever day your main program wants to call the function, you change the arguments “sport”, “dinner”, and “homework” for that day of the week.

Function Structure

Functions have three parts:

  1. Name of the function
  2. Body of the code (computation the function performs)
  3. Parameters (the names on which you want the calculations to be performed by the function)

Python supports two types of functions

  1. User-defined functions. You can define your own function by writing a block of code.
  2. Built-in functions. Several functions in Python are available for use by the programmer.  The main program can call the needed function by name. The function will do the computations and will return the computed value to the calling program. Refer to the Appendix section for the list of all Python built-in functions available in Python.

Python.org documentation list all built-in functions at the link here.

Verified by MonsterInsights