0

I don't really see any difference, with passing pointers to functions and calling a function by reference. Am I right

#include <stdio.h>

int multi;

int multiplication(int *a, int *b){
    multi = (*a) * (*b);

    return multi;
}

int main()
{
    int X = 2, Y=3;
    multiplication(&X, &Y);

    printf("%d", multi);
    return 0;
}
  • what are you comparing? I only see one function. – Joseph D. Apr 05 '18 at 12:20
  • What do you mean by `calling by reference`? Passing a pointer to a pointer? For your example, you can't assign a new pointer to `a` or `b`. Is that what you're asking? – Millie Smith Apr 05 '18 at 12:21
  • In the multiplication function i passed the parameters using pointers, then called upon the function using reference, is it right to say so? – Kayondo Benard Apr 05 '18 at 12:22
  • 2
    Do you really mean _calling a function by reference_ or rather _passing parameters by reference_? Calling a function by reference would involve a [function pointer](https://www.cprogramming.com/tutorial/function-pointers.html) in C where “references” are commonly implemented using pointers. – Melebius Apr 05 '18 at 12:24
  • No, you called the function here `multiplication(...);`. That's the *call*. `multi` is just another variable. – StoryTeller - Unslander Monica Apr 05 '18 at 12:25
  • 3
    If you look at old C books, especially before 2003, they will likely discuss "passing by reference" with examples very much like what you're showing. And they don't make any apologies about it. Nowadays if you say that's "passing by reference," people who've been brought up in the C++ & Java age will authoritatively say that you're sadly uninformed and no, that's not passing by reference, it's "simulating" passing by reference. But there was a day when even learned, respected programmers would say that's "passing by reference". – phonetagger Apr 05 '18 at 12:26
  • Thanks for the good duplicate, should have checked myself before writing an answer .... –  Apr 05 '18 at 12:30
  • Basically was reading somewhere on tutorial point and got stack somewhere because I couldn't differentiate between the two, but the code is somehow similar. If i call the function **multiplication** and use the addresses **&** and when defining the function i pass the parameters as pointers. – Kayondo Benard Apr 05 '18 at 12:31
  • Why would you pass pointers here? You never use them to update the referred `int`. Sometimes examples can be made too simple... – underscore_d Apr 05 '18 at 12:34
  • @phonetagger even the C89 standard doesn't mention pass or call by reference, but explains in the "function call" section that parameters can be modified, but don't influence the arguments -- and this is indexed at the beginning of the document with the keywords "call by value". So, it wasn't precise back then as well, it's just people obviously cared less :) –  Apr 05 '18 at 12:42
  • @FelixPalmen It is clear that the distinction has become more strict in recent years. If you look at K&R's *The C Programming Language* (2nd edition at least), you'll see that it specifically says C is a "call by value" language, specifically differentiating it from "call by reference" languages. But then even they go on to refer to constructs such as `a[i]` and `*(a+i)` being ***references*** to the same object. And surely you've heard, maybe even said yourself, that the `*` operator ***dereferences*** a pointer. That implies that a pointer is, in concept at least, a form of reference. – phonetagger Apr 05 '18 at 13:13

1 Answers1

1

From the example code you show, you obviously mean pass by reference, not call by reference.

There is no pass by reference in C, it's always pass by value. Of course, you can get the effect of pass by reference by passing a pointer to something. The pointer is your reference. It's again passed by value, but you use that value to access some other object.

Languages that have pass by reference will typically use pointers to implement it.