SlideShare une entreprise Scribd logo
1  sur  17
Parameter defination.
Types of parameter.
Call by value.
Call by Reference.
Advantages of call by reference.
Disadvantages of call by reference.
    .
PARAMETERS
 A parameter is an intrinsic property of the procedure,
 included in its definition.

 Parameter passing methods are the ways in which
 parameters are transferred between functions when
 one function calls another.

 C++ provides two parameter passing methods--pass-
 by-value and pass-by-reference .
Types of parameter
 Parameters are of two types


    Formal parameters
    Actual parameters
Formal parameters

 Formal parameters are written in the function
 prototype and function header of the definition.

 Formal parameters are local variables which are
 assigned values from the arguments when the function
 is called.
Value Parameter Rules
 Formal parameter is created on function invocation and it is initialized
  with the value of the actual parameter.

 Changes to formal parameter do not affect actual parameter.

 Reference to a formal parameter produces the value for it in the
  current activation record.

 Formal parameter name is only known within its function.

 Formal parameter ceases to exist when the function completes.
Example of Formal Parameters
    Return type     Function name   Formal parameter


                   float CircleArea (float r) {
                   const float Pi = 3.1415;
 Local object
Definition           return Pi * r * r;
                      }

          Return statement         Function body

Actual Parameters

 When a function is called, the values (expressions) that
  are passed in the call are called the arguments or actual
  parameters (both terms mean the same thing).

 The time of the call each actual parameter is assigned to
  the corresponding formal parameter in the function
  definition.
Example of Actual Parameter
                         Actual parameter

cout << CircleArea(MyRadius) << endl



To process the invocation, the function that contains the
insertion statement is suspended and CircleArea()
does its job. The insertion statement is then completed
using the value supplied by CircleArea().
A function can be invoked in 2
ways
Call by value


Call by Reference
Call By Value
 The call by value method copies the values of actual
 parameter in formal parameter.

 That is the function create its own copy of argument
 value and then uses it.
Example of call by value
#include<iostream.h>
int main( )
{ int cube(int);                     Formal Parameter
   int vol,side=7;                        7
     :                      a
    vol=cube(side);
     :                                  value copied
  cout<<vol;
                                 7
Return 0;            side
}                           Actual parameter
int cube(int a)
{ return a*a*a*;
}
Call By Reference

 In call by reference method the called function does
 not create its own copy rather it refers to original value
 only by different name i.e reference.

 When function is called by reference then ,the formal
 parameter become reference or alias to the actual
 parameter in calling function.
Example of Call By Reference
#include <iostream.h>
 void duplicate (int& a, int& b, int& c)
{ a*=2; b*=2; c*=2;
 }
int main ( )
{
  int x=1, y=3, z=7;
  duplicate (x, y, z);
  cout << "x=" << x << ", y=" << y << ", z=" << z;
  return 0;
}
x=2, y=6, z=14
Advantages of passing by
reference:

 It allows us to have the function change the value of
  the argument, which is sometimes useful.
 Because a copy of the argument is not made, it is fast,
  even when used with large structs or classes.
 We can pass by const reference to avoid unintentional
  changes.
 We can return multiple values from a function
Disadvantages of passing by
reference
 It can be hard to tell whether a parameter passed by
  reference is meant to be input, output, or both.
 An argument passed by value and passed by reference
  looks the same. We can only tell whether an argument is
  passed by value or reference by looking at the function
  declaration. This can lead to situations where the
  programmer does not realize a function will change the
  value of the argument.
 Because references are typically implemented by C++ using
  pointers, and dereferencing a pointer is slower than
  accessing it directly, accessing values passed by reference is
  slower than accessing values passed by value.
Parameter passing to_functions_in_c

Contenu connexe

Tendances

Tendances (20)

Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Scope rules : local and global variables
Scope rules : local and global variablesScope rules : local and global variables
Scope rules : local and global variables
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Functions in C
Functions in CFunctions in C
Functions in C
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#
 
This pointer
This pointerThis pointer
This pointer
 
File in C language
File in C languageFile in C language
File in C language
 
Function in c
Function in cFunction in c
Function in c
 

Similaire à Parameter passing to_functions_in_c

Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
ankush_kumar
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 

Similaire à Parameter passing to_functions_in_c (20)

Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Functions
FunctionsFunctions
Functions
 
