You are on page 1of 13

COLLEGE OF COMPUTER STUDIES AND MULTIMEDIA ARTS

CCS0006L
(COMPUTER PROGRAMMING 1)

MACHINE PROBLEM

7
FUNCTIONS AND RECURSION

Rosendo, Sean Reznor M.


Student Name:
4-C
Section:
Mr. Renjun Orain
Professor:
I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE
 Analyze a complex problem and identify and define the computing requirements appropriate to its solution.
[PO: B]
 Design, implement and evaluate computer-based systems or applications to meet desired needs and
requirements. [PO: C]

II. COURSE LEARNING OUTCOME/S (CLO) ADDRESSED BY THE LABORATORY EXERCISE


 Select and apply appropriate program constructs in developing computer programs. [CLO: 2]
 Develop, test and debug computer programs based on a given specification using the fundamental
programming components. [CLO: 3]

III. INTENDED LEARNING OUTCOME/S (ILO) OF THE LABORATORY EXERCISE


At the end of this exercise, students must be able to:
 Construct a user defined functions creating well-structured C++ programs.
 Implement a recursive solution in solving a computing problem.

IV. BACKGROUND INFORMATION

Programs can be constructed into a number of separate modules or functions allowing the
programmer to replace many occurrences of the same section of code wit single occurrence in the form of
function. Such function can be called or invoked as often as required by the source code and from different
portions within the program code.

A function is a subprogram that acts on data and often returns a value.

Good C++ programmers write programs that consist of many of these small functions. These
programmers know that a program written with numerous functions is easier to maintain, update and debug
than one very long program. By programming in a modular (functional) fashion, several programmers can
work independently on separate functions which can be assembled at a later date to create the entire
project.

Each function has its own name. When that name is encountered in a program, the execution of
the program branches to the body of that function. When the function is finished, execution returns to the
area of the program code from which it was called, and the program continues on to the next line of code.

CCS0003L-Computer Programming 1 Page 2 of 13


All C++ programs must contain the function main( ). The execution of the program starts from the
function main( ). A C++ program can contain any number of functions according to the needs. The general
form of the function is: -

return_type function_name(parameter list)


{
local_definitions;
function_implementation;
}

Example:

int sum(int num1, int num2)


{
int sum;
sum = num1 + num2;
return sum;
}

The function of consists of two parts function header and function body. The function header is:-

return_type function_name(parameter list)

The return_type specifies the type of the data the function returns. The return_type can be void
which means function does not return any data type. The function_name is the name of the function. The
name of the function should begin with the alphabet or underscore. The parameter list consists of variables
separated with comma along with their data types. The parameter list could be empty which means the
function do not contain any parameters. The parameter list should contain both data type and name of the
variable. For example,

CCS0003L-Computer Programming 1 Page 3 of 13


int sum(int n1, int n2)

is the function header of the function factorial. The return type is of integer which means function
should return data of type integer. The parameter list contains two variables n1 and n2 of type integer. The
body of the function has the local definitions and function implementation that performs the computations.

Function Prototypes

The prototype of a function provides the basic information about a function which tells the compiler
that the function is used correctly or not. It contains the same information as the function header contains.
It can be used to check the calls to the function for the proper number of parameters and the correct
types of parameters.
To write the prototype, simply copy the header from the function to the beginning of the program
and append a semicolon to the end as a signal to the compiler that this is not a function but a prototype.

The prototype of the function in the above example would be like

int sum (int x, int y);

V. LABORATORY ACTIVITY

ACTIVITY 7.1: Check the Temperature

Write a program that performs temperature conversion from C to F, F to C and C to K based on the
value of the choice argument that calls either of the three functions namely Celcius_to_Fahrenheit ,
Fahrenheit_to_Celcius and Celcius_to_Kelvin. If the user wishes to terminate the program, the user
must type ‘N’ or ‘n’ to the question “Do you want to continue[Y/N]?” but if the user enters ‘Y’ or ‘y’ as the
answer then the program must return to the main menu.

