Functions with a Parameter

A function often needs pieces of information to be able to complete its work. In the last section you might have noticed that I had to ask for the length and width twice, once for each of the area and perimeter calculations, clearly not ideal. One method of doing this is to declare the variable as a global variable and then any code within that file can access it. This is a very bruit force method and is very bad programming style. By doing it this way, the variable is created and held in memory for the entire time the file is running, even thought the information may only be needed for a small fraction of the time. Also the function is no longer portable, since it relies on external global variables that might not exist in another program. A better, more elegant and more memory friendly way is to pass the information into the function using a parameter. There are three main ways to pass information to a function, by value, by reference and by object reference. For the time being we will just consider by value. (We will even assume python is doing by value, even though it is really doing by object reference.)

Passing By Value

The first method of transferring information to a function is to pass it “By Value”. This means that a copy of the data is made and it is passed over to the function to do with it what it pleases. Since it is a copy of the data, any changes to the data are not reflected in the original variable. From the animation below you can see that when the cup is passed “By Value” to the function and then filled up, the original cup is still empty. This is because the variable passed to the function is a copy, not the original variable. If the function changes this variable, nothing happens to the original one.

Pass by value or reference

A variable or value passed along inside a function call is called an parameter . Parameter(s) are usually placed inside a bracket when you invoke the function. For example:

// calling a function
CalculateArea(lengthFromUser, widthFromUser);
// calling a function
// calling a function
// calling a function
# calling a function
calculate_area(length_from_user, width_from_user)
// calling a function
// calling a function

When you are creating your function, you must also tell the program that the function is expecting these two values. To do this after the function name declaration you place in brackets the two declaration statements declaring that the function must be passed in two variable (just like when a regular variable is being declared). If your programming language requires that you declare what type the variables will be normally, you will most like have to do that to.

The following is the function declaration line for the examples above:

// declaring a function
void CalculateArea(int length, int width) {
    ...
// declaring a function
// declaring a function
// declaring a function
# declaring a function
def calculate_area(length, width):
    ...
// declaring a function
// declaring a function

Here is a full example of the previous sections program, but now the main function takes care of gettin gthe length and width. This way it only have to ask you the information once and it passes the length and width to each function:

// Copyright (c) 2019 St. Mother Teresa HS All rights reserved.
//
// Created by: Mr. Coxall
// Created on: Oct 2019
// This program uses user defined functions

#include <iostream>

void CalculateArea(int length, int width) {
    // calculate area
    int area;

    // process
    area = length * width;

    // output
    std::cout << "The area is " << area << "cm^2" << std::endl;
}


void CalculatePerimeter(int length, int width) {
    // calculate perimeter
    int perimeter;

    // process
    perimeter = 2 * (length + width);

    // output
    std::cout << "The perimeter is "<< perimeter << "cm" << std::endl;
}

main() {
    // this function gets length and width

    int lengthFromUser;
    int widthFromUser;

    // input
    std::cout << "Enter the length of a rectangle (cm): ";
    std::cin >> lengthFromUser;
    std::cout << "Enter the width of a rectangle (cm): ";
    std::cin >> widthFromUser;
    std::cout << std::endl;

    // call functions
    CalculateArea(lengthFromUser, widthFromUser);
    CalculatePerimeter(lengthFromUser, widthFromUser);
}
// functions
// functions
// functions
#!/usr/bin/env python3

# Created by: Mr. Coxall
# Created on: Oct 2019
# This program uses user defined functions


def calculate_area(length, width):
    # calculate area

    # process
    area = length * width

    # output
    print("The area is {0} cm²".format(area))


def calculate_perimeter(length, width):
    # calculate perimeter

    # process
    perimeter = 2 * (length + width)

    # output
    print("The perimeter is {0} cm".format(perimeter))

def main():
    # this function gets length and width

    # input
    length_from_user = int(input("Enter the length of a rectangle (cm): "))
    width_from_user = int(input("Enter the width of a rectangle (cm): "))
    print("")

    #call functions
    calculate_area(length_from_user, width_from_user)
    calculate_perimeter(length_from_user, width_from_user)


if __name__ == "__main__":
    main()
// functions
// functions