User Defined Functions in C
User Defined Functions in CUser Defined Functions in C
User Defined Functions in C
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
Unit iv functions
Unit  iv functionsUnit  iv functions
Unit iv functions
 
Function in c
Function in cFunction in c
Function in c
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
 
Python_Unit_2.pdf
Python_Unit_2.pdfPython_Unit_2.pdf
Python_Unit_2.pdf
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
Functions1
Functions1Functions1
Functions1
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
 

Plus de ForwardBlog Enewzletter (10)

Sorting searching
Sorting searchingSorting searching
Sorting searching
 
Pn junction
Pn junctionPn junction
Pn junction
 
Feedback amplifiers
Feedback amplifiersFeedback amplifiers
Feedback amplifiers
 
Oscillators
OscillatorsOscillators
Oscillators
 
Compile time polymorphism
Compile time polymorphismCompile time polymorphism
Compile time polymorphism
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Oo ps
Oo psOo ps
Oo ps
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
Presentation on graphs
Presentation on graphsPresentation on graphs
Presentation on graphs
 

Dernier

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 

Parameter passing to_functions_in_c

  • 1.
  • 2. Parameter defination. Types of parameter. Call by value. Call by Reference. Advantages of call by reference. Disadvantages of call by reference. .
  • 3. PARAMETERS  A parameter is an intrinsic property of the procedure, included in its definition.  Parameter passing methods are the ways in which parameters are transferred between functions when one function calls another.  C++ provides two parameter passing methods--pass- by-value and pass-by-reference .
  • 4. Types of parameter  Parameters are of two types  Formal parameters  Actual parameters
  • 5. Formal parameters  Formal parameters are written in the function prototype and function header of the definition.  Formal parameters are local variables which are assigned values from the arguments when the function is called.
  • 6. Value Parameter Rules  Formal parameter is created on function invocation and it is initialized with the value of the actual parameter.  Changes to formal parameter do not affect actual parameter.  Reference to a formal parameter produces the value for it in the current activation record.  Formal parameter name is only known within its function.  Formal parameter ceases to exist when the function completes.
  • 7. Example of Formal Parameters Return type Function name Formal parameter float CircleArea (float r) { const float Pi = 3.1415; Local object Definition return Pi * r * r; }  Return statement Function body 
  • 8. Actual Parameters  When a function is called, the values (expressions) that are passed in the call are called the arguments or actual parameters (both terms mean the same thing).  The time of the call each actual parameter is assigned to the corresponding formal parameter in the function definition.
  • 9. Example of Actual Parameter Actual parameter cout << CircleArea(MyRadius) << endl To process the invocation, the function that contains the insertion statement is suspended and CircleArea() does its job. The insertion statement is then completed using the value supplied by CircleArea().
  • 10. A function can be invoked in 2 ways Call by value Call by Reference
  • 11. Call By Value  The call by value method copies the values of actual parameter in formal parameter.  That is the function create its own copy of argument value and then uses it.
  • 12. Example of call by value #include<iostream.h> int main( ) { int cube(int); Formal Parameter int vol,side=7; 7 : a vol=cube(side); : value copied cout<<vol; 7 Return 0; side } Actual parameter int cube(int a) { return a*a*a*; }
  • 13. Call By Reference  In call by reference method the called function does not create its own copy rather it refers to original value only by different name i.e reference.  When function is called by reference then ,the formal parameter become reference or alias to the actual parameter in calling function.
  • 14. Example of Call By Reference #include <iostream.h> void duplicate (int& a, int& b, int& c) { a*=2; b*=2; c*=2; } int main ( ) { int x=1, y=3, z=7; duplicate (x, y, z); cout << "x=" << x << ", y=" << y << ", z=" << z; return 0; } x=2, y=6, z=14
  • 15. Advantages of passing by reference:  It allows us to have the function change the value of the argument, which is sometimes useful.  Because a copy of the argument is not made, it is fast, even when used with large structs or classes.  We can pass by const reference to avoid unintentional changes.  We can return multiple values from a function
  • 16. Disadvantages of passing by reference  It can be hard to tell whether a parameter passed by reference is meant to be input, output, or both.  An argument passed by value and passed by reference looks the same. We can only tell whether an argument is passed by value or reference by looking at the function declaration. This can lead to situations where the programmer does not realize a function will change the value of the argument.  Because references are typically implemented by C++ using pointers, and dereferencing a pointer is slower than accessing it directly, accessing values passed by reference is slower than accessing values passed by value.