Using Functions

Functions are an important part of all programming languages.

Functions allow the statements required to perform a specific task to be grouped together and given a name.

Using the function name causes the group of statements with the corresponding name to be executed.

Function Use

There are 2 important pieces to the use of functions:

  1. Function call
  2. Function definition

Often the term function is used alone and from the context listener knows whether it is a function call or a function definition that is being referred to.

Function Definition

function definition - A function definition is a named group of statements. After a function is defined it is then possible to cause the group of statements to be executed (using a "function call").

We will look at how functions definitions are created in a later set of notes.

A function definition groups statements together to accomplish some task.

The function's name should describe the task the function accomplishes.

Function Call

Function call - A function call is a use of the function name that causes the statements of the function definition to be executed.

A function call consists of a function name followed by parentheses, which optionally contain a comma separated list of expressions.

A function call has the form:

function_name(optional_parameter_list)

Arguments, Parameters - Arguments, Parameters are values provided for the function to use in carrying out its task. These values are a comma-separated list placed in parentheses after the function name.

If no arguments are needed then there are no values in the parentheses.

Two Types of Functions

There are two types of functions:

  1. Free functions - A free functions is called simply by using the function name (and providing any required arguments).
  2. Methods - A method is similar to a free function but additionally it is called "on" an object using the dot (period) operator and may use or change the object (variable) it is called on.

Methods are part of object-oriented programming.

With object-oriented programming variables are more complicated types (than numbers) and are regarded as, and called, objects.

An object's (variable's) type determines the methods that may be called on it.

We will look at methods (starting with string methods) later.

Function Call Examples

Here are some examples to see what function calls look like.

More details on what is going on coming up.

num = -5
abs_num = abs(num)

target = input('Enter a name: ')

list.sort()

str = 'food'
str_caps = str.upper()

Again, a function call is recognized by a name followed by (...).

Result and Side Effect

Functions can have an effect in one of two ways:

  1. Return a result - A value is "sent back" that will be used in the place where the function is called.
  2. Cause a Side Effect - Change the given values (parameters) or other values that exist outside the function.

For a function that returns a result the result is:

  • Used in an expression (calculation)
  • Assigned to a variable

For a function that causes a side effect the call stands alone since the call will have caused a change somehow.

The print function (Python 3) is an example of a function that causes a side effect.

A function may have both a result and a side effect.

Result Example

Consider the following code:

# Modules and importing will be discussed in later notes

from math import sqrt
a = 3

b = 9
c = a + 2 * sqrt(b)  # What happens first here?

In the above code, on the statement with the function call, what happens first?

Operator precedence and associativity apply.

A function call has a higher operator precedence than any arithmetic-type operation.

The sqrt function returns a result that is used in the further evaluation of the expression.

Side Effect Example

A function with a side effect causes a change.

# Create a list, lists are covered later
data = [5, 1, 3, 8, 7, -4]
print(data)
# Call the function (method) named sort
data.sort()
# The list is now sorted
print(data)

The type Function

The type function:

Function Name:  type
Argument: A variable or literal (actual value like 5 of 'hi') is provided.
Result: An object is returned that will display as a string indicating the type of the argument.
Side Effect: None.

Example:

x = 5
t = type(x)
print(t)

The input Function

The input function has a result and a side effect:

Function Name: input
Argument: An optional string argument may be provided.
Result: A string representation of the value that the user types at the keyboard is returned.

Note: In order to use the value as a number, the value must be converted with a conversion function (below)


Side Effect: The value of the argument, if provided, is output as a prompt to the user.

Input example:

value = input('Enter a value: ')

Conversion Functions

The conversion functions have a result and no side effect:

Function Name: int, float, str, hex, bin
Argument: An argument is provided. The type of the argument depends on the conversion that is to be done. See below.
Result: A converted value is returned. The function name indicates the conversion that will happen. If the conversion is not possible there will be an error.
Side Effect: None.

Example:

# Input a value, integer returned (Python 2)
value = input('Enter a value: ')
# Convert the integer to a string
str_value = str(value)

Other Conversion Functions

Some conversion functions:

int(arg) - arg is a string comprised of digits, with an optional leading + or -. An integer is returned.
float(arg) - arg is a string comprised of digits, a possible decimal point, with an optional leading + or -. A real number is returned.
str(arg) - arg is a number. A string is returned.
hex(arg) - arg is an integer. A string is returned. The string is prefixed with 0x.
bin(arg) - arg is an integer. A string is returned. The string is prefixed with 0b.

The base conversion functions hex and bin are something you are unlikely to use but are mentioned here for those who might find them interesting.

The max Function

The max function:

Function Name: max
Arguments: Two or more comparable items.
Result: The maximum value is returned.
Side Effect: None.

Example:

a = 5
b = 1
c = 7
x = max(a, b) # 5 is returned
y = max(a, b, c) # 7 is returned

There is a min function also.

The range Function

The range function:

Function Name: range
Arguments: One, two, or three arguments may be provided.

  1. range(stop)
  2. range(start, stop)
  3. range(start, stop, step)

start - The first value in the sequence. The default is 0.
stop - The sequence goes up to, but not including, stop. (This way it is possible to use the stop value as the start in a following range.)
step - The increment between values. The default is 1.

Result: A list is returned containing the sequence that begins with start ends with stop, with values incremented by step. Note, lists have not been covered yet.
Side Effect: None.

Example:

x = 5
y = range(x)
print(y)      #
Output: [1, 2, 3, 4, 5]

Final Note

Functions aid in abstraction, the use of the function name causes a complicated task to be accomplished.

The user of a function does not usually care about how the function accomplishes its task, the user cares about what the function does for her (its result and/or side effect).

Consider the sqrt function.

Functions are important and are heavily used.

Many more examples will be seen.