DEPARTMENT OF COMPUTING
CS-107: Computer Programming
Class: BEE-16CD
Lab 08: Complex Functions
CLO-3: Build a program and associated documentation using appropriate IDE and
supplementary tools
Date: 9th Apr 2025 - 10th Apr 2025
Time: 14:00 - 16:50
Lab Instructor: Mr. Moeed Ahmed
Class Instructor: Ms. Mehreen Tahir
CS107: Computer Programming
Spring 2025
Page 1
Introduction
Lab 08: Complex Functions
This lab focuses on some advance usage of multiple parameters functions, function recursion and
custom functions by including libraries.
Objectives
After successfully performing this lab you will be able to understand the use of:
Multi-parameters functions
Different recursion functions
Different functions of <cmath>, <algorithm>, <cctype> and similar
Tools/Software Requirement
Microsoft Visual Studio 2013 or later
Description
Multi-parameters Functions
C++ multiple function parameters describe the ability of a function to accept more than
one argument or can say functions with multiple parameters to perform operations using several
inputs, this feature makes a function to execute more complex operations by working with
multiple subset of data at once.
In C++, you can define a function with multiple parameters by listing them in the
function’s declaration and definition, separated by commas. Here's the basic syntax:
There are 2 types of passing data to multiple function parameters:
1.
Single Data Type for Multiple Parameters
CS107: Computer Programming
Spring 2025
Page 2
2.
Multiple Data Types for Multiple Parameters
Multiple Parameters Passing Techniques
Multiple parameters (or, single) passing techniques in C++ refer to the methods which are
used to pass arguments to functions. These techniques define how the data is transferred and
manipulated within the function. Here we will discuss few primary techniques:
Pass by Value
Pass by Reference
CS107: Computer Programming
Spring 2025
Page 3
Recursive Functions
A recursive function is a function which is particularly used for recursion where a function
calls itself, either directly or indirectly, to address a problem.
It must include at least one base case to terminate the recursion and one recursive case
where the function invokes itself.
Calling a Recursive Function
Calling a recursive function is just like calling any other function, where you will use the
function's name and provide the necessary parameters in int main() body.
Types of Recursion
Recursion can be categorized into two main types where each with its own sub-categories:
CS107: Computer Programming
Spring 2025
Page 4
1.
Direct Recursion - Direct recursion occurs when a function calls itself directly
a) Tail Recursion:
A form of direct recursion where the recursive call is the last operation in the function. It
is used for solving accumulative calculations and list processing problems.
b) Head Recursion:
The recursive call is made before any other operation in the function. Processing occurs
after the recursive call returns. It is used for tree traversals and output generation.
2.
Indirect Recursion - Indirect recursion occurs when a function calls another function, which
eventually leads to the original function being called. This involves two or more functions
calling each other.
CS107: Computer Programming
Spring 2025
Page 5
Useful Functions
pow( )
sqrt()
floor()
ceil()
min()
max()
swap()
toupper()
tolower()
abs()
Header file:
Syntax:
#include <cmath>
pow(base, exponent);
Header file:
Syntax:
#include <cmath>
sqrt(number);
Header file:
Syntax:
#include <cmath>
floor(value);
Header file:
Syntax:
#include <cmath>
ceil(number);
Header file:
Syntax:
#include <algorithm>
min(value1, value2);
Header file:
Syntax:
#include <algorithm>
max(value1, value2);
Header file:
Syntax:
#include <utility> //check either utility or algorithm
swap(value1, value2);
Header file:
Syntax:
#include <cctype>
toupper(ch); //where ch is lower case character
Header file:
Syntax:
#include <cctype>
tolower(ch); //where ch is upper case character
Header file:
Syntax:
#include <cmath>
abs(num);
Lab Tasks
1.
Define a function called hypotenuse() that calculates the length of longest side of a rightangle triangle when the other two sides are given. This function should take two arguments of
type double and return the hypotenuse as a double number. The program should also display
the value to the nearest whole number.
Pythagorean theorem:
where c is the hypotenuse, and a and b are the other two sides
CS107: Computer Programming
Spring 2025
Page 6
2.
Write a program with a function greet() to print a welcome message to the user after signing
up on the registration web form at a gym. The program will take details like name, age,
weight and birth place as input.
a) The function should assign an registration id to each user using random function and
display in output
b) The function must print output if no name was provided
c) The function should print date and time of joining
d) The program will ask again for the input if age is below 18
3.
Computers are playing an increasing role in education. Write a program that will help an
primary school student learn multiplication. Use rand function to produce two positive onedigit integers. It should then type a question such as:
How much is 6 times 7?
a) The student then types the answer and your program checks the answer
b) If it is correct, print "Very good!" and then ask another multiplication question
c) If the answer is wrong, print "No. Please try again." and then let the student try the same
question again repeatedly until the student finally gets it right
d) Program will terminate upon negative value
4.
Write a modular program to check whether a given number is a strong number or not. A
number is considered a strong number if the sum of the factorials of its individual digits is
equal to the original number entered by the user.
You must implement a recursive function called factorial() which takes a number as an input
and returns its factorial. Your program should keep on asking the end-user to enter ‘y’ to
continue & ‘q’ to quit the program.
Deliverable:
1.
Insert the screenshots for all above tasks which includes your code, its output after
compilation and terminal window after debugging; for each single program (don’t
crop any screenshot). Also add code for each lab task.
2.
Finalize the document in a well structured manner. Save the file with your name and
registration number and upload it on LMS under submission link for evaluation
before the deadline.
CS107: Computer Programming
Spring 2025
Page 7