0% found this document useful (0 votes)
10 views9 pages

CSI Pract7 8

This document provides practical exercises for C++ programming, focusing on function calling methods, specifically call by value and call by reference. It includes example programs for exchanging variable values and calculating factorial using recursion. Key differences between call by value and call by reference are also outlined.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

CSI Pract7 8

This document provides practical exercises for C++ programming, focusing on function calling methods, specifically call by value and call by reference. It includes example programs for exchanging variable values and calculating factorial using recursion. Key differences between call by value and call by reference are also outlined.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

STES’

Sinhgad College of Arts, Science and


Commerce (Jr.College)
Ambegaon (Bk)
Bifocal Computer Science
Mrs. Taraka Ghamande
(Teacher & Moderator In Computer Science)
CSI –Chapter 3

C++ Programming
Practical No. 7
Aim
: To study the function calling methods.

Problem statement
Write a program in c++ to exchange the variables
using the call by reference method of calling
functions.
Enter the program and verify the execution of the
same on the computer.
C++ Program to exchange the contents of two variables using call by value

#include<iostream.h>
void main( )
{
int a,b;
void swap(int , int );
cout<<”Enter two values “;
cin>>a>>b;
cout<<”The Original values are “<<a<<”\t”<<b<<endl;
swap(a,b);
cout<<”The Exchanged values are “<<a<<”\t”<<b<<endl;
}
void swap( int x, int y)
{
int t;
t=x;
x = y;
y = t;
}
Sinhgad_JR_CS_DEPT_TG_C++_ 4
C++ Program to exchange the contents of two variables using call by
reference.

#include<iostream.h>
void swap(int * , int *);
void main( )
{
int a,b;

cout<<”Enter two values “;


cin>>a>>b; 34 45
cout<<”The Original values are “<<a<<”\t”<<b<<endl; 34 45
swap(&a,&b);
cout<<”The Exchanged values are “<<a<<”\t”<<b<<endl;
}
void swap( int *x, int *y)
{
int t;
t=*x; //t=value at the address stored in x t=34
*x = *y; // *x---a= *y—b--45
*y = t; //*y --b= 34 Sinhgad_JR_CS_DEPT_TG_C++_ 5
}
Call by Value & Call by reference
•In Call by value method original value is not modified whereas, in Call by
reference method, the original value is modified.

•In Call by value, a copy of the variable is passed whereas in Call by reference, a
variable itself is passed.

•In Call by value, actual and formal arguments will be created in different
memory locations whereas in Call by reference, actual and formal arguments
will be created in the same memory location.

•Call by value is the default method in programming languages like C++, PHP,
Visual Basic NET, and C# whereas Call by reference is supported only Java
language.

•Call by Value, variables are passed using a straightforward method whereas


Call by Reference, pointers are required to store the address of variables.
Practical No. 8
Aim: To study the recursive function in C++
function.

Problem statement
Write a program in c++ to calculate the factorial
of a number using the recursive function.
Enter the program and verify the execution of the
same on the computer.
// Factorial of n = 1*2*3*...*n
#include <iostream.h>
int factorial(int);
void main()
{ int n, result;
cout << "Enter a non-negative number: "; Output
cin >> n; Enter a non-negative number: 4
Factorial of 4 = 24
result = factorial(n);
cout << "Factorial of " << n << " = " << result;
}
int factorial(int n)
{
if (n > 1) Logic of the program
{ return n * factorial(n - 1); }
else
{
return 1;
}
}
Recursive Function

•A function that calls itself is known as a recursive function.

•The recursion continues until some condition is met.(stopping


condition)

•To prevent infinite recursion, if...else statement (or similar


approach) can be used where one branch makes the recursive call
and the other doesn't.

You might also like