Output:
*************************************************************************************************************************
TEMPERATURE CONVERTER
*************************************************************************************************************************
[1] – Celsius to Fahrenheit
[2] – Fahrenheit to Celsius
[3] – Celcius to Kelvin
**************************************************************************************************************************
Choose your option: 1
Enter temperature in Celsius: 25.5
25.5 degree Celsius is 77.9 degree Fahrenheit.

Do you want to continue[Y/N]?

CCS0003L-Computer Programming 1 Page 4 of 13


Program: (save as [surname_7_1.cpp])
#include <iostream>
#include <iomanip>
using namespace std;

double fahrenheitToCelsius(double fahrenheit)


{
double celsius;
celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
return celsius;
}

double Cel_To_Fah(float n)
{
return ((n * 9.0 / 5.0) + 32.0);
}

int main()
{
double fahrenheit,Celsius;
int choice;
while(1)
{
cout<<"**************************************************************************"<<endl;
cout << setw(50);
cout<<"Temperature Converter"<<endl;
cout<<"**************************************************************************"<<endl;

cout<<"[1] - Celsius to fahrenheit "<<endl;


cout<<"[2] - Fahrenheit to Celsius"<<endl;
cout<<"[3] - Celsius to Kelvin"<<endl;
cout<<"**************************************************************************"<<endl;

cout<<"Enter your choice : ";


cin>>choice;

if(choice == 1)

CCS0003L-Computer Programming 1 Page 5 of 13


{
cout<<"Enter temperature in Celsius :";
cin>>Celsius;
cout<<Celsius<<" degree Celsius is " <<Cel_To_Fah(Celsius)<<" degree fahrenheit. "<<endl;
}
if(choice == 2)
{
cout<<"Enter temperature in fahrenheit :";
cin>>fahrenheit;
cout<<fahrenheit<<" degree Fahrenheit is " <<fahrenheitToCelsius(fahrenheit)<<" degree
celsius."<<endl;
}
if(choice == 3)
{
cout<<"Enter temperature in Celsius :";
cin>>Celsius;
cout<<Celsius<<" degree Celsius is " <<Celsius+273.15<<" degree Kelvin."<<endl;
}

char temp;
cout<<"Do you want to continue [Y/N] : ";
cin>>temp;
if(temp == 'N')
break;
}
}

CCS0003L-Computer Programming 1 Page 6 of 13


Output:(screenshot of the output)

CCS0003L-Computer Programming 1 Page 7 of 13


ACTIVITY 7.2: Volume of Figures

Develop a program that asks the user to select a letter that represents the shape of the figure s/he wants to
solve the volume. If the user wishes to terminate the program, s/he must press X in the menu to exit. Use
looping structure, switch statement and functions to implement the program.

Output:

*************************************************************************************************************************
Volume of Shapes
*************************************************************************************************************************
[C] – Cube
[S] – Sphere
[R] – Rectangular Parallelepiped
[X] – EXIT
*************************************************************************************************************************
Choose your option: C
You choose to solve the Volume of the Cube!
Enter side: 3
The volume of the cube is 27.00.

Press any key to continue…

Program: (save as [surname_7_2.cpp])


