Passing Arrays Inside Function

Passing Arrays Inside Function

Passing arrays inside function is a new concept for the beginner of the C programming language.Howerver, It is a best practise in programming passing arrays inside the function.Simply it is understood that array is the contagious memory allocation into the memory i.e. RAM. Array represents the chunks of data into the memory. Whenever we try to operate the data into the array, it requires a lot of time to work on the whole sets of data into the main program.Due to which it increases the load in main program. This scenario is not recommended inside the main program. Instead of this, passing arrays inside function is the solution of the problems that we should do now. Passing arrays inside function can be done by two ways.Array elements can be passed to a function by calling the function by value, or by reference. In the call by value we pass values of array elements to the function, whereas in the call by reference, we pass addresses of array elements to the function. These two calls are illustrated below:

/*Demonstration of call by value */

A simple example of passing arrays inside function

[c] void main()
void display(int m); //prototype of a function
{
int i;
int mark[ ] = {52, 55, 76, 66, 77, 78, 95};

for (i = 0; i <= 6 ; i++)
display (markfi]); //calling function
}
void display (int m)
{
printf(“%d”,m); //it prints all the array elements
}
[/c]

And here’s the output… 52 55 76 66 77 78 95
Here, we are passing an individual array element at a time to the function display( ) and getting it printed in the function display(). Note that since at a time only one element is being passed, this element is collected in an ordinary integer variable m, in the function display().Let’s have a look on the figure.

Passing Arrays Inside Function
Passing Arrays Inside Function

And now the call by reference.

/* Demonstration of call by reference */

Another example of passing arrays inside function

[c] void main()
void display(int *n);//function prototyping
{
int i;
int mark[ ] = {52, 55, 76, 66, 77, 78, 95};
for (i = 0; i <= 6 ; i++)
display (&mark[i]);
}
void display (int *n ) {
printf (“%d\t”,n);
}

[/c]

And here’s the output…
52, 55, 76, 66, 77, 78, 95
Here, we are passing addresses of individual array elements to the function display( ). Hence, the variable in which this address is collected (n) is declared as a pointer variable. And since n contains the address of array element, to print out the array element we are using the ‘value at address’ operator (*).

In these examples,Passing Array Elements to Functions are demonstrated,if you have any problems regarding the passing array elements to functions then feel free to contact me for the solution.Here is the link of the book which you can follow which is very helpful Recomended Book For Programming in C and another book for the beginners is as follow:Another recomended book for C programming Beginner

This entry was posted in Uncategorized. Bookmark the permalink.