CSC133 – LAB 1 (Continued)
Fall 2002
In order to keep the programs that we write organized and readable we need a way of breaking them down into separate logical parts. Functions allow us to do this. A function is a block of code (collection of statements) intended to accomplish a single task. Every function begins with a function header, which among other things specifies the name of the function.
Example:
int main()
Every function also has a function body that is enclosed in braces { }.
In this assignment you will enter and execute a small C++ program that is comprised of two functions. The first function’s name is heading. This function will display on the screen, your name, course and section number, lab assignment number, and a brief description of this assignment. This is function heading:
void heading()
{
cout<<”Your name\n”;
cout<<”CSC133-your section number\n”;
cout<<”Lab #1”<<endl;
cout<<”This assignment demonstrates the use of functions”<<endl;
}
Replace “Your name” with your name. Replace “your section number” with your section number.
The second function is named main. Every C++ program has one, and only one, function named main and execution of every C++ program starts with the first executable statement in its function main. Function main adds and displays two numbers. This is function main:
int main()
{
int num1=5, num2=10;
cout<<”The sum of num1 + num2 is “<<(num1 + num2)<<endl;
return 0;
}
1. Be sure to save the work you’ve been doing.
2. Close all open windows except the Turbo C++ window.
3. Under the file menu, click new.
4. Type the following code:
#include <iostream.h>
5. Type the code for the heading function as described above.
6. Type the code for the main function as described above.
7. Save your program with the name lab1f.cpp
8. Click the lightning bolt.
9. Correct errors until your program executes.
10. Answer the following questions:
a. What was the output of your program?
b. Was the output what you had expected?
c. What happened to the statements that printed your name, course, section, etc?
11. Make the following change to the main function. After the line
int num1=5, int num2=10;
insert the line
heading();
12. Rerun your program and observe the output. Has it changed?
The statement
heading();
is known as a calling statement. If execution begins in function main then the calling statement causes the computer to suspend the execution of function main and begin execution of function heading. When function heading completes, function main continues executing from where it left off.
13. Cut and paste function heading so that it is now located after function main.
14. Execute your program again. What happened?
15. You got an error because the description of function heading was located after the call to function heading. To correct this problem we need to add another statement to function main. In function main after the open braces { insert the line
void heading();
19. Rerun your program. Did it execute correctly this time?
The statement
void heading();
is known as a prototype statement. Prototype statements allow us to write the code for a function after the call to that function. We saw that if the function was coded before the call, the prototype was not necessary.
Congratulations. You’ve now completed your first lab assignment. J