#include<bits/stdc++.h>
using namespace std;
// Prints the volume of cube
void volumeOfCube()
{
float side;
cout<<"Enter side: ";
cin>>side;
float volume=side*side*side;
cout<<"\nThe volume of the cube is "<<volume<<"."<<endl;
}
//Prints the volume of sphere
void volumeOfSphere()
{
float radius;
cout<<"Enter radius: ";
cin>>radius;
float volume= (4*3.14*radius*radius*radius)/3;

CCS0003L-Computer Programming 1 Page 8 of 13


cout<<"\nThe volume of the sphere is "<<volume<<"."<<endl;
}
//Prints the volume of Rectangular Parallelepiped
void volumeOfRectangularParallelepiped()
{
float length,breadth,height;
cout<<"Enter length: ";
cin>>length;
cout<<"\nEnter breadth: ";
cin>>breadth;
cout<<"\nEnter height: ";
cin>>height;
float volume=length*breadth*height;
cout<<"\nThe volume of the Rectangular Parallelepiped is "<<volume<<"."<<endl;
}
int main()
{
char option;//the option user chooses will be stored in this variable
do{
//menu
cout<<"************************************************************************************************************
***********"<<endl;
cout<<"Volume of Shapes"<<endl;
cout<<"************************************************************************************************************
***********"<<endl;
cout<<"\t\t\t[C] – Cube"<<endl;
cout<<"\t\t\t[S] – Sphere"<<endl;
cout<<"\t\t\t[R] – Rectangular Parallelepiped"<<endl;
cout<<"\t\t\t[X] – EXIT"<<endl;
cout<<"************************************************************************************************************
***********"<<endl;
cout<<"Choose your option: ";
cin>>option;//taking user input
char dummykey;
switch (option)
{
case 'C':
{
volumeOfCube();

CCS0003L-Computer Programming 1 Page 9 of 13


cout<<"Press any key to continue…"<<endl;
cin>>dummykey;
break;
}
case 'S':
{
volumeOfSphere();
cout<<"Press any key to continue…"<<endl;
cin>>dummykey;
break;
}
case 'R':
{
volumeOfRectangularParallelepiped();
cout<<"Press any key to continue…"<<endl;
cin>>dummykey;
break;
}
case 'X':
{
cout<<"Exiting..."<<endl;
break;
}
default :
cout<<"Invalid option"<<endl;//if input doesn't match with the available options.
}

}while(option!='X');//if users select 'X' the the loop will break else it will continue

return 0;
}

CCS0003L-Computer Programming 1 Page 10 of 13


Output:(screenshot of the output)

CCS0003L-Computer Programming 1 Page 11 of 13


ACTIVITY 7.3: Highest Common Factor

Write a program that uses recursion to compute for the highest common factor of two numbers.

Output:

Enter two positive integers: 366 60


HCF of 366 and 60 is 6.

Program: (save as [surname_7_3.cpp])


#include <iostream>
using namespace std;
int hcf(int m, int n)
{
if (m % n == 0) {
return n; // base case
}
else
{
return hcf(n, m % n); // recursive case
}
}

int main()
{
int m, n;
cout << "Enter two positive integers: ";
cin >> m >> n;
cout << "HCF of " << m << " and " << n << " is " << hcf(m, n) << "." << endl;
return 0;
}

Output:(screenshot of the output)

CCS0003L-Computer Programming 1 Page 12 of 13


VI. QUESTION AND ANSWER

Directions: Briefly answer the questions below.

 Why is it necessary to use functions in creating complex programs?


VII.
In complex coding scenarios, using functions makes it quicker and easier as opposed to manually
coding without functions.

 When is recursion applicable in solving computing problems?


VIII.
In large values where manually solving computing problems would take longer.

IX. REFERENCES
 Abraham (2015). Coding for dummies. John Wiley and Sons: Hoboken, NJ
 Zak, D (2015). An Introduction to Programming with C++. 8th Edition
 Cadenhead, R et. Al. (2016). C++ in 24 Hours, Sams Teach Yourself (6th Edition).Sams Publishing
 McGrath, M. (2017). C++ programming in easy steps (5th ed.). Warwickshire, United Kingdom: Easy
Steps Limited
 Tale, T. (2016). C++: The Ultimate Beginners Guide to C++ Programing. CreateSpace Independent
Publishing Platform
 http://cs.uno.edu/~jaime/Courses/2025/devCpp2025Instructions.html

CCS0003L-Computer Programming 1 Page 13 of 13

You might also like