Laboratory Exercise on Function Parameters and Arrays
Work Started in Class
Function Parameters
In several programming languages, a function can be a parameter for another function. In some languages, parameters are un-typed — the parameter may have one type (e.g., a number) at one time, then another type (e.g, a string) for another call, and yet another type (e.g., a function) for a third call. In C, however, each variable and parameter has a designated type. In particlar, as we shall see, the designated type can indicate a function. For example, a function might have the following signature:
void printTable (double func (double, double))
-
Program func-parm.c uses a function printTable with the signature above to print a table of metric equivalents.
-
Save, compile, and run the program, and describe what is printed. Also, explain the roles of the functions toLiters and toCenti, and how these functions are utilized to obtain the output.
-
Function printTable prints a table, with rows always going from 0.0 to 10.0 and the columns going from 0.0 to 10.0, based on local variables rowStart, rowEnd, colStart, and colEnd. In this example, however, it would make more sense for the number of pints to go from 0.0 to 7.0 (number of pints less than a gallon), and the number of inches to go from 0.0 to 11.0 (number of inches less than a foot).
Replace these local variables with parameters, so that function printTable has the new signature:
void printTable (double rowStart, double rowEnd, double colStart, double colEnd, double func (double, double))
Then change the calls to printTable in the main program to more appropriate limits for each type of table.
-
-
Within a program to control a Scribbler 2 robot, write a function with the following signature:
void boxMove (void movement (double, double))
The function should move the Scribbler 2 robot 5 times using the following sequence:
- move forward for 1 second
- call the movement function with values 0.6, 1.0.
The main part of the program should call boxMove using the MyroC rTurnLeft function as parameter and then call boxMove again using rTurnRight.
Why do you think the name boxMove was chosen? Is there a better name?
Arrays of Functions
As noted earlier in this lab, functions as parameters provide one mechanism to take advantage of common elements within an algorithm. A second approach involves utilizing an array of functions.
Program func-parm-arrays.c produces exactly the same output as program func-parm.c from Step 1.
-
Copy this program to your account, compile it, and run it to check that the output matches the output from Step 1.
-
Check that the functions toLiters, toCenti, and printTable are unchanged from Step 1.
-
In the main program, note how variable titles is declared and initialized as an array of two strings. (In this context, char * indicates titles will refer to the start of strings, and [2] indicates the array will refer to the start of 2 strings.)
-
In the main program, note how the variable f refers to an array, each containing the address of a function of two double parameters.
-
-
Write 3 functions that cause the Scribbler 2 robot to react in different ways (e.g., move/turn, beep, spin). Then, write a program that uses these three functions as follows.
- Within main, make an array that references each of these functions.
- Use a loop to execute these functions in order, and then use a second loop to execute the functions in reverse order.
created 20 July 2011 by Erik Opavsky and Dilan Ustek revised 29 July 2011 by Erik Opavsky and Dilan Ustek revised 12 October 2011 by Erik Opavsky revised 31 October 2011 by Erik Opavsky revised (shortening lab (removal of old steps 1, 6, 7)) 21 July 2012 by Henry M. Walker reorganized within module 27-28 January 2014 readings added 19 September 2014 by Henry M. Walker wording refined for passing values and addresses as parameters 21 September 2014 by Henry M. Walker reformatted 12 August 2016 by Henry M. Walker split function pointers from testing 29 March 2022 by Henry M. Walker |
![]() ![]() |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |