Source One Educational Society
 
Home | OS Install | MS-Office | Photoshop | SQL | Networking | HTML | Java | Model Papers | FAQ's | Health Tips | News | Jobs | Fun SMS
C ++ Tutorial
o

INTRODUCTION

  Instructions for use
o BASICS OF C++
  Structure of a program
  Variables Data Types
  Declaration of variables
  Initialization of variables
  Constants
  Operators
  Basic Input / Output
o CONTROL STRUCTURES
  Control Structures
  Functions (I)
  Functions (II)
  Overloaded functions
o COMPOUND DATA TYPES
  Arrays
  Character Sequences
  Pointers
  Dynamic Memory
  Data structures
  Other Data Types
o OBJECT ORIENTED PROGRAMMING
  Classes (I)
  Classes (II)
  Friendship and inheritance
  Polymorphism
o ADVANCED CONCEPTS
  Templates
  Namespaces
  Exceptions
  Type Casting
  Preprocessor directives
o C++ STANDARD LIBRARY
  Input/Output with files
 
 
C++ Language Tutorial
 

CONTROL STRUCTURES

Functions (II)
 
Arguments passed by value and by reference.
 
Until now, in all the functions we have seen, the arguments passed to the functions have been passed by value. This means that when calling a function with parameters, what we have passed to the function were copies of their values but never the variables themselves. For example, suppose that we called our first function addition using the following code:
 
int x=5, y=3, z;
z = addition ( x , y );
 
What we did in this case was to call to function addition passing the values of x and y, i.e. 5 and 3 respectively, but not the variables x and y themselves.
 
function
 
This way, when the function addition is called, the value of its local variables a and b become 5 and 3 respectively, but any modification to either a or b within the function addition will not have any effect in the values of x and y outside it, because variables x and y were not themselves passed to the function, but only copies of their values at the moment the function was called.
 
But there might be some cases where you need to manipulate from inside a function the value of an external variable. For that purpose we can use arguments passed by reference, as in the function duplicate of the following example:
 
Example:
Output:
// passing parameters by reference
#include <iostream>
using namespace std;
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
 
The first thing that should call your attention is that in the declaration of duplicate the type of each parameter was followed by an ampersand sign (&). This ampersand is what specifies that their corresponding arguments are to be passed by reference instead of by value.
 
When a variable is passed by reference we are not passing a copy of its value, but we are somehow passing the variable itself to the function and any modification that we do to the local variables will have an effect in their counterpart variables passed as arguments in the call to the function.
 
arguments passed
 
To explain it in another way, we associate a, b and c with the arguments passed on the function call (x, y and z) and any change that we do on a within the function will affect the value of x outside it. Any change that we do on b will affect y, and the same with c and z.
 
That is why our program's output, that shows the values stored in x, y and z after the call to duplicate, shows the values of all the three variables of main doubled.
 
If when declaring the following function:
 
void duplicate (int& a, int& b, int& c)
we had declared it this way:
void duplicate (int a, int b, int c)
 
i.e., without the ampersand signs (&), we would have not passed the variables by reference, but a copy of their values instead, and therefore, the output on screen of our program would have been the values of x, y and z without having been modified.
 
Passing by reference is also an effective way to allow a function to return more than one value. For example, here is a function that returns the previous and next numbers of the first parameter passed.
 
Example:
Output:
// more than one returning value
#include <iostream>
using namespace std;
void prevnext (int x, int& prev, int& next)
{
prev = x-1;
next = x+1;
}
int main ()
{
int x=100, y, z;
prevnext (x, y, z);
cout << "Previous=" << y << ", Next=" << z;
return 0;
}
Previous=99, Next=101
 
Default values in parameters.
 
When declaring a function we can specify a default value for each of the last parameters. This value will be used if the corresponding argument is left blank when calling to the function. To do that, we simply have to use the assignment operator and a value for the arguments in the function declaration. If a value for that parameter is not passed when the function is called, the default value is used, but if a value is specified this default value is ignored and the passed value is used instead. For example:
 
Example:
Output:
// default values in functions
#include <iostream>
using namespace std;
int divide (int a, int b=2)
{
int r;
r=a/b;
return (r);
}
int main ()
{
cout << divide (12);
cout << endl;
cout << divide (20,4);
return 0;
}

5

6

 
As we can see in the body of the program there are two calls to function divide. In the first one:
 
divide (12)
 
we have only specified one argument, but the function divide allows up to two. So the function divide has assumed that the second parameter is 2 since that is what we have specified to happen if this parameter was not passed (notice the function declaration, which finishes with int b=2, not just int b). Therefore the result of this function call is 6 (12/2).
 
In the second call:
 
divide (20,4)
 
there are two parameters, so the default value for b (int b=2) is ignored and b takes the value passed as argument, that is 4, making the result returned equal to 5 (20/4).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Home  |  Aboutus  |  Post Your Content  |  Tell a Friend  |  Contact us  |  Sitemap
© 2009 365edu. All Rights Reserved. Site by 365edu. Best view in 1024x768 resolution.