2
$\begingroup$

If I'm passing parameters as call by value-return and call a void function, will the local values within the function still be copied to the corresponding values in the caller function even though the called function doesn't return? For example:

int x = 4;

void foo(int a) {
    a = 8 + 8;
}

int main() {
    foo(x);
    print x;
}

Will the value printed be 4 or 16? My hunch is that because foo never returns, the local value for a within the stack frame of foo never gets copied back to the value for x in the stack frame for main, but I wanted to double check.

$\endgroup$
4
  • 1
    $\begingroup$ Why do you think foo() never returns? $\endgroup$
    – Maxpm
    Nov 3 '17 at 8:26
  • $\begingroup$ Of course, this will depend a bit on the language/compiler implementation (and specification); but if void functions are allowed, the returning has to be implemented in some sensible way, implicitely without a return statement. $\endgroup$ Nov 3 '17 at 12:11
  • $\begingroup$ @Maxpm ohhh wait it just implicitly returns nothing, right? $\endgroup$ Nov 3 '17 at 22:52
  • $\begingroup$ This is a question about the C programming language, and so off-topic here. I suggest running the program and seeing what happens. $\endgroup$ Nov 10 '17 at 23:13
0
$\begingroup$

It will print 4 because sending by value will change the copy not the original variable which is x in your example.

$\endgroup$
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.