Function Argument : Array & Array Element

Passing Value Argument

When we pass a variable or value as a function argument it passes as Call By Value ; means in Stack area of memory , value in caller function is just copied in the called function. Lets see the following code for better understanding

Call By Vlaue

Here main function is the caller function and Func is the called function . Now , we pass value of variable as a function argument to the Func function , and the formal parameter of this Func function which is ‘Number‘ variable take that value .

What is happen in here is , the value of ‘Send_Number‘ variable which is 1234 , is just copied and stored in ‘Number‘ variable of the called function . This process is simply called Call by Value .

But if we pass an array as a function parameter something different is happen in here . This time the process is not Call by value but its Call by reference . Let’s see what’s that really mean .

Passing Array Argument

Let’s consider the following sample of code

ArrayArgumentFunction

Few things to notice : while we pass an array we just simply write the name of the array – just A , right ? Then in the called function , in Func we write simply A[ ] .

 However , we simply set the iteration condition till 5 but we can do some smart approach here . We can determine the size of the array and use it in our code. What we can do is to find the size of an array using sizeof – a built in function which gives the size of the parameter in byte unit.

For example , if we write sizeof( char ) – it will return 1 byte . So if we write sizeof(A) – it will return the value of the array which is 20 bytes . As we know modern C compiler allocate memory space for integer is 4 bytes and as we declare 5 integer , it will return 20 bytes .

Now here we want the size of array and not how much it takes spaces in memory . So in order to find the length of the array we simply divide the total size of the array by size of the one element of the array .

So we can measure by  sizeof( A ) / sizeof( A[ 0 ] ) – it will return the length of the array here which is 5 . Now , let’s modify our code 

replace-e1504700786862.png

OutPut_ArrayArgumentFunction

So here we can see that, size of the array is 20 and as each of the element is 4 in size , the length of the whole array we count 5 , then we simply pass the value of length of the array as a function argument and we normally get the desire output.

Now let’s change something in here , we will find the length of the array not in caller function but in called function . So here we change something like this 

size-in-caller-function1-e1504714097395.png

output_two-e1504703869736.png

Ops! It seems there is a problem . Why total size of array is 4 but not 20 and why just one element is printed out only. 

Function Argument : Call By Reference

In call by value process , value in one function is just copied to another function . So While we pass a value or variable as a function argument, a copy of that value or variable is just passed but not the original value . But when we pass whole array with all of its element , whole array is not send as a copy . We simply pass the base address of the array element.

We must remember that while we pass array as a function argument , we simply write like this Func(A) ~ by sending like this of an array, not means to send the whole of it but the address of the first member or element of the array because only writing A is similar to &A[ 0 ] – we’re familiar with &A[ 0 ] ~which means the address of the first element of the array. So , while we passing array as an argument , we simply send the base address of that array .

It is logically understandable that , an array can be large in size , so its not necessary to make copy such a large array . What we can do is to send the base address of the array and we know that we good to go to iterate the whole array if we’ve the base address of the array .

Again if we pass only array element as a function argument then it operated as call by value . So when we pass a variable or array element as a function argument – value just copied but when we pass whole array as a function argument – only we pass the base address of the array .

In the called function here which is Func – the formal parameter is A[ ] . When we call this function we send the base address of an array as actual parameter which is A or &A[ 0 ] . When we run our code , compiler implicitly convert the A[ ] to *A . Now , it seems more understandable that as formal parameter is treated as a pointer variable – its then norm to hold or assign the address of other variable – here which is &A[ 0 ] or A . So,

void Func ( int A[ ] ) ;

void Func ( int  *A ) ;

both are exactly same in manner. In Call by reference , as We pass the original data , so we can modify data from called function , but in Call by value , as We pass data as copy then it won’t effect to the caller function even though we change data in the caller function. Lets consider the following code for better understanding 

Call by value …

call by value

result of call by value

As we can see the value of Data which is 12 , is the same Before and After calling Double function . We change the value of Data in the called function which is Double function and we print it in the main function . After we print the value of Data variable , it remains the same value . So , we can clearly understand that when we send data as a function argument , it just passes as a copy and not  originally . So even we modify our data in the called function , it’s not effect on the caller function .

Call by reference …

call by ref

res of call by ref

Here we’ve an array of integer which is  A[ ] = {1, 2, 3, 4, 5} and we pass the whole array as a function argument and modify it in the called function . We multiply 2 times with each element and when we print our array of integer , instead of printing 1 2 3 4 5 , it prints 2  4  6  8  10 .  So our data has been modified in the called function and it effect on the caller function . This is called Call by reference . Here , the real data has been send but not copied and how it happens – we already discussed it earlier and that is we pass the base address of the array not fully .

Call by Value :

  • Any changes made are not reflected in the actual arguments
  • It works locally
  • Actual arguments remain preserved and no chance of modification accidentally.

Call by Reference :

  • Any changes made are reflected in the actual arguments.
  • Actual arguments will not be preserved.
  • It works globally

Reference :