Passing to Functions

October 27, 2015 Posted by WithU Technologies
There are two ways to pass arguments to function. When a function is being called, the arguments are either pass by reference or by value.

And by default you are carry forwarding the value of the arguments to the function.

Passing by value to Functions: 

#include <iostream>
#include <stdlib.h>
using namespace std;

void newF(int a){

    a = 41;

    cout<<"Change "<<a<<endl;   //Changes to the parameter within the function and it outputs 41

}

int main(){

    int r = 12;

      newF(r); //copies the argument’s actual value into the functions formal parameter

      cout<<r; //Outputs 12. Thus no effect on original argument

}

·         The program started with the main function and then it initialises an integer variable named r and assigns a value of 12 to it.

·         After that it calls the newF() function and at that time a copy of argument is passed to the function, and thus the original argument is not modified by the function.

·         So, when we output r, the value remains unchanged and provides an output of 12.

Hence, we come to a conclusion that passing by value method copies the argument’s actual value into the functions formal parameter. 

Thus we can make changes to the parameter within the function without having any effect on the argument itself.

Passing by Reference to Function:

#include <iostream>
#include <stdlib.h>
using namespace std;

void newF(int *a){   //Pointer is used as parameter

    *a = 41;  

}

int main(){

    int r = 12;

      newF(&r); // Variable directly passed to the function using address-of-operator (&)

      cout<<r; // Arguments value changed by accessing it via pointer. Thus outputs 41.

}

·         The program started with the main function and initialises integer r like before and assign it a value of 12.

·         Then it calls newF function with ampersand operator along with r.

·         In newF() function a pointer is taken as parameter to access the location of argument directly.

·         Thus in output the function has actually changed the argument’s value, as accessed it via the pointer.

Hence the method by reference copies the argument’s reference into the formal parameter. Then within the function the reference or address is used to access the actual argument used during the call.

Thus if any change will take place within the parameter, the effect will also take place upon the argument.

Passing Arrays to Functions:

#include <iostream>
#include <stdlib.h>
using namespace std;

void newArray(int a[], int y){

      for(int x=0;x<y;x++){

            cout<<"Output "<<x<<" is "<<a[x]<<endl;

      }

}

int main(){

    int r[5] = {10,15,12,14,56};

      newArray(r,5);

}

/*

 Output

Output 1 is 15

Output 2 is 12

Output 3 is 14

Output 4 is 56

 */

·         As always the program starts with the main function which then initialise an array named r with 5 as its memory storage size and then it assigns 5 values in its 5 locations.

·         After that it calls the newArray function where the first argument is the array itself without any square brackets and then we define the y value i.e. the size or length of that array.

·         The array function named newArray() with two parameters, first one initialised the array name a and in the next parameter we declared the size or length of that array.

·         So, the corresponding values match and then it initialises the for loop which will create the index locations of that array and with generation of each loop instance as the index element, it also output the respective array defined for it.