0% found this document useful (0 votes)
9 views

Lab Activity 5b

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab Activity 5b

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB ACTIVITY 5B: FUNCTION (II)

Duration: 2 Hours

Learning Outcomes
This lab activity encompasses activities 5B(i), 5B(ii) and 5B(iii).

By the end of this practical session, you should be able to :

• Apply the functions in programming.


• Declare function prototypes
• Identify the scope of variables
• Use the parameters passing techniques

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

The following example illustrates how to passing arguments to function.

Procedure:

Step 1: Type the programs given below

//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;

cout<<"Enter a character: ";


cin>>character;

cout<<"Enter repeat count: ";


cin>>count;

RepeatChar(character,count); //call function


return 0;
}
//function definition
void RepeatChar(char ch, int n)
{
for(int j=0; j<n; j++)
cout<<ch<<"\t";
cout<<endl;
}

Step 2: Compile the program.


Step 3: Write the output.

Step 4: Save the program as ________________

Activity 5B (ii)

Activity Outcome: Write and compile a program in user-defined function.


Duration: 60 minutes

The following example illustrates how to write program using user-defined function and identify
function calls using call by value.

Procedure:

Step 1: Type the programs given below.

//This is a program to identify the function calls using call by value

#include <iostream>
using namespace std;

//Function prototype/declaration
int sum(int, int, int);

int main()
{
int p = 11, q = 22, r = 33, total;

total = sum(p, q, r); //call function


cout<<"Total value is: "<<total<<endl;

total = sum(p, q=55, r); //call function


cout<<"Total value is: "<<total<<endl;
return 0;

//function definition
int sum(int p, int q, int r)
{
int result;

result = p + q + r;
return (result);

Step 2: Compile the program.


Step 3: Write the output.

Step 4: Save the program as ________________

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.

Step 6: What is the return type of a function sum?


__ _____________________________________________________________

Step 7: List all parameter(s) involve in the function sum?


________________________________________________________________
Activity 5B (iii)

Activity Outcome: Write and compile a program in user-defined function.


Duration: 30 minutes

The following example illustrates how to write program using user-defined function and identify
function calls using call by reference.

Procedure:

Step 1: Type the programs given below.

//This is a program to identify the function calls using call by reference

#include<iostream>
using namespace std;

//function prototype/declaration
void funct_ref(int &z1, int z2);

int main()
{
int x = 1;
int y = 1;

funct_ref(x, y); //call function

cout<<"x is "<<x<<endl;
cout<<"y is "<<y<<endl;

//function definition
void funct_ref(int &z1, int z2)
{
z1++;
z2++;
}

Step 2: Compile the program.


Step 3: Write the output.
Step 4: Save the program as ________________

Step 5: Change the initial value of x to 10, and y to 20. Then compile the program and write the
output.

You might also like