Lab Activity 5b
Lab Activity 5b
Duration: 2 Hours
Learning Outcomes
This lab activity encompasses activities 5B(i), 5B(ii) and 5B(iii).
Hardware/Software: C++ software (Dev C++, Microsoft Visual Studio, Turbo C++ 5.0/6.0)
Activity 5B (i)
Activity Outcome: Write and compile a program in user-defined function.
Duration: 30 minutes
Procedure:
//This is a program to receive input from the user and display the output
using user-defined function
#include<iostream>
using namespace std;
//function prototype/declaration
void RepeatChar(char, int);
int main()
{
char character;
int count;
Activity 5B (ii)
The following example illustrates how to write program using user-defined function and identify
function calls using call by value.
Procedure:
#include <iostream>
using namespace std;
//Function prototype/declaration
int sum(int, int, int);
int main()
{
int p = 11, q = 22, r = 33, total;
//function definition
int sum(int p, int q, int r)
{
int result;
result = p + q + r;
return (result);
Step 5: Change the initial value of p to 15, q to 10, and r to 50. Then compile the program and
write the output.
The following example illustrates how to write program using user-defined function and identify
function calls using call by reference.
Procedure:
#include<iostream>
using namespace std;
//function prototype/declaration
void funct_ref(int &z1, int z2);
int main()
{
int x = 1;
int y = 1;
cout<<"x is "<<x<<endl;
cout<<"y is "<<y<<endl;
//function definition
void funct_ref(int &z1, int z2)
{
z1++;
z2++;
}
Step 5: Change the initial value of x to 10, and y to 20. Then compile the program and write the
output.