Passing an argument by value sends a value of the argument to the called function. The called function might modify the value, but the original value from the calling is not changed.
Example of call-by-value |
void test(int); void main( ) { int x = 5; test( ); cout <<x<<endl; } void test(int y) { y = y + 10; //y = 5 + 10 = 15 cout << y; } |
Output |
15 5 |
CALL- BY- POINTER
Passing an argument by address allows the called function to change the value of the argument. It requires the use of pointers. A pointer is a variable that can store a memory address. The memory address of the local variable in the calling function is passed to the called function. The argument is declared as a pointer in the called function declaration and its definition. Use the symbol * to declare a pointer argument.
Example of call-by-pointer |
void test(int *); void main( ) { int x = 5; test( &x); //Passing an address of variable x cout <<x<<endl; } void test(int *y) { *y = *y + 10; //y = 5 + 10 = 15 cout << y; } |
Output |
15 15 |
Call- by-Reference is similar to Call-by-Pointer. The result is still the same, except the pointer notation is different.
Example of call-by-reference |
void test(int &); void main( ) { int x = 5; test( x); //Passing an address of variable x cout <<x<<endl; } void test(int &y) { y = y + 10; //y = 5 + 10 = 15 cout << y; } |
Output |
15 15 |
The programs shown below use call-by-pointer or call-by-reference to swap two values of two variables in the calling function. The results of both techniques are the same.
Example of call-by-pointer |
Example of call-by-Reference |
void test(int *, int *); void main( ) { int x = 5, y 10; cout<<"Before calling the function test\n"; cout<<"The value of x is "<<x<<".\n"; cout<<"The value of y is "<<y<<".\n"; test( &x,&y); //Passing an address of variable x cout<<"After calling the function test\n"; cout<<"The value of x is "<<x<<".\n"; cout<<"The value of y is "<<y<<".\n"; } void test(int *value1,int *value2) { int temp; temp = *value1; *value1 = *value2; *value2 = temp; } |
void test(int &, int &*); void main( ) { int x = 5, y 10; cout<<"Before calling the function test\n"; cout<<"The value of x is "<<x<<".\n"; cout<<"The value of y is "<<y<<".\n"; test( x,y); //Passing an address of variable x cout<<"After calling the function test\n"; cout<<"The value of x is "<<x<<".\n"; cout<<"The value of y is "<<y<<".\n"; } void test(int &value1,int &value2) { int temp; temp = value1; value1 = value2; value2 = temp; } |
Output |
Output |
Before calling the function test The value of x is 5. The value of y is 10. After calling function test The value of x is 10. The value of y is 5. |
Before calling the function test The value of x is 5. The value of y is 10. After calling function test The value of x is 10. The value of y is 5. |
A function can return a pointer.
Syntax Data type * functioname(arguments);
The program below shows how a function returning a pointer.
Function returning a pointer |
int * test(int); int x = 5; void main( ) { int *y; y=test(x); // Function returns a memory address cout <<*y<<endl; } int * test(int z) { int *v; *v = z + 10; // *v = 5 + 10 = 15 return v; //Return the value of v (memory address value) } |
Output |
15 |
When you declare a one-dimensional array as int num[100], the operating system allocates 200 bytes ( 100 x 2) of memory. This memory is fixed or static, you can not adjust the memory as the program is run.
Under a dynamic memory allocation scheme, the amount of storage to be allocated is determined and adjusted as the program is run, rather than being fixed at compile time. Two C++ operator, new and delete, that provide this capability are describes in the table below.
Operator Name |
Description |
new | Reserve the number of bytes requested by the declaration. Return the address of the first reserved location or NULL if sufficient memory is not available. |
delete | Release a block of bytes previously reserved. The address of the first reserved location is passedas an argument to the function |
Syntax for memory allocation Data Type *pointer_variable = new Data Type[number of elements];
For example, the declaration
int *grades = new int[100];
reserves an area (200 bytes) sufficient to store 100 integers and places the address of the first integer into the pointer grades.
Syntax for memory deallocation delete pointer;
For example, the statement
delete grade;
will release the memory allocated for the grade pointer.
In the program below, the actual size of the array that is created depends on the number input by the user. Since the pointer and array name is related, each value in the newly created storage area can be accessed using the standard array notation, such as grade[ i ], or the equivalent pointer notation *(grade + i).
Dynamic array |
void main( ) { int numgrades, i , *grade; cout<<"Enter the number of grade to be processed: "; cin << numgrades; //Create an array grade = new int[numgrades]; //Input grade for(i = 0; i < numgrades; i++) {cout << "Enter a grade: "; cin >> grade[i]; // or cin >> *(grade + i); } cout << "\nAn array was created for "<< numgrades <<" integers\n"; cout << "The values stored in the array are:\n"; for(i=0 ; i < numgrades; i++) {cout << grade [i] << endl;} // or cout << *(grade +i)<<endl; //Release the memory of the array grade delete grade; } |
Output |
Enter the number of grades to be processed: 4 Enter a grade: 90 Enter a grade: 85 Enter a grade: 97 Enter a grade: 70 An array was created for 4 integers The values stored in the array are: 90 85 97 70 |