SlideShare a Scribd company logo
1 of 14
Function Call
by
Value & Reference
By: Arijit Dhali
Contents
2
User Defined Function
Common Method of Swapping
Integers
Call by Value
Call by Reference
Differences
Conclusion
User Defined Function
A user-defined function is a function provided by the
user of a program or environment, in a context where
the usual assumption is that functions are built into
the program or environment.
Syntax:
return_type function_name (parameter list)
{
Function Body
return statement;
}
3
Common Method of Swapping Two Integers
4
#include<stdio.h>
main()
{
int x, y, z;
printf (“n Enter the value of x: ");
scanf ("%d", &x);
printf (“n Enter the value of y: ");
scanf ("%d", &y);
z = x;
x = y;
y = z;
printf ( "n After swap : x = %d, y = %d n", x, y);
return 0;
}
Using Third Variable
Source Code:
Storing Value
Storing Value
Swap using 3rd
Variable
Declaring Variable
5
Output:
Values Get Swapped
6
Call by Value
The call by value method of passing argument to a function, copies
the actual value of an argument into the formal parameter of the
function. In this case, changed made to parameter inside the function
had no effect on argument.
7
#include<stdio.h>
void swapvalue(int x,int y);
int main()
{
int a,b;
printf(“n Enter two numbers: ");
scanf("%d %d",&a,&b);
printf(“n Before Swapping a=%d, b=%d n",a,b);
swapvalue(a,b);
printf(“n After Swapping a=%d, b=%d n",a,b);
return 0;
}
void swapvalue(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
return;
}
Source Code:
Swap using 3rd
Variable
Function Definition
Function Declaration
Declaring Variable
Function Calling
Passing Variable
8
Output:
Values Doesn’t Swap
9
Call by Reference
The Call by Reference method of passing arguments to a function copies
the address of an argument into the formal parameter. Inside the function,
the address is used to access the actual argument used in the call. This
means that changes made to be parameter affect the passed argument. To
pass the value by reference, argument pointers are passed to the functions
just like any other value. So, accordingly you need to declare the function
parameters as pointer types as in the following swap(), which exchanges
the values of the two integer variables pointed to by its arguments.
10
#include<stdio.h>
void swapreference(int *x,int *y);
int main()
{
int a,b;
printf(“n Enter two numbers: ");
scanf("%d %d",&a,&b);
printf(“n Before Swapping a=%d, b=%d",a,b);
swapreference(&a,&b);
printf(“n After Swapping a=%d, b=%d",a,b);
return 0;
}
void swapreference(int *x, int *y)
{
int temp=0;
temp=*x;
*x=*y;
*y=temp;
return;
}
Source Code:
Swapping
address using
3rd Variable
Function Calling
Passing Address
Function Definition along
with defining Pointer
Function Declaration
Declaring Variable
11
Output:
Values Get Swapped
› The function is called by
directly passing value of
variable as argument.
› We need to declare a
general variable as function
argument.
› Changes made in the formal
parameters will not be
reflected in the actual
parameters. It is because
formal and actual
parameters are stored in
different locations.
Call by Value v/s Call by Reference
› The function is called by
directly passing address of
variable as argument.
› We need to declare pointer
variable as function
argument.
› Changes made in the formal
parameters will be reflected
in the actual parameters. It is
because formal and actual
parameters are stored in
same locations.
12
Call by Value Call by Reference
13
Conclusion
As the Conclusion , If we want to pass only the value of variable use ‘call by
value’ approach, and if you want to see the change in the original value of the
variable then use ‘call by reference’ approach.
GRACIAS
14
Hope to see you again!!!

More Related Content

What's hot (20)

Function in c
Function in cFunction in c
Function in c
 
file handling c++
file handling c++file handling c++
file handling c++
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
File in C language
File in C languageFile in C language
File in C language
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
FUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdfFUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdf
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
File in c
File in cFile in c
File in c
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Function in c
Function in cFunction in c
Function in c
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Functions in C
Functions in CFunctions in C
Functions in C
 

Similar to Types of function call

Similar to Types of function call (20)

Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
chapter-7 slide.pptx
chapter-7 slide.pptxchapter-7 slide.pptx
chapter-7 slide.pptx
 
Function in c
Function in cFunction in c
Function in c
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Array Cont
Array ContArray Cont
Array Cont
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
C function
C functionC function
C function
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
User Defined Functions in C
User Defined Functions in CUser Defined Functions in C
User Defined Functions in C
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.ppt
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers
 
C programs
C programsC programs
C programs
 

More from ArijitDhali

Signal Constellation, Geometric Interpretation of Signals
Signal Constellation,  Geometric Interpretation of  SignalsSignal Constellation,  Geometric Interpretation of  Signals
Signal Constellation, Geometric Interpretation of SignalsArijitDhali
 
Stack Queue SubRoutine
Stack Queue SubRoutineStack Queue SubRoutine
Stack Queue SubRoutineArijitDhali
 
Overviewing the techniques of Numerical Integration.pdf
Overviewing the techniques of Numerical Integration.pdfOverviewing the techniques of Numerical Integration.pdf
Overviewing the techniques of Numerical Integration.pdfArijitDhali
 
Motorola 68020.pdf
Motorola 68020.pdfMotorola 68020.pdf
Motorola 68020.pdfArijitDhali
 
Stereotactic Radiosurgery in Brain Metastases.pdf
Stereotactic Radiosurgery in Brain Metastases.pdfStereotactic Radiosurgery in Brain Metastases.pdf
Stereotactic Radiosurgery in Brain Metastases.pdfArijitDhali
 
Active Filters.pdf
Active Filters.pdfActive Filters.pdf
Active Filters.pdfArijitDhali
 
Wideband Frequency Modulation.pdf
Wideband Frequency Modulation.pdfWideband Frequency Modulation.pdf
Wideband Frequency Modulation.pdfArijitDhali
 
Celebrity Problem.pdf
Celebrity Problem.pdfCelebrity Problem.pdf
Celebrity Problem.pdfArijitDhali
 
SSBSC Single Side Band - Suppressed Carrier Compressed
SSBSC Single Side Band - Suppressed Carrier CompressedSSBSC Single Side Band - Suppressed Carrier Compressed
SSBSC Single Side Band - Suppressed Carrier CompressedArijitDhali
 
Biodiversity Hotspots in India
Biodiversity Hotspots in IndiaBiodiversity Hotspots in India
Biodiversity Hotspots in IndiaArijitDhali
 
LTI Systems - With/Without Memory
LTI Systems - With/Without MemoryLTI Systems - With/Without Memory
LTI Systems - With/Without MemoryArijitDhali
 
RLC Series Resonance
RLC Series ResonanceRLC Series Resonance
RLC Series ResonanceArijitDhali
 
Bivariate Discrete Distribution
Bivariate Discrete DistributionBivariate Discrete Distribution
Bivariate Discrete DistributionArijitDhali
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's AlgorithmArijitDhali
 
Conditional Probability
Conditional ProbabilityConditional Probability
Conditional ProbabilityArijitDhali
 
Isomerism of Transition Metal Complex
Isomerism of Transition Metal ComplexIsomerism of Transition Metal Complex
Isomerism of Transition Metal ComplexArijitDhali
 
Space Solar Power
Space Solar PowerSpace Solar Power
Space Solar PowerArijitDhali
 
Power Series - Legendre Polynomial - Bessel's Equation
Power Series - Legendre Polynomial - Bessel's EquationPower Series - Legendre Polynomial - Bessel's Equation
Power Series - Legendre Polynomial - Bessel's EquationArijitDhali
 

More from ArijitDhali (20)

Signal Constellation, Geometric Interpretation of Signals
Signal Constellation,  Geometric Interpretation of  SignalsSignal Constellation,  Geometric Interpretation of  Signals
Signal Constellation, Geometric Interpretation of Signals
 
Stack Queue SubRoutine
Stack Queue SubRoutineStack Queue SubRoutine
Stack Queue SubRoutine
 
Overviewing the techniques of Numerical Integration.pdf
Overviewing the techniques of Numerical Integration.pdfOverviewing the techniques of Numerical Integration.pdf
Overviewing the techniques of Numerical Integration.pdf
 
Motorola 68020.pdf
Motorola 68020.pdfMotorola 68020.pdf
Motorola 68020.pdf
 
Stereotactic Radiosurgery in Brain Metastases.pdf
Stereotactic Radiosurgery in Brain Metastases.pdfStereotactic Radiosurgery in Brain Metastases.pdf
Stereotactic Radiosurgery in Brain Metastases.pdf
 
Active Filters.pdf
Active Filters.pdfActive Filters.pdf
Active Filters.pdf
 
Wideband Frequency Modulation.pdf
Wideband Frequency Modulation.pdfWideband Frequency Modulation.pdf
Wideband Frequency Modulation.pdf
 
Celebrity Problem.pdf
Celebrity Problem.pdfCelebrity Problem.pdf
Celebrity Problem.pdf
 
SSBSC Single Side Band - Suppressed Carrier Compressed
SSBSC Single Side Band - Suppressed Carrier CompressedSSBSC Single Side Band - Suppressed Carrier Compressed
SSBSC Single Side Band - Suppressed Carrier Compressed
 
Biodiversity Hotspots in India
Biodiversity Hotspots in IndiaBiodiversity Hotspots in India
Biodiversity Hotspots in India
 
LTI Systems - With/Without Memory
LTI Systems - With/Without MemoryLTI Systems - With/Without Memory
LTI Systems - With/Without Memory
 
RLC Series Resonance
RLC Series ResonanceRLC Series Resonance
RLC Series Resonance
 
Bivariate Discrete Distribution
Bivariate Discrete DistributionBivariate Discrete Distribution
Bivariate Discrete Distribution
 
Solar Cell
Solar CellSolar Cell
Solar Cell
 
Barcode Decoder
Barcode DecoderBarcode Decoder
Barcode Decoder
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
 
Conditional Probability
Conditional ProbabilityConditional Probability
Conditional Probability
 
Isomerism of Transition Metal Complex
Isomerism of Transition Metal ComplexIsomerism of Transition Metal Complex
Isomerism of Transition Metal Complex
 
Space Solar Power
Space Solar PowerSpace Solar Power
Space Solar Power
 
Power Series - Legendre Polynomial - Bessel's Equation
Power Series - Legendre Polynomial - Bessel's EquationPower Series - Legendre Polynomial - Bessel's Equation
Power Series - Legendre Polynomial - Bessel's Equation
 

Recently uploaded

Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEselvakumar948
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARKOUSTAV SARKAR
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxmaisarahman1
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilVinayVitekari
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 

Recently uploaded (20)

FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 

Types of function call

  • 1. Function Call by Value & Reference By: Arijit Dhali
  • 2. Contents 2 User Defined Function Common Method of Swapping Integers Call by Value Call by Reference Differences Conclusion
  • 3. User Defined Function A user-defined function is a function provided by the user of a program or environment, in a context where the usual assumption is that functions are built into the program or environment. Syntax: return_type function_name (parameter list) { Function Body return statement; } 3
  • 4. Common Method of Swapping Two Integers 4 #include<stdio.h> main() { int x, y, z; printf (“n Enter the value of x: "); scanf ("%d", &x); printf (“n Enter the value of y: "); scanf ("%d", &y); z = x; x = y; y = z; printf ( "n After swap : x = %d, y = %d n", x, y); return 0; } Using Third Variable Source Code: Storing Value Storing Value Swap using 3rd Variable Declaring Variable
  • 6. 6 Call by Value The call by value method of passing argument to a function, copies the actual value of an argument into the formal parameter of the function. In this case, changed made to parameter inside the function had no effect on argument.
  • 7. 7 #include<stdio.h> void swapvalue(int x,int y); int main() { int a,b; printf(“n Enter two numbers: "); scanf("%d %d",&a,&b); printf(“n Before Swapping a=%d, b=%d n",a,b); swapvalue(a,b); printf(“n After Swapping a=%d, b=%d n",a,b); return 0; } void swapvalue(int x,int y) { int temp; temp=x; x=y; y=temp; return; } Source Code: Swap using 3rd Variable Function Definition Function Declaration Declaring Variable Function Calling Passing Variable
  • 9. 9 Call by Reference The Call by Reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to be parameter affect the passed argument. To pass the value by reference, argument pointers are passed to the functions just like any other value. So, accordingly you need to declare the function parameters as pointer types as in the following swap(), which exchanges the values of the two integer variables pointed to by its arguments.
  • 10. 10 #include<stdio.h> void swapreference(int *x,int *y); int main() { int a,b; printf(“n Enter two numbers: "); scanf("%d %d",&a,&b); printf(“n Before Swapping a=%d, b=%d",a,b); swapreference(&a,&b); printf(“n After Swapping a=%d, b=%d",a,b); return 0; } void swapreference(int *x, int *y) { int temp=0; temp=*x; *x=*y; *y=temp; return; } Source Code: Swapping address using 3rd Variable Function Calling Passing Address Function Definition along with defining Pointer Function Declaration Declaring Variable
  • 12. › The function is called by directly passing value of variable as argument. › We need to declare a general variable as function argument. › Changes made in the formal parameters will not be reflected in the actual parameters. It is because formal and actual parameters are stored in different locations. Call by Value v/s Call by Reference › The function is called by directly passing address of variable as argument. › We need to declare pointer variable as function argument. › Changes made in the formal parameters will be reflected in the actual parameters. It is because formal and actual parameters are stored in same locations. 12 Call by Value Call by Reference
  • 13. 13 Conclusion As the Conclusion , If we want to pass only the value of variable use ‘call by value’ approach, and if you want to see the change in the original value of the variable then use ‘call by reference’ approach.
  • 14. GRACIAS 14 Hope to see you again!!!