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++:
#include <iostream.h>
int num2 = 10;insert the line
heading();
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.
void heading();
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.
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:
cout << "The number of bytes used for an int is " << sizeof(int) << endl;
#include <iostream.h>
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2002