Defining Functions

Functions

It is important to organize your code into modular blocks since we write more complicated code as we learn.

The programs we have written so far are a single function called main. We have been using pre-defined functions and methods including built-in functions, such as print(), len(), read(), readline(),etc.

You can think of a function as a small program inside a program.

A function definition is a program that creates a function.

A function is called or invoked when a function is subsequently used in a program.

User-defined functions

1. Use the keyword def and then the function name.

2. Add parameter or parameters within the braces to the function:

3. Add statements in the function.

4. End the function with a return statement if the function outputs something. Without the return statement, the function returns an object None.

A function definition looks like this:

def < name> <formal-parameters>):
     <body>

The function name must be an identifier. The formal-parameters (can be empty) is a sequences of variable names (also identifiers). The formal parameters, like all variables used in the function, are only accessible in the body of the function.

A function is called by using its name followed by a list of actual parameters or arguments.

<name>(<actual-parameters>)

There are four steps when a function is called.

1. The calling program suspends execution at the point of the call.

2. The formal parameters of the function are assigned the values supplied by the actual parameters in the call.

3. The body of the function is executed.

4. Control returns to the position after where the function was called.

For example,

def happy():
  print("Happy birthday to you!")
def singAdam():
  print("Happy birthday, dear Adam!")
def singAnna():
  print("Happy birthday, dear Anna!")

def main():
  happy()
  singAdam()
  happy()
  singAnna()

main()

There are four functions: main(), happy(), singAdam(), SingAnna(). Try the program and study the output.

The above example has no formal parameters.

Functions and Parameters/Arguments

The term parameters and arguments are the same. A parameter is the variable listed inside the braces in the function definition.

An argument is the value that is sent to the function when it is called. https://www.w3schools.com/python/python_functions.asp

def sum(a, b):
  print("The sum of",a, "and",b, "is:", a + b)

def diff(a, b):
  print("The difference between", a, "and", b, "is:", a - b)

def main():
  sum(2,3)
  diff(8,2)
main()

Actual Parameters

Actual parameters are values that are passed to a function when it is invoked. In the above example, in the main function, the value 2 and 3 are passed to the function sum(). The value 2 and 3 are the actual parameters. After the sum of 2 and 3 is displayed on the screen, the function diff() is invoked. Again, new two integers values 8 and 2 are passed to the diff() function. Now the actual parameters are 8 and 2. The difference of 8 and 2 will be displayed on the screen.

Formal Parameters

The formal parameters are enclosed in the braces on the right side of the function name. Each formal parameter is separated by a comma. They are the variables that receive values when the function is invoked. The variables a and b are formal parameters; they are accessed within the function. After the function call sum() is completed, the control is returned back to the main program.

Functions that return values

We use return statement if a function is returning values.

Example 1

def cube(x):
  return x**3

print(cube(2))
print(cube(3))

output
>8
>27

Example 2

def area(x, y):
    return x*y
    
def main():
  a,b = eval(input("Please enter x and y values separate by a comma: "))
  print("For a rectangle with length", a, "and width", b,", the area is:", area(a,b))

main()

Output

Please enter x and y values separate by a comma: 12,45
For a rectangle with length 12 and width 45, the area is: 540

Example 3

def swap_values(a, b):
    temp = a
    a = b
    b = temp
    return a, b

def main():
  x,y = eval(input("Please enter x and y values separate by a comma: "))

  print("Before swap: x =", x, "y =", y)

  x, y = swap_values(x, y)
  print("After swap: x =", x, "y =", y)

main()

Output

~/Lab8$ python example3.py
Please enter x and y values separate by a comma: 456.7,123
Before swap: x = 456.7 y = 123
After swap: x = 123 y = 456.7
~/Lab8$ 

In this program, the swap_values function takes two variables a and b as input. It creates a temporary variable temp and assigns the value of a to temp. Then, it assigns the value of b to a and finally assigns the value of temp (which was originally a) to b. The function returns the swapped values. In the main function, we ask user to enter two variables x and y, initialize two variables x and y with values the user entered. We then call the swap_values function passing x and y as arguments. The returned values are assigned back to x and y. Finally, we print the values of x and y to see the swapped values.

For more examples on functions returns values, check out the following websites.

https://www.w3schools.com/python/gloss_python_function_return_value.asp
https://runestone.academy/runestone/books/published/thinkcspy/Functions/Functionsthatreturnvalues.html

Anonymous Functions: Lambda Expression

A lambda function is a small anonymous function. It can take any number of arguments (values), but can only have one expression. The keyword lambda must come first. A full colon (:) separates the argument and the expression. It is very convenient to use lambda functions when an anonymous function is required for a short period.

Why use lambda functions?

1. Reduce the number of lines of code.

2. They are often used when a function is needed temporarily. Often used inside another function such as filter(), map() and reduce().

3. You can define a lambda and call it immediately at the end of the definition which you cannot do with def functions.

#Normal Python function

def a(b):
    return b**2
print("Normal function using def", a(3))

#The expression is executed and the result is returned.
#lambda arguments : expression
>a = lambda b : b**2
>print(a(3))
9
>print(a(6))
36
#Another example with multiple arguments
> x = lambda a,b, c: a*(b-c)
> print(x(2,5,1))
8
https://www.w3schools.com/python/python_lambda.asp

Here is an example on using lambda function to filter list elements. We only want odd numbers.

>numbers = [23,56,789,100,-75,82]
>odd_list  = list(filter(lambda x : (x%2!=0), numbers)) 
>print(odd_list)
[23,789,-75]

!= is the symbol we use for the not equal operator, and == is the symbol we use for the equal operator.

https://www.tutorialspoint.com/python/python_basic_operators.htm

For more examples on lambda functions using filter(), map() and reduce().

https://www.machinelearningplus.com/python/lambda-function/
https://www.guru99.com/python-lambda-function.html

Lab Assignment

1. Write a program to compute the volume and surface area of a sphere from the input radius. Here are the formulas:

Requirement:

Your program output should be similar to the following. The output, volume and surface area are required to be set in 2 decimal places.

This program computes the volume and surface area of a sphere.

Please enter the radius of the sphere: 5

The sphere volume is 523.60 cubic units.
The sphere surface area is 314.16 square units.

2. Write a short lambda function to list all even numbers from 0 - 20.

Requirement: Instead of using a list for all integers between 0 - 20, use range(20).

Here is the sample output.

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]