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

Lab Activity 5a

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)
10 views

Lab Activity 5a

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

LAB ACTIVITY 5A: FUNCTION (I)

Duration: 2 Hours

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

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 5A (i)
Activity Outcome: Write and compile a program using C++ using functions in programming
Duration : 15 minutes

Procedure:

Step 1: Type the programs given below:

// function example
#include <iostream>
using namespace std;

int addition (int a, int b)


{
int r;
r=a+b;
return r;
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}

Step 2: Compile the program.


Step 3: Write the output.
Activity 5A (ii)
Activity Outcome: Write and compile a program using C++ using functions prototype
Duration : 15 minutes

Procedure:
Step 1: Type the programs given below

#include < iostream>


using namespace std;
int sum (int x, int y); //declaring function
int main()
{
int a = 10;
int b = 20;
int c = sum (a, b); //calling function
cout << c;
return 0;
}
int sum (int x, int y) //defining function
{
return (x + y);
Step 2: Compile the program.
}
Step 3: Write the output.

Activity 5A (iii)
Activity Outcome: Write and compile a program using C++ using function concept
Duration : 30 minutes

Procedure:

Step 1: Type the programs given below

#include <iostream> // function returning the max between two numbers


using namespace std; int max (int num1, int num2)
// function declaration {
int max (int num1, int num2); // local variable declaration
int main () int result;
{ // local variable declaration: if (num1 > num2)
int a = 100; result = num1;
int b = 200; else
int ret; result = num2;
// calling a function to get max value. return result;
ret = max (a, b); }
cout << "Max value is: " << ret << endl;
return 0;
}
Step 2: Compile the program.
Step 3: Write the output.

Activity 5A (iv)
Activity Outcome: Write and compile a program using C++ using passing parameter by
reference
Duration : 30 minutes

Procedure:

Step 1: Type the programs given below


// passing parameters by reference
#include <iostream>
using namespace std;

void duplicate (int& a, int& b, int& c)


{
a*=2;
b*=2;
c*=2;
}

int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}

Step 2: Compile the program.


Step 3: Write the output.
Activity 5A (v)
Activity Outcome: Write and compile a program using C++ using passing parameter by value
Duration : 30 minutes

Procedure:

Step 1: Type the programs given below

// default values in functions


#include <iostream>
using namespace std;

int divide (int a, int b=2)


{
int r;
r=a/b;
return (r);
}

int main ()
{
cout << divide (12) << '\n';
cout << divide (20,4) << '\n';
return 0;
}

Step 2: Compile the program.


Step 3: Write the output.

You might also like