Professional Documents
Culture Documents
CCS0006L
(COMPUTER PROGRAMMING 1)
MACHINE PROBLEM
7
FUNCTIONS AND RECURSION
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.
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.
Example:
The function of consists of two parts function header and function body. The function header is:-
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,
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.
V. LABORATORY ACTIVITY
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.
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;
if(choice == 1)
char temp;
cout<<"Do you want to continue [Y/N] : ";
cin>>temp;
if(temp == 'N')
break;
}
}
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.
}while(option!='X');//if users select 'X' the the loop will break else it will continue
return 0;
}
Write a program that uses recursion to compute for the highest common factor of two numbers.
Output:
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;
}
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