Laboratory Exercise on Function Prototypes and the Run-time Stack
Function Prototypes
-
Program add2.c utilizes a function prototype at the start of the code for function add2, followed by the main procedure and then the complete function definition for add2.
- Copy program add2.c to your account, compile it, run it, and write a brief description of how it works.
- Comment out the function prototype for add2 at the beginning of the program, and then try to compile the program again. Describe what happens and explain why.
- Going back to the original program, change the prototype so the parameter value has type int. (Do not change the function definition at the bottom of the code!) Again, try to compile the program, describe what happens, and explain why.
-
Copy the simulation program couple-6.c to your account.
- Modify the program so that functions simulate_couple and simulate_several_couples are defined in full at the end of the file — that is, after main is defined. Try to compile and run the program and explain what happens.
- Building on part a, add an index of functions at the start of couple-6.c, so that each function has a brief description and a function prototype. Again compile and run the program.
-
Write a program that instructs a Scribbler 2 robot to beep twice with these commands:
rBeep (1, 880); rBeep (2, 1760);
- Try compiling your program without including MyroC.h.
- Add #include "MyroC.h" near the start of the program, and try compiling and running the program again.
In each case, explain what happens.
Note that the rBeep command expects the duration parameter to be first with type double In this case, we would like the int 1 or 2 to be converted to double as part of processing. This problem explores what does or does not happen!
The Run-time Stack
- Returning to the original couple-6.c program, draw schematic memory diagrams for the state of memory just after each function begins and just before each function ends. Be sure you can explain the running of the program at each step, based upon this series of schematic diagrams.
-
Consider the program lab-value-param.c.
-
Copy the program to your account, compile, and run it.
-
Draw schematic memory diagrams for the state of memory when each print statement is executed. Be sure you can explain the running of the program at each step, based upon this series of schematic memory diagrams.
-
Motivational Pep Talk
Many elements of program execution with the C programming language depend upon details of the storage of data within main memory.
Although drawing schematic memory diagrams may seem tedious at times, these diagrams often represent the most reliable mechanism for interpreting many C programs.
In looking ahead to later problem solving and C programming in this course, resist any temptation to skip steps or skim over the drawing of diagrams now!
Homework
-
Write your own program to control a Scribbler 2 program.
- The program should command the robot to beep and/or move.
-
The program should include these features:
- The program should contain at least two global variables.
- The program should have at least two functions, and each function should have both parameters and local variables.
- At least one function should call another function.
- Once the program is written, draw schematic memory diagrams that shows the state of memory just after each function begins and just before each function ends.
-
Repeat Step 5 for the program homework-value-param.c.
Overall lab materials | ||
created 7 August 2016 by Henry M. Walker revised 9-12 August 2016 by Henry M. Walker |
![]() ![]() |
|
Step 5 | ||
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 21 July 2012 by Henry M. Walker step moved to subsequent lab 2 January 2014 by Henry M. Walker wording refined for passing values as parameters 21 September 2014 by Henry M. Walker | ||
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |