-2

I was studying about passing by reference. It made me wonder what would happen in the following example (Written in pseudo-C which supports "by reference"):

int foo(int a) {
    a = 5;
    return a * 2;
}

int main() {
    int a = 1;
    a = foo(a);
    printf("%d",a);
    return 0;
}

What should be printed? if we only did foo(a); without assigning into a then we would get 5. but what would be printed when assigning? should it be 5 or 10?

jxh
  • 69,070
  • 8
  • 110
  • 193
vesii
  • 2,760
  • 4
  • 25
  • 71
  • What happened when you ran it? – Oliver Charlesworth Feb 26 '19 at 22:29
  • 4
    Since you're imagining a psuedo-C that has pass-by-reference, you'll have to decide for yourself what the semantics associated with argument passing will be. I don't see how you can expect *us* to answer that, at least not in a way appropriate for SO. – John Bollinger Feb 26 '19 at 22:31
  • Until you give semantics for `pasudo-C` language for "by reference" variable passing, there is no one that can answer that question. – KamilCuk Feb 26 '19 at 22:33
  • 2
    Why do you have to hypothesize about a pseudo-C? Why not ask about, say, `a = foo(&a);` with `int foo(int* a) { *a = 5; return *a * 2; }`? Or perhaps ask about C++ where you could have `int foo(int& a)`? – jamesdlin Feb 26 '19 at 22:38
  • Removed the `C` tag. I thought there is only one way possible and if not it will be more helpful to see explanation for the difference, benefits and maybe a real PL which uses it. I don't think it's a question "opinion-based" but sorry if it is. – vesii Feb 26 '19 at 23:01
  • First a is set to 5. Then 10 is returned. Then a is set to 10. There is no ambiguity. – user253751 Feb 26 '19 at 23:21
  • @vesii: You are only going to confuse readers and yourself by using C syntax and declare it to be pseudocode, and that the syntax should be interpreted as pass by reference. In my experience, pseudocode that wants to specify a function with pass by reference parameters use a declarative syntax with textual descriptions like: `foo(integer a : in/out, string b : in, string c : out) ...` – jxh Feb 26 '19 at 23:26
  • Possible duplicate of [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – jxh Feb 26 '19 at 23:36
  • Although the close reason is too broad, I had voted to close the question as a duplicate. See the linked question. The assignment occurs after the function call completes, so there is no ambiguity, as was commented by @immibis. – jxh Feb 26 '19 at 23:38

1 Answers1

1

Since you have a = foo(a); in your main() function, a will contain the result returned by foo(a). foo(a) will always return 10 no matter what a is.

C does not support pass by reference. Changing a = foo(a); to just foo(a); would mean a would retain the value it had before it was passed to foo(), so it would be 1.

One variation of C that supports pass by reference is C++. In C++, you could write foo() as:

int foo(int &a) {
    a = 5;
    return a * 2;
}

The int &a syntax is used to denote that the parameter will be passed by reference. Now, foo will assign 5 to the referenced variable, but still always return 10.

In this case a = foo(a); will result in a having the value 10, and foo(a); alone will result in a having the value 5.

jxh
  • 69,070
  • 8
  • 110
  • 193
  • 1
    He's talking about "pseudocode" in a C-like form, not C. But pseudocode is entirely subjective. I flagged his question for being opinion-based. – Dustin Nieffenegger Feb 26 '19 at 22:41
  • @DustinNieffenegger: I agree that precise interpretation of pseudocode may be subjective. However, it is the lingua franca for specifying algorithms, so it should be possible to reason about it without being considered opinion-based. – jxh Feb 26 '19 at 22:45