CS102 Computer Programming 1 Lab session - # 6
Term: Spring 2026
Topic:
• Functions • Overloading function
• Defining and calling functions • Function prototypes
• main functions, order of functions, • Default arguments
max function • Inline function
• void function • Local, Global and Static Variables
• Passing argument by value • Passing argument by reference
Review
• A function is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a function.
• Functions are used to perform certain actions, and they are important for
reusing code: Define the code once, and use it many times.
Create a function
• C++ provides some pre-defined functions, such as main(), which is used
to execute code. But you can also create your own functions to perform
certain actions.
• To create (often referred to as declare) a function, specify the name of the
function, followed by parentheses ():
• myFunction() is the name of the function
• void means that the function does not have a return value. You will learn
more about return values later in the next chapter
Page 2
• inside the function (the body), add code that defines what the function
should do
Call a function
• Declared functions are not executed immediately. They are ”saved for later
use”, and will be executed later, when they are called.
• To call a function, write the function’s name followed by two parentheses ()
and a semicolon ;
• In the following example, myFunction() is used to print a text (the action),
when it is called:
Page 3
Page 4
Parameters and Arguments
• Information can be passed to functions as a parameter. Parameters act as
variables inside the function.
• Parameters are specified after the function name, inside the parentheses.
You can add as many parameters as you want, just separate them with a
comma:
Page 5
Problem 1
Define a function that returns the product of two numbers en-
tered by user:
int product(int x, int y);
Page 6
Problem 2
Write a function with parameters v1, v2, t that returns a linear
acceleration according to the formula:
⃗a = v⃗2−t v⃗1
Function header: float acc(float v1, float v2, float t);
Page 7
Problem 3
Write a function with name fun to print the circumference and
area of a circle of radius entered by user by defining your own
function.
Page 8
Problem 4
Overload previous function in order to find a perimeter and an
area of rectangle with parameters a and b for sides.
Page 9
Problem 5
Write a function to swap two integers without using third vari-
able. Pass parameters as reference.
Page 10
Problem 6
Write a function to return the factorial of a number by defining
a function named ’Factorial’. In case there is no parameter dur-
ing function call, it should return Factorial of 1.
Function header: long long Factorial(int n = 1);
Page 11
Problem 7
Write a function to find gcd of two numbers. Header of function
should be:
int gcd(int num1,int num2)
Page 12
Problem 8
Define two functions to print the maximum and the minimum
number respectively among three numbers entered by user.
Example:
Input: 23 14 56 Input: -4 30 87
Output: Maximum: 56 Output: Maximum: 87
Minimum: 14 Minimum: -4
Page 13
Problem 9
Create a simple calculator which asks user to input two numbers
and do following operations: sum, subtract, product, division.
Use ( sum, subtract, product, division) for function naming.
Page 14
Problem 10
(Math: Triangular numbers) A triangular number is defined
as m(m + 1)/2 for m = 1, 2, c , and so on. Therefore, the first
few numbers are 1, 5, 12, 22, . . . . Write a function with the
following header that returns a triangular number:
int getTriangularNumber(int n)
Write a test program that uses this function to display the first
75 triangular numbers with 5 numbers on each line.
Page 15
Problem 11
(Display even digits in an integer) Write a function with the
following header to display the even digits in order in an integer:
void displayEven(int number)
For example, displayEven(342) displays 4 and 2. Write a test
program that prompts the user to enter an integer and displays
the even digits in it.
Page 16
Problem 12
(Armstrong integer) Write the functions with the following head-
ers:
• int cubeOfDigits(int number);
• bool isArmstrong(int number);
Use cubeOfDigits to implement isArmstrong. An integer is an
Armstrong integer if the sum of the cubes of its digits is equal
to the number itself. Write a test program that prompts the
user to enter an integer and reports whether it is an Armstrong
integer.
Page 17
Problem 13
(Number of days in February) Write a function that returns
the number of days in February using the following header:
int numberOfDaysInFebruary(int year)
Write a test program that displays a table with the numbers
of days in February from year 1985, . . . , upto 1983. Remem-
ber that in the leap year there are 29 days in February.
Page 18
Problem 14
(Financial application: future investment value) Write a pro-
gram that reads in investment amount, annual interest rate,
and number of years, and displays the future investment value
using the following formula:
f utureInvestmentV alue = investmentAmount × (1 +
monthlyInterestRate)numberOf Y ears∗12
For example, if you enter amount 1000, annual interest rate
3.25%, and number of years 1, the future investment value is
1032.98.
Then, write a function that computes future investment value
at a given interest rate for a specified number of years. Use the
following function header:
Page 19
double futureInvestmentValue( double investmen-
tAmount, double monthlyInterestRate, int years)
For example, f utureInvestmentV alue(10000, 0.05/12, 5) re-
turns 12833.59. Write a test program that prompts the user to
enter the investment amount (e.g., 1000) and the interest rate
(e.g., 9%) and prints a table that displays future value for the
years from 1 to 30.
Page 20
Problem 15
(Display ASCII values) Write a function that prints the ASCII
values of the char- acters using the following header:
void printASCII(char ch1, char ch2, int numberPerLine)
This function prints the ASCII values of characters between ch1
and ch2 with the specified number of characters per line. Write
a test program that prints 6 ASCII values per line of characters
from ’a’ to ’m’.
Page 21
Problem 16
(Display matrix) Write a function that displays an n-by-n ma-
trix using the following header:
void printMatrix(int n)
Each element is in range from 0 till 1000, which is generated
randomly. Write a test program that prompts the user to enter
n and displays an n-by-n matrix.
Page 22
Problem 17
(Even Palindrome number) A number is a palindrome if its re-
versal is the same as itself. An even palindrome number is a
number which is even and also a palindrome. Write a program
that displays the first 50 even palindrome numbers. Display 5
numbers per line and align the numbers properly.
2 4 6 8 22
44 66 88 202 212
...
Page 23
Problem 18
Given the following template create functions to calculate se-
ries:
#include <iostream>
#include<cmath>
using namespace std;
double seriesA(int n)
double seriesB(int n)
double seriesC(int n)
double seriesD(int n)
double pi=3.1415;
double e=2.7182;
int main()
{
return 0;
}
Page 24
a. n −2 4 8 −2 i
c. 2π+1 + 2π+2 − 2π+3 ... + 2π+i
π2−i
X
i=1 d.
b. n
v
u n
π uX 2iπ
∗ ie
Y
t
2 ei
i=1 i
Page 25
Problem 19
(Sort three numbers) Write a function with the following header
to display three numbers in increasing order:
#include <iostream>
using namespace std;
void displaySordetedNumber(double a, double b, double c){
}
int main() {
displaySordetedNumber(10.0,20.1,40.2);
return 0;
Write a test program that prompts the user to enter three
numbers and invokes the method to display them in increasing
order.
Page 26
Problem 20
(Convert milliseconds to hours, minutes, and seconds) Write
a function that converts milliseconds to hours, minutes, and sec-
onds using the following header:
string convertMillis(long millis)
{
//TODO
}
The function returns a string as hours:minutes:seconds. For
example, convertMillis(5500) returns a string 0:0:5, convertMil-
lis(100000) returns a string 0:1:40, and convertMillis(555550000)
returns a string 154:19:10. Write a test program that prompts
the user to enter a long integer for milliseconds and displays a
string in the format of hours:minutes:seconds.
Page 27
Problem 21
(Current date and time) Invoking time(0) returns the elapse
time in milliseconds since midnight January 1, 1970. Write a
program that displays the date and time.
Example:
Current date and time is November 17, 2023 16:34:23
Page 28
Problem 22
(Geometry: area of a regular polygon) A regular polygon is
an n-sided polygon in which all sides are of the same length
and all angles have the same degree (i.e., the polygon is both
equilateral and equiangular). The formula for computing the
area of a regular polygon is
n ∗ s2
Area =
4 ∗ tan( Pni
Write a function that returns the area of a regular polygon using
the following header: double area(int n, double side) Write a
main method that prompts the user to enter the number of
sides and the side of a regular polygon and displays its area.
Page 29
Example:
Input:
Enter the number of sides: 5
Enter the side: 6.5
Output:
The area of the polygon is 72.6902
Page 30
Problem 23
(Game: craps) Craps is a popular dice game played in casinos.
Write a program to play a variation of the game, as follows:
Roll two dice. Each die has six faces representing values 1, 2, ...
, and 6, respectively. Check the sum of the two dice. If the sum
is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called
natural), you win; if the sum is another value (i.e., 4, 5, 6, 8, 9,
or 10), a point is established. Continue until you roll either a 7
(you lose) or the same point value (you win).
Example:
• You rolled 5 + 6 = 11 • You rolled 1 + 2 = 3
You win You lose
• You rolled 4 + 4 = 8 • You rolled 3 + 2 = 5
point is 8 point is 5
You rolled 6 + 2 = 8 You rolled 2 + 5 = 7
You win You lose
Page 31
Problem 24
(Emirp) An emirp (prime spelled backward) is a nonpalindromic
prime number whose reversal is also a prime. For example, 17
is a prime and 71 is a prime. So 17 and 71 are emirps. Write a
program that displays the first 100 emirps. Display 10 numbers
per line and align the numbers.
13 17 31 37 71 73 79 97 107 113
149 157 167 179 199 311 337 347 359 389
...
Page 32
Problem 25
(Additive prime) A prime number is called an Additive prime if
the sum of its dig- its is also a prime number. Write a program
that finds the first 25 additive prime numbers
Prime Number Sum of digits
2 2
3 3
... ...
11 2
... ... ...
Page 33
Problem 26
(Binary to Octal) Write a function that returns an octal number
from a binary number. The function header is as follows: int
bin2Octal(int binary) Write a test program that prompts
the user to enter a binary number as a string and displays the
corresponding octal value.
Page 34