Parameter passing to_functions_in_c
Upcoming SlideShare
Loading in...5
×
 

Parameter passing to_functions_in_c

on

  • 1,020 views

 

Statistics

Views

Total Views
1,020
Views on SlideShare
1,018
Embed Views
2

Actions

Likes
1
Downloads
23
Comments
0

2 Embeds 2

http://forwardblog.in 1
http://www.forwardblog.in 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Parameter passing to_functions_in_c Parameter passing to_functions_in_c Presentation Transcript

  • 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 objectDefinition 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 parametercout << CircleArea(MyRadius) << endlTo process the invocation, the function that contains theinsertion statement is suspended and CircleArea()does its job. The insertion statement is then completedusing the value supplied by CircleArea().
  • A function can be invoked in 2ways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; 7Return 0; side} Actual parameterint 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 byreference: 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 byreference 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.