Function Lab 2
Type in Word. Work with a partner and submit ONE COPY per
pair. Include the following at the top of your submission:
Partner 1: Name
______________________________________________
role (driver or navigator) _____________________________
Partner 2: Name
______________________________________________
role (driver or navigator)______________________________
Please note if one
partner did not work on all problems:
---------------------------------------------------------------------------------------------------
//**************************************************
// NewWelcome program
// This program prints a "Welcome Home" message
//**************************************************
#include <iostream>
using namespace std;
void PrintLines( int ); // Function prototype
int main()
{
PrintLines(2);
cout << " Welcome Home!" << endl;
return 0;
}
//****************************************************************
// PrintLines prints lines of asterisks, where
// numLines specifies how many lines to print
//***************************************************************
void PrintLines( int numLines )
{
int line; // Loop control variable
for (line = 0; line < numLines; line++)
{
cout << "*************************************" << endl;
}
}
#include <iostream>
using namespace std;
int Square(int x);
void CalcCube(int x, int& cube);
int main()
{
int square,number, cube;
cout << "Enter number, 0 to quit" << endl;
cin >> number;
while(number != 0)
{
square = Square(number);//this is a function
CalcCube(number, cube); //this is a procedure
cout << number << " squared is " << square
<< " cubed is " << cube << endl;
cout << "Enter number, 0 to quit" << endl;
cin >> number;
}
return 0;
}
//**************************************
// this value returning function returns the square of x
//**************************************
int Square(int x)
{
int result;
result = x * x;
return (result);
}//**************************************
// this void function calculates the cube of x
//**************************************
void CalcCube(int x, int& cube)
{
cube = x * x * x;
}
|
value-returning function |
void function |
Name of function |
|
|
Type in c++(int, float, void) |
|
|
Contains return statement(y/n) |
|
|
Contains arguments(y/n) |
|
|
Other differences |
|
|
#include <iostream>
using namespace std;
void Swap(int x,int y); //prototype
int main()
{
int num1, num2;
cout << "Please enter two numbers to swap, the same numbers will quit." << endl;
cin >> num1 >> num2;
while (num1 != num2)
{
Swap(num1,num2);
cout << "After swap, the numbers are "
<< num1 << " " << num2 << endl;
cout << "Please enter two numbers to swap, ";
cout << "the same numbers will quit." << endl;
cin >> num1 >> num2;
}
cout << "Thanks!" << endl;
return 0;
}
//***************************
//This subroutine swaps two numbers
//*****************************
void Swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
1. Type in the above program as swap.cpp. Add a comment to include your name and date. Compile and run with different values.
2. To find out why this is not working, insert the following output statements into the swap function as appropriate.
a. cout << “ beginning of swap” << x << “ “ << y << endl;
b. cout << “ end of swap” << x << “ “ << y << endl;
Are the variables being swapped inside the swap function?
3. Explain why this program is not working correctly.
4. Fix the program so the swap will work. Compile and run and be sure the swap works.
5. Submit a copy of the final listing and output. Also include a copy of the hierarchy chart for the program.