CSCI 201 Study Aid: Functions

As programs expand in length and complexity, they become difficult to write, read, understand, and debug. Computer scientists have learned through experience that these difficulties become less significant if programs are written in blocks, or modules. In the C++ language these modules are called functions. Functions allow you to group code into a block that can be used repeatedly.

Library Functions

As C developed in the early years, users wrote functions for performing standard tasks, and many of these functions have been collected in what is known as the standard library, which is included with all compilers that meet the ANSI standards.

Access to these functions is acquired by inserting header files which contain the functions' prototypes. The actual function definitions are not usually accessable to programmers, since they are pre-compiled, and in any case programmers are not likely to want to change these functions. In order to use the library functions we need only know what the function declaration looks like.

For example, in order to use the library function which calculates the square root of a number, we must either know the following:

Your text book provides a description of some of the more commonly used C++ libraries in Appendix B.

Programmer-written Functions

If programming could be accomplished by assembling the right library functions, programming would be a great deal simpler than it is. It would also be less exciting and stimulating (and, yes, frustrating) than it is. Almost every program that needs to be written needs some user defined functions. Writing these functions is what programming in C++ is all about.

Defining a Function

In general, a function definition takes the form:

Declaring a Function

Functions with No Parameters or No Return Value

If there is no need for a function to return a value, a special type called void should be used to specify the return type in the function header. The following function prints the word "hello":
void print_welcome()
{
    cout << "\n" << "Hello" << endl;
}

If a function has no parameters, then the keyword void may again be used.

void print_welcome(void)
{
    cout << "\n" << "Hello" << endl;
}
Empty parentheses, () may be used when there are no parameters as shown above.

Function Communication

A function is called or invoked by a statement, referred to here as the calling statement. The calling statement usually, but not always, passes some information to the called function. The called function takes this information, performs some processing, and then usually, but not always, passes some information back to the function containing the calling statement (referred to henceforth as the calling function).

As is true of any form of communication the calling function and the called function must be in agreement about the parameters which are passed to the called function and the value which is returned to the calling function after execution of the called function.

Below is a complete program to calculate the doubling time for a population under continuous exponential growth.

// Complete program to calculate population doubling time

#include <iostream.h>

float doublingTime(float bacterialGrowthRate);  // Prototype - notice,
                                                //it is defined before main

int main ()
{
    float twiceTime, rateEColi;                // data declaration
    rateEColi = 0.84;       // Unit is bacteria per hour. Rates vary
                            // with the environment.

    twiceTime = doublingTime(rateEColi);    // Calling statement
				            // rateEColi is the
					    // actual parameter

    cout << "Doubling time for Escherichia coli is: "
         <<  twiceTime  << endl;
    return 0;
}

float doublingTime(float bacterialGrowthRate)   // Function definition - 
						// bacterialGrowthRate is
						// known as a 
						// formal parameter
{
    float natLogOf2 = 0.6931, time2Double;      // Data declarations for
                                                // doublingTime function
    time2Double = natLogOf2 / bacterialGrowthRate;
    return time2Double;
}

In the example program above, note that the rate of growth of E. Coli, rateEColi, is passed to the doubling_Time function, where its value is copied and referred to by the variable name bacterialGrowthRate. Remember that the two identifiers, the name of the actual parameter and the name of the formal parameter may or may not be the same; this has no significance because the values of the actual parameters are copied to the formal parameters. This process of copying the value of the actual parameter to the formal parameter is known as PASS BY VALUE.

Noteworthy facts concerning Passing By Value

Passing by Reference

Exercises

  1. What is the official name of the parameters that are used in the code which calls a function:
    1. Input parameters
    2. First parameters
    3. Actual parameters
    4. Formal parameters

  2. What is the official name given to the parameters which appear in the definition of a function?
    1. Output parameters
    2. Second parameters
    3. Actual parameters
    4. Formal parameters

  3. Actual parameters must have the same names as formal parameters. True or False?

  4. When a function which uses parameters is called, the actual and formal parameters must match each other in which of the following ways?
    1. Their data types must match.
    2. There must be the same number of actual as formal parameters
    3. The order in which they are specified must match.
    4. All of the above

  5. Spot the flaws in the following code:
    int   mysubroutine(int x, char y, float z)
    {
        if (y == 'a') return(x);
        if (z < 3.14) return(-x);
        return(x*x);
    }
    
    int main()
    {
       int a = 1;
       char c = '1';
    
       cout << mysubroutine(3, 'a', 7.1);
       a = mysubruotine(3, 'a', 1.2);
       mysubroutine(1.0, 'c', 4);
       c = mysubroutine(1, a, 3.2);
       a = mysubroutine(1, c, 3.2);
    }
    

  6. Which of the following are illegal:
    int  foo() { return(-1); }
    int  foo() { return('1'); }
    int  foo() { return(1.0); }
    int  foo() { return("1"); }
    int  foo() {  ; }
    void foo() {  ; }
    

    Solutions to 1 through 6

  7. Write a function which prints an increasing sequence of numbers with any given starting number and any given ending number.

    For example, if the function is called like this:

      print_numbers( 4, 9 );
    

    It will print:

      4 5 6 7 8 9
    

    Write a program which calls your function a number of times to test that it is accurate.

    Solution

  8. Write two different versions of a function that triples a number that it receives as input. In the first version, the number to be tripled is passed-by-value, and in the second version the number is passed-by-reference. Write a program to demonstrate both versions of your function.

    Solution