Lab Manual Functions
Lab Manual Functions
Lab- 09 Manual
Lab Instructor: Syed Wajid Ali
Department of AI
Email: [email protected]
Lab 09: Functions and Recursion
Lab Rubrics
Functions
1. Objective:
Learn how to write user defined functions which do not return value OR can return
single value.
2. Scope:
3. Useful Concepts:
NOTE: The program should always have a main function whether it has other function
or not. Without the main function the program will not do anything as the program
always starts from the main function.
The program may need to repeat the same piece of code at various places or it may
require repeating same task again and again. The program may become very large and
messy if we do not divide it into sub programs (functions). The piece of code that is
executed repeatedly is stored in a separate function, and is called when and where it is
required.
1. Built- in Functions:
Examples:
1. The function show neither accepts any argument nor returns any
value.
Function name No parameters
void indicates no return
type void show ( );
5. The function sum accepts two integer type arguments and returns an
integer value.
Function name int indicates integer
int indicates integer parameters
return type int sum (int,int);
Functions Definition:
A set of statements that explains what a function does is called function
definition. The function definition can be written at the following places.
Before main function
After main function
Function declaration is not required if the function definition is written before main
function. Function declaration is compulsory if function definition is written after main
function. The function definition consists of two parts:
Functions Header:
Functions Body:
The set of statements written inside the function are called function body. The
body of the function appears after function header and the statements are enclosed in
curly braces { }.
Syntax:
Return-type Function-Name
Function header
(Parameters)
{
Statement 1;
Statement 2;
Function body
Statement n;
return statement (optional);
}
Functions Call:
The statement that activates a function is called function call. A function is called with its
name followed by required parameters in parenthesis “( ) “. If there are many
parameters, these are separated by comas. If there are no parameter, empty parenthesis
are used. Following steps show the process of function call.
In pass by value the value of actual parameter is copied to formal parameters. If the function
makes any changes in formal parameters, it does not affect the values of actual parameter.
In pass by reference method, the memory address of actual parameter is passed to the
function. The formal parameter becomes second name for actual parameter. Formal parameter
does not occupy separate memory, it means both actual and formal parameters refer to the same
memory location. If the function makes any changes in formal parameters, the change is also
reflected in actual parameter.
A function can return a single value. The return type in function declaration indicates the type of
value returned by a function. The keyword return is used to return the value back to the calling
function. When the return statement is executed in a function, the control moves back to the
calling function along with the returned value.
Program # 2: Display a simple message using the function and call a function
using for loop.
Coding by using for loop:
#include <iostream>
using namespace std;
void displayMessage()
{
cout<<"Hello from the function diplayMessage. \n";
}
int main()
{
cout<<"Hello from main.\n";
for(int i=1;i<=10;i++)
{
displayMessage();
}
cout<<"Back in function main again.\n";
return 0;
}
Program # 3: Display a simple message using the function and call a function
using while loop.
Coding by using while loop:
#include <iostream>
using namespace std;
void displayMessage()
{
cout<<"Hello from the function diplayMessage. \n";
}
int main()
{
cout<<"Hello from main.\n";
int i=1;
while (i<=10)
{
displayMessage ();
i++;
}
Lab Manual: Programming Fundamentals
cout<<"Back in function main again.\n";
return 0;
}
int main()
{
cout<<"I am passing 5 and 6 to displyValue. \n";
displayValue(5,6);
cout<<"Now back in main function.\n";
return 0;
}
Lab Manual: Programming Fundamentals
Function Prototype
Program #6:
Coding:
#include <iostream>
using namespace std;
void first();
void second();
int main()
{
cout<<"I am starting in function main. \n";
first();
second();
cout<<"Back in function main again.\n";
return 0;
}
void first()
{
cout<<"Hello from first.\n";
}
void second()
{
cout<<"Hello from second.\n"; }
Program #7:Write a program to show one number by using
the function Prototype.
coding:
#include <iostream>
using namespace std;
void displayValue(int);
int main()
{
cout<<"I am passing 5 to displyValue. \n";
displayValue(5);
cout<<"Now back in main function.\n";
return 0;
}
void displayValue(int num)
Lab Manual: Programming Fundamentals
{
cout<<"The number is " <<num <<endl;
}
Program #10:
Write a program that contains a function that takes two arguments of
integer types and displays their quotient.
#include <iostream>
using namespace std;
int quotient(int, int); // Function Prototype
int main()
{
int one,two, quot;
cout<<"Enter two integers: ";
cin>>one>>two;
quot = quotient(one, two); // Function Call
cout<<"Quotient = "<<quot<<endl; return 0;
}
int quotient(int one, int two) // Function Definition
{
int q = one/two;
return q;}
Program #11:
C++ Program to Calculate Square of a Number using Functions
#include<iostream>
using namespace std;
int calculateSquare(int number)
{
return number * number;
}
int main()
{
int number, square;
Lab Manual: Programming Fundamentals
cout << "\nPlease Enter Number to find Square of it = ";
cin >> number;
square = calculateSquare(number);
cout << "\nThe Square of the Given " << number << " = " << square;
return 0;
}
Program #12:
C++ Program to Calculate Square of ten numbers using Functions.
#include<iostream>
using namespace std;
for (number=1;number<=10;number++)
{
square = calculateSquare(number);
cout << "\nThe Square of the Given " << number << " = " << square;
}
return 0;
}
Program #13:
Write a program that inputs two integers in main ( ) and passes the integers
to swap( ) by value. The swap( ) exchanges the values. The main( ) should
display the values before and after swapping.
#include <iostream>
using namespace std;
void swap_value(int, int);
Lab Manual: Programming Fundamentals
int main()
{
int one,two;
cout<<"Enter two integers: ";
cin>>one>>two;
cout<<"Before Swap: "<<one<<" and "<<two<<endl;
swap_value(one, two);
cout<<"After Swap: "<<one<<" and "<<two<<endl;
return 0;
}
Pass by Reference
Program #14:
#include <iostream>
using namespace std;
void f(int & x)
{
x = x+2;
cout << x << endl;
Lab Manual: Programming Fundamentals
}
// Driver code
int main()
{
int a = 5;
cout << a << endl;
f(a);
cout << a << endl;
}
Program #15: Write a program that inputs two integers in main ( ) and
passes the integers to swap( ) by reference. The swap( ) exchanges the
values. The main( ) should display the values before and after swapping.
#include <iostream>
using namespace std;
void swap_ref(int&, int&);
int main()
{
int one,two;
cout<<"Enter two integers: ";
cin>>one>>two;
cout<<"Before Swap: "<<one<<" and "<<two<<endl;
swap_ref(one, two);
cout<<"After Swap: "<<one<<" and "<<two<<endl;
return 0;
}
Lab Manual: Programming Fundamentals
void swap_ref(int& one, int& two)
{
cout<<"Entering Swap: "<<one<<" and "<<two<<endl;
int temp;
temp = one;
one = two;
two = temp;
cout<<"Exiting Swap: "<<one<<" and "<<two<<endl;
}
Recursion
Recursion is a programming and mathematical concept where a function or algorithm calls
itself as part of its execution. In simpler terms, it's a process where a problem is solved by
breaking it down into smaller, similar sub-problems, and the solution to each sub-problem is
found by calling the same function recursively.
Lab Manual: Programming Fundamentals
Program #16:
Fibonacci Series Using Recursion in C++
Fibonacci number series is the sequence of numbers such that each number
is the sum of the two preceding ones starting from zero(0) and one(1).
#include <iostream>
using namespace std;
int fibonnaci(int x)
{
if((x==1)||(x==0))
{
return(x);
}
else
{
return(fibonnaci(x-1)+fibonnaci(x-2));
}
}
int main()
{
int x , i=0;
cout << "Enter the number of terms of series : ";
cin >> x;
cout << "\nFibonnaci Series : ";
while(i < x)
{
cout << " " << fibonnaci(i);
i++;
}
return 0;
Lab Manual: Programming Fundamentals
}
Program #17:
Factorial Program Using Recursion In C++
Factorial is the product of an integer and all other integers below it. For
example, the factorial of 5 (5!) is equal to 5x4x3x2x1 i.e. 120.
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factorial of " << n << " = " << fact(n);
return 0;
}
int fact(int n)
{
if(n > 1)
return n * fact(n - 1);
else
return 1;
}
Program #18:
Program To Calculate Number Power Using Recursion In C++
Lab Manual: Programming Fundamentals
In this program, we will calculate the power of the number using the
recursion approach where the base and exponent is taken as input by the
user.
#include <iostream>
using namespace std;
int main()
{
int base, power, result;
return 0;
}
Tasks
Task 1: Write a Program in c++ in which you need to implement a function Factorial() by
using Recursion.
For Example: Factorial of 5 is 1*2*3*4*5=120
Task 2: Write a Program in c++ in which you need to implement a function () by using
Recursion.
Task 3: Basic Pass by Value Calculation
Write a function calculateArea that takes two parameters, length and width, and returns the
area of a rectangle. The function should not modify the original values of length and width.
Task:
Implement a calculateArea function that demonstrates pass by value.
Call the function and print the calculated area without affecting the original
variables.
Example: