0

If I only pass an element of an integer array (not the whole array) to a function , is it passed by value or by reference by default ?

for example-

arr[]={2,3,4,5,6}
ans=fun(arr[2])

and fun is some function which multiplies any value by 2 , then will i get the ans as 8 or 4 if i print arr[2] in the main function afterwards? Any help would be appreciated.

4
  • Try with a small example? Commented Feb 21, 2014 at 4:31
  • 1
    value. arr[2] is 4. suppose fun is defined: int fun(int arg) { printf("%d\n",arg); return(arg); }, and fun would print 4. Commented Feb 21, 2014 at 4:34
  • 2
    @CommanderCorianderSalamander C has no reference Commented Feb 21, 2014 at 4:34
  • thouuuught I had the C++ tag clicked... let's not talk about it ^^ Commented Feb 21, 2014 at 4:35

3 Answers 3

5

C has no references.

int fun(int a) {
    a = 0;
}

int main() {
    int arr[5] = {2, 3, 4, 5, 6};
    fun(arr[2]);

    printf("%d\n", arr[2]); //prints 4
    return 0;
}

You can pass a pointer (still passes to the function by value, but that value is the address of the value you are interested in).

int fun(int *a) {
    *a = 0;
}

int main() {
    int arr[5] = {2, 3, 4, 5, 6};
    fun(&arr[2]);

    printf("%d\n", arr[2]); //prints 0
    return 0;
}

I have a suspicion you meant C++. In that case, the function's signature determines whether it is passed by reference.

int fun(int &a) { //by reference, because of &
    *a = 0;
}

int main() {
    int arr[5] = {2, 3, 4, 5, 6};
    fun(arr[2]);

    printf("%d\n", arr[2]); //prints 0
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You question is tagged with c so I will answer in that context:

In the C language, there is no pass-by-reference. We can achieve a similar result as pass-by-reference by passing pointers.

That being said, if you're not passing a pointer to a function, then you're not changing the value within the calling scope.

void func(int a)
{
    a = a * 2; // value of a within the calling scope does not change (pass-by-value)
}

void func2(int *a)
{
    *a = (*a) * 2; // value pointed to by a is changed within the calling scope
}

Comments

0

In C arrays are passed as a pointer to the first element. They are the only element that is not really passed by value (the pointer is passed by value, but the array is not copied). That allows the called function to modify the contents.

When we pass any array as an argument what matters is how we are handling it in the called function. If you are getting the address of the passed value(*val) then it will behave as Pass by reference and if you are getting the value directly in a variable as (val) then it will behave like a pass by value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.