CSC 101 Lecture Notes Week 8

Review of Program 4 Testing

More on Function Parameters,
including parameters to
main


Relevant Reading: Chapters 6 (again) and 13





  1. Review of Testing for Program 4

    1. The index to the program 4 directory has links to several files related to testing your blackjack program.

    2. We'll go over those testing resources in class.


  2. Review of Function Parameters

    1. The primary use of function parameters is to provide input to the function; we have seen many examples of this in lectures so far.

    2. In the Week 6 notes, we saw how arrays can be use as function output parameters, in addition to being used for input.

    3. This week, we'll see how pointers can be used for function output parameters of any type.

    4. We'll also see how to supply parameters to the main function of a C program.


  3. Recall from Notes Week 6: The Precise Steps of a Function Call

    1. We have seen a variety of different kinds of functions so far in 101.

    2. All functions follow the same steps when they're called.

    3. The steps are:

      1. evaluate the actual parameters -- the actual parameters are the values and expression the function is called with

      2. assign actual parameter values to the corresponding formal parameters -- the formal parameters are the names of the parameters the function is defined with

      3. run the function body -- the function body is the C code between the opening and closing curly braces of the function definition

      4. return from the function -- the return from a function is done by zero or more return statements in the function body; in the case of void functions, no explicit return is necessary, since the function returns when control reaches the closing curly brace of its definition


  4. A Naive Attempt at Function Output Parameters

    1. Suppose we want to write a function named exchange that can exchange the value of two integer variables.

    2. The intended effect of the function goes like this:

      1. Declare two integer variables outside of the exchange function.

      2. Assign the variables some values.

      3. Call the exchange function with the two variables.

      4. When the function returns, the variable values have been swapped.

    3. Here's an attempt at defining exchange, in a C program file named exchange-naive.c:
      
      #include <string.h>
      #include <stdio.h>
      
      /****
       *
       * Implementation and testing a C function that wants to exchange the value of
       * two integer variables.  Unfortunately, this function does not work as
       * intended.  See the file exchange.c for a version that does work.
       *
       */
      
      void exchange(int x, int y) {
          int tmp;                        /* Hold the value of x temporarily */
      
          tmp = x;
          x = y;
          y = tmp;
      }
      
      int main() {
      
          int i = 10;
          int j = 20;
          printf("i,j before exchange = %d,%d\n", i, j);
          exchange(i, j);
          printf("i,j after exchange = %d,%d\n", i, j);
      
          return 0;
      }
      
      

    4. When this program runs, the before and after values i and j are unchanged, i.e., the program outputs 10 and 20 in both of the printf statements.

      1. This is not the intent of the program, alas.

      2. What we want the program to output in the second printf is 20 and 10.

      3. So what's going on?

    5. The answer can be had with a step-by-step trace of what happens when the exchange function is called:

      1. evaluate the actual parameters -- these are 10 and 20, respectively

      2. assign the actual values to the corresponding formal parameters -- this results in the values of 10 and 20 being assigned to the parameters x and y

      3. run the function body -- this results in the values of x and y being exchanged; BUT NOTE CAREFULLY HERE: the exchange of x and y has no effect at all on i and j in main, since x and y are in totally separate memory locations from i and j

      4. return from the function -- since the function has a void return, it does not send back any value; and again, the changes that were made locally to x and y are not output in any way back to main

    6. Figure 1 is a picture of what memory looks like just before exchange returns.


      Figure 1: Main and exchange memory just before exchange returns; changing x and y had no effect on i and j.





  5. The Correct Version of Exchange

    1. To get the exchange function to work, we need to use formal output parameters, as discussed in Section 6.4 of the book.

    2. The idea is to declare a formal parameter to be a pointer.

    3. Here is a working version of the exchange function, in a program file named exchange.c :
      
      #include <string.h>
      #include <stdio.h>
      
      /****
       *
       * Implementation and testing a C function that exchanges the value of two
       * integer variables.  It illustrates the concept of formal output parameters,
       * covered in Section 6.4 of the book.
       *
       */
      
      void exchange(int *x, int *y) {
          int tmp;                        /* Hold the value of x temporarily */
      
          tmp = *x;
          *x = *y;
          *y = tmp;
      }
      
      int main() {
      
          int i = 10;
          int j = 20;
          printf("i,j before exchange = %d,%d\n", i, j);
          exchange(&i, &j);
          printf("i,j after exchange = %d,%d\n", i, j);
      
          return 0;
      }
      
      

    4. For this version of the exchange program, calling the exchange function goes like this:

      1. evaluate the actual parameters -- these are a pointer to the variable x and a pointer to the variable y, respectively; note the use of the '&' operator in the actual parameters; '&' returns a pointer to (aka, address of) the variable it operates on

      2. assign the actual values to the corresponding formal parameters -- this results in the pointer values being assigned to the parameters x and y

      3. run the function body -- this results in the values of variables pointed to by x and y being exchanged; AND NOTE CAREFULLY HERE: the exchange of x and y does affect i and j in main, since x and y are pointers to i and j, and using the '*' operator in the body of exchange means that the change happens on what x and y point to, not on the values of x and y themselves

      4. return from the function -- since the function has a void return, it does not send back any value; however, since the changes through the formal output parameters did effect i and j in main, this means that the effect of executing exchange has produced a change in, i.e., and output to, the variables in main.

    5. Figure 2 is a picture of what memory looks like just before the pointer-based version of exchange returns.


      Figure 2: Main and exchange memory just before exchange returns.





  6. Formal Parameters to the Main Function of a C Program

    1. Section 13.7 on Page 688 of the book discusses this subject.

    2. This topic is also covered in CSC 101 Lab 7 .

    3. Here is the simple example from the Lab 7 page, that reads numeric command-line arguments and converts them from strings to ints.
      
      #include <stdio.h>
      #include <stdlib.h>
      
      /****
       *
       * Read command-line arguments and convert them to ints.  Invoke this program
       * from the terminal like this, for example:
       *
       *     cmd-line 10 20 30
       *
       * assuming that the program has been compiled with "gcc ... -o cmd-line".  The
       * output with this invocation is this:
       *
       *     arg[1] = 10
       *     arg[2] = 20
       *     arg[3] = 30
       *
       */
      
      int main(int argc, char** argv) {
          int i;
          int val;
          char* arg;
      
          for (i = 1; i < argc; i++) {
      
              arg = argv[i];
              val = atoi(arg);
              printf("arg[%d] = %d\n", i, val);
      
          }
      
          return 0;
      }
      
      
      

    4. We'll discuss this during lecture.




index | lectures | handouts | programs | labs | examples | solutions | grades