Lab 3
1. Create a folder named Functions in side C++ Programming
2. In Functions folder create a file name 1.c with the following code:
#include <iostream>
using namespace std;
void displayMessage(){
cout << "Hello from the function displayMessage.\n";
}
int main(){
cout << "Hello from main.\n";
for (int count = 0; count < 5; count++){
displayMessage(); // Call displayMessage
}
cout << "Back in function main again.\n";
return 0;
}
3. Create a file name 2.c with the following code:
#include <iostream>
using namespace std;
void first();
void second();
int main()
{
cout << "I am starting in function main.\n";
first(); // Call function first
second(); //Call function second
cout << "Back in function main again.\n";
return 0;
}
void first()
{
cout << "I am now inside the function first.\n";
}
void second()
{
Lab 3 1
cout << "I am now inside the function second.\n";
}
4. Create a file name 3.c with the following code:
#include <iostream>
using namespace std;
//Funtion Prototype
void displayValue(int);
int main()
{
cout << "I am passing 5 to displayValue.\n";
displayValue(5);
cout << "Now I am back in main.\n";
return 0;
}
void displayValue(int num)
{
cout << "The value is " << num << endl;
}
5. Create a file name 4.c with the following code:
#include <iostream>
using namespace std;
void showSum(int, int, int);
int main()
{
int value1, value2, value3;
// Get three integers
cout << "Enter three integers and I will display ";
cout << "their sum: ";
cin >> value1 >> value2 >> value3;
// Call showSum passing three arguments.
showSum(value1, value2, value3);
return 0;
}
void showSum(int num1, int num2, int num3)
{
Lab 3 2
cout << (num1 + num2 + num3) << endl;
}
6. Create a file name 5.c with the following code:
#include <iostream>
using namespace std;
// Function prototype
int sum(int, int);
int main(){
int value1 = 20, // The first value
value2 = 40, // The second value
total; // To hold the total
// Call the sum function, passing the contents of value1 and value2
// as arguments. Assign the return value to the total variable.
total = sum(value1, value2);
// Display the sum of the values.
cout << "The sum of " << value1 << " and "
<< value2 << " is " << total << endl;
return 0;
int sum(int num1, int num2)
{
return num1 + num2;
}
7. Create a file name 6.c with the following code:
#include <iostream>
using namespace std;
// Function prototype
bool isEven(int);
int main()
{
int val;
// Get a number from the user.
cout << "Enter an integer and I will tell you ";
cout << "if it is even or odd: ";
cin >> val;
Lab 3 3
// Indicate whether it is even or odd.
if (isEven(val))
{
cout << val << " is even.\n";
}
else
{
cout << val << " is odd.\n";
}
return 0;
}
bool isEven(int number)
{
bool status;
if (number % 2 == 0)
{
status = true;
}
else
{
status = false;
}
return status;
}
8. Create a file name 7.c with the following code:
#include <iostream>
using namespace std;
void anotherFunction(); // Function prototype
int main()
{
int num = 1; // Local variable
cout << "In main, num is " << num << endl;
anotherFunction();
cout << "Back in main, num is " << num << endl;
return 0;
}
void anotherFunction()
{
int num = 20; // Local variable
cout << "In anotherFunction, num is " << num << endl;
}
Lab 3 4
9. Create a file name 8.c with the following code:
#include <iostream>
using namespace std;
void anotherFunction();
int num = 2;
int main(){
cout << "In main, num is " << num << endl;
anotherFunction();
cout << "Back in main, num is " << num << endl;
return 0;
}
void anotherFunction()
{
cout << "In anotherFunction, num is " << num << endl;
num = 50;
cout << "But, it is now changed to " << num << endl;
}
10. Create a file name 9.c with the following code:
#include <iostream>
using namespace std;
//Function prototype with default arguments
void displayStars(int = 10, int = 1);
int main()
{
displayStars(); // Use default values for cols and rows.
cout << endl;
displayStars(5); // Use default value for rows.
cout << endl;
displayStars(7, 3); // Use 7 for cols and 3 for rows.
return 0;
}
void displayStars(int cols, int rows)
{
// Nested loop. The outer loop controls the rows and
// the inner loop controls the columns.
for (int down = 0; down < rows; down++){
for (int across = 0; across < cols; across++){
cout << "*";
}
Lab 3 5
cout << endl;
}
}
11. Create a file name 10.c with the following code:
#include <iostream>
#include <cstdlib> // Need for the exit function
using namespace std;
void Function(); // Function prototype
int main()
{
Function();
return 0;
}
void Function(){
cout << "This program terminates with the exit function.\n";
cout << "Bye!\n";
exit(0);
cout << "This message will never be displayed\n";
cout << "because the program has already terminated.\n";
}
12. Zip the file then upload it.
Lab 3 6