CSC133 Introduction to CS Using C++
Lab 2

Part I

In this part of the lab, you will learn how to use functions.

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.An example function header is:

	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 #2" << endl;
	cout << "This assignment demonstrates the use of functions" << endl;
}

When you type in this function (as described below), 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. This main function adds and displays two numbers. This is function main:

int main()
{
	int num1 = 5;
	int num2 = 10;
	cout << "The sum of num1 + num2 is " << (num1 + num2) << endl;
	return 0;
}

Complete the following steps to get some idea of how functions work in C++:

  1. Be sure to save any 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 lab2.cpp
  8. Click the lightning bolt.
  9. Correct errors until your program executes.
  10. Answer the following questions:
    1. What was the output of your program?
    2. Was the output what you had expected?
    3. 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 num2 = 10;
    insert the line
         heading();
  12. Rerun your program and observe the output. Has it changed?

    The statement
         heading();
    is known as a call 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 the program. Before function main, and after the #include statement, insert the line
         void heading();
  16. 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.

Part II

In this part of the lab, you will create a program that will determine the number of bytes allocated for some of the data types in C++. You will also get more practice using functions.

Programs use variables to hold data, which is used to calculate the desired output. Each variable is a name for a memory location which contains a value. When we use a variable in our program, the computer uses the value contained in the memory location associated with the variable.

A variable has four attributes: name, data type, address, and value. The programmer determines the name and data type when the variable is declared. The address of the memory location where the variable is actually stored is determined by the compiler. The value of the variable is garbage (unless the programmer initializes the variable) until the program stores a value in the variable.

The size of the memory location allocated for a variable depends on the type of the variable. In C++, the size also can be different when using a different compiler or a different computer. C++ has a built in operator called sizeof, which returns the number of bytes allocated for a given variable or a given data type. For example, to determine the number of bytes allocated to an int variable, you can write:

     sizeof(int)

For this part of the lab, you need to write three functions:

  1. Function heading should print your name, course, section number, and lab assignment number (see function heading from Part I)
  2. Function calcsize should calculate and print the number of bytes allocated for variables of type char, short, int, long, float, and double.
    1. To write calcsize, first write the function heading. The name of the function is calcsize, and the return type is void. Put a pair of parentheses () after the function name.
    2. The function body comes after the parentheses. Remember that the function body is enclosed in braces {}. The function body should contain a statement to print the number of bytes allocated to each of the data types listed above. For example, to print the number of bytes allocated to an int, you would write:
           cout << "The number of bytes used for an int is "
                                    << sizeof(int) << endl;
  3. Function main should call heading and then call calcsize.
  4. At the beginning of your program, include comments with your name, course, section number, and lab number.
  5. The first statement after the comments in your program should be:
         #include <iostream.h>
  6. Include prototypes for heading and calcsize so that you can put the main function before the definitions of heading and calcsize.
  7. Be careful about formatting and indentation. Use the sample progrm given in the second part of lab 1 as a guideline.

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2002