www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Lesson 17: Functions and Arrays Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
In C language, the default mechanism of calling a function is ‘call by value’. When we call a function, say fn(), and pass it a parameter x (argument value) by writing statement fn(x), the calling mechanism puts the value of x at some other place. Then calls the function and gives this value to it. This means a copy of the value is sent to the program. The original x remains untouched and unchanged at its place. The function uses the passed value (that has placed at some other place) and manipulates it in its own way. When the control goes back to the calling program, the value of original x is found intact. This is the call by value mechanism.

Now let’s see what happens when we pass an array to a function. To pass an array to a function, we will tell the function two things about the array i.e. the name of the array and the size. The size of the array is necessary to pass to the function. As the array is declared in the calling function, it is visible there. The calling function knows its size but the function being called does not know the size of the array. So it is necessary to pass the size of the array along with its name. Suppose we have declared a character array in the program by the following statement:

char name[50] ;

We have a function, say reverse that reverses the array elements and displays them. Firstly, we need to write the prototype of the function reverse. We say that this function returns nothing so we use the keyword void in its return type. Secondly, we have to write the parameters this function will get. We write these parameters with their type. Now the prototype of this function will be written as

void reverse ( char [], int ) ;

In the above statement, the brackets [] are necessary. These brackets indicate that an array of type char will be passed to the function. If we skip these brackets and simply write char, it will mean that a single character will be passed to the function. In addition, the second parameter i.e. of type int, is of array's size. Note that in the prototype of the function we have not written the names of the parameters. It is not necessary to write the names of the parameters in function prototype. However, if we write the names, it is not an error. The compiler will simply ignore these names.
Now we will define the function reverse. In the function's definition, we will use the array and variable names. These names are local to this function so we can give these variables a name other than the one used in declaration in the calling program. We write this as below.

void reverse ( char characters [], int arraySize )
// The body of the function.

You are encouraged to try to write the function body for yourself.

Let’s say we have a character array name and a name ‘adnan’ is stored in it. We call the reverse function by passing the array name to it. For this we write

reverse ( name, 100 );

In this function call, we are sending the name of the array to the function i.e. name and the size of the array that is 100. When this call of the function is executed the control goes to the function reverse. The statements in this function are executed which reverses the array and displays it. After this, the control comes back to the main function to the statement next to the function call statement. The return type of the function is void so it does not return any thing. Now in the main, we write the statement:

cout << name;

What will be displayed by this statement? Whether it will be the original name ‘adnan’ or something else. It will display the reversed array. In this instance, we see that whatever the function reverse did to the array ( that was passed to it) is appearing in the calling function. It means that the original array in the calling program has been changed. Here we change (reverse) the order of the characters of array in the function and find that the characters of the array in the calling function are reversed. This means that the called function has not a copy of the array but has the original array itself. Whereas in case of simple variables, a called function uses a copy of variables passed to it in a 'call by value' mechanism, which is by default in case of simple variables. In arrays, the by default mechanism is ‘call by reference’. While passing arrays to a function, we don’t need to use & and * operators, as we use for variables in call by reference mechanism. Thus if we pass an array to a function, the array itself is passed to the function. This is due to the fact that when we declare an array, the name of the array has the address of the memory location from where the array starts. In other words, it is the address of the first element of the array. Thus the name of the array actually represents the address of the first location of the array. Passing the name of array to a function means the passing of the address of the array which is exactly the same as we do in call by reference. So whatever the function does to the array, it is happening in the same memory locations where the array originally resides. In this way, any modifications that the function does to the contents of the array are taking place in the contents of the original array too. This means that any change to the array made by the function will be reflected in the calling program. Thus an important point to remember is that whenever we pass simple variables to a function, the default mechanism is call by value and whenever we pass an array to a function, the default mechanism is call by reference. We know that when we talk about a single element of an array like x [3] (which means the fourth element of the array x), it is treated as simple variable. So if we pass a single element of an array to a function (let’s say like fn ( x [3] ); ), it is just like a simple variable whose copy is passed to the function (as it is a call by value). The original value of the element in the array remains the same. So be careful while passing arrays and a single element of array to functions. This can be well understood from the following example.

Example Code

Suppose we declare an array in the main program and pass this array to a function, which populates it with values. After the function call, we display the elements of the array and see that it contains the values that were given in the function call. This demonstrates that the called function changes the original array passed to it. Following is the code of the program.

/*This program demonstrates that when an array is passed to a function then it is a call by reference and the changes made by the function effects the original array*/
# include <iostream.h>
void getvalues( int [], int) ;
main ( )
int num [10], i ;
getvalues ( num, 10) ; //function call, passing array num
//display the values of the array
cout << “\n The array is populated with values \n” ;
for ( i = 0 ; i < 10 ; i ++)
cout << " num[" << i << "] = " << num[i]<< endl ;
void getvalues ( int num[], int arraysize)
int i ;
for ( i = 0 ; i < arraysize ; i ++)
num[i] = i ;

Here in the function getvalues, we can get the values of the array from user by using the cin statement. Following is the output of the execution of the program.

The array is populated with values
num[0] = 0
num[1] = 1
num[2] = 2
num[3] = 3
num[4] = 4
num[5] = 5
num[6] = 6
num[7] = 7
num[8] = 8
num[9] = 9

NEXT>>>>>Lesson 18. Multidimensional Arrays

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.