Bloomberg LP Interview Question
InternsCountry: United States
Interview Type: Phone Interview
1) We pass by value when you don't want the value to be changed on client side which passes its value. Value is affected locally.
When we pass by reference, the value corresponding to the reference gets changed locally and globally(changed on client side too)
2) Yes, Stricture can be passed by Value and reference
3)No. Basically array value always have a reference to a object. And its is not a primitive datatype. So Array can only be passed by reference.
Note:Fixed-size arrays (size fixed at compile-time) can be part of a struct, and then they are passed by value.
I mean declarations like int foo[5]; inside a struct.
Credit: Anonymous
Pass by Value -- When a variable is passed by value. It's value is copied into the local address space that is made available to the function.Whatever changes the code in the function makes to this variable are done inside it's local space.
- isandesh7 January 24, 2013e.g.void swap(int a,int b){ int temp; temp=a;a=b;b=temp; }
call to this funtion swap(x,y) willl not swap the values of x and y.
Pass by reference -- When you pass by reference the compiler implicitly does this when it encounters the use of the variable in the function code. It will refer to the original variable that was passed. and the change to the value of that variable will be done at the address location it was originally placed at.
e.g. void swap(int &a,int &b){int temp; temp=a;a=b;b=temp;}
swap(x,y) ... this will interchange the values of x and y.
Pass by address ... In pass by address you do the compilers job. You explicitly catch the original address of the variable in a special variable called as a pointer. then you use the derefrencing operator to change the value in the address.
e.g. void swap(int *a, int *b){ int temp; temp = (*a); *a=*b; *b=temp; }
swap(&x,&y) .... this will interchange the values of x and y.
An array is a sequence of addresses.. Passing it by value would have been counter intuitive and would require huge amount of overhead. Hence arrays are always passed by reference.
You could define an array inside of a structure and then pass it. that would pass it by value.