Lab Activity 5c

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

LAB ACTIVITY 5C: FUNCTION (III)

Duration: 2 Hours

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

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
• Understand recursive functions

Hardware/Software: C++ software (Dev C++, Microsoft Visual Studio, Turbo C++ 5.0/6.0)

Activity 5C (i)
Activity Outcome: Write and compile a program in user-defined function.
Duration: 30 minutes

The following example illustrates how to write program to describe the concept of recursion.

Procedure:

Step 1: Type the programs given below.

//This is a program that shows the concept of recursion

#include<iostream>
using namespace std;

//function prototype/declaration
int fibonacci(int);

int main()
{
for(int c = 0; c<=10; c++)
{
cout<<"fibonacci("<<c<<")="<<fibonacci(c)<<endl; //call function
}
}
DFC 20113 PROGRAMMING FUNDAMENTALS

//function definition
int fibonacci(int number)
{
if((0==number)||(1==number))
return number;
else
return fibonacci(number - 1) + fibonacci(number - 2);
}

Step 2: Compile the program.


Step 3: Write the output.

Step 4: Save the program as ________________

Activity 5C(ii)

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.

Procedure:

Step 1: Type the programs given below.

1|Page
DFC 20113 PROGRAMMING FUNDAMENTALS

//Count the area of a rectangle and triangle by using the function

#include<iostream>
using namespace std;

//function prototype/function declaration


int RECTANGLEAREA(int,int);
int TRIANGLEAREA(int,int);

int main()
{
int height,width;
int height2,base;

cout<<"INSERT RECTANGLE HEIGHTS:";


cin>>height;

cout<<"\nINSERT RECTANGLE WIDTH:";


cin>>width;

//call function
cout<<"\nTHE AREA OF RECTANGLE IS:"<<RECTANGLEAREA(height,width);

cout<<endl<<endl;

cout<<"\nINSERT TRIANGLE HEIGHTS:";


cin>>height2;

cout<<"\nINSERT TRIANGLE WIDTH:";


cin>>base;

//call function
cout<<"\nTHE AREA OF TRIANGLE IS:"<<TRIANGLEAREA(height2,base);

cout<<endl<<endl;

return 0;
}
//function definition
int RECTANGLEAREA(int height, int width)
{
int area=height*width;
return area;
}
//function definition
int TRIANGLEAREA(int height2, int base)
{
double area3=0.5*height2*base;
return area3;
}
Step 2: Compile the program.
Step 3: Write the output.
Step 4: Save the program as _______________

2|Page
DFC 20113 PROGRAMMING FUNDAMENTALS

Activity 5C(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.

Procedure:

Step 1: Type the programs given below.

1- //This is a program to display Area of circle using a function


2-
3- #include<iostream>
4- using namespace std;
5- void displayArea(double);
6-
7- int main()
8- {
9- double radius = 2;
10- displayArea(radius);
11- displayArea(3);
12-
13- return 0;
14- }
15-
16- void displayArea(double radius)
17- {
18- double area = radius * radius * 3.142;
19- cout<<"area is "<<area<<endl;
20- }

Step 2: Compile the program.


Step 3: Write the output.

Step 4: Save the program as ________________

Step 5: Which line(s) represent function prototype/declaration? ______________

Step 6: Which line(s) represent calling function? _________________________

Step 7: Which line(s) represent function definition? _______________________

3|Page
DFC 20113 PROGRAMMING FUNDAMENTALS

Activity 5C (iv)
Activity Outcome: Write and compile a program using C++ using recursive function
Duration: 30 minutes

Procedure:

Step 1: Type the programs given below with recursive function

#include <iostream>
using namespace std;

void numberFunction(int i)
{
cout << "The number is: " << i << endl;
i++;
if(i<10)
{
numberFunction(i);
}
}
int main()
{

int i = 0;
numberFunction(i);

return 0;
}

Step 2: Compile the program.


Step 3: Write the output.

4|Page
DFC 20113 PROGRAMMING FUNDAMENTALS

Step 4: Write the programs given below with function only.

#include <iostream>
Step 5:namespace
using Compile thestd;
program.
Step 6: Write the output.
void numberFunction(int i)
{
cout << "The number is: " << i << endl;
}

int main()
{

for(int i=0; i<10; i++)


{
numberFunction(i);
Step 7: Discuss
} the differences between two program above.

return 0;
}

5|Page

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy