Lab3 2
Lab3 2
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;
}
#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";
}
#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;
}
#include <iostream>
using namespace std;
int main()
{
int value1, value2, value3;
Lab 3 2
cout << (num1 + num2 + num3) << endl;
}
#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);
#include <iostream>
using namespace std;
// Function prototype
bool isEven(int);
int main()
{
int 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;
}
if (number % 2 == 0)
{
status = true;
}
else
{
status = false;
}
return status;
}
#include <iostream>
using namespace std;
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;
}
#include <iostream>
using namespace std;
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;
}
Lab 3 5
cout << endl;
}
}
#include <iostream>
#include <cstdlib> // Need for the exit function
using namespace std;
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";
}
Lab 3 6