c, I wonder how the value returned from the c++ function is transferred to the main function

Asked 2 years ago, Updated 2 years ago, 112 views

int number(int * x)
{
    Return a value of return *x; // a
}

int main()
{
    int a= 1;
    int b;
    b= number(&a);
}

In this case, if you copy the value of a to the variable b and return it, right?

But I wonder when and how the copy will be returned.

In my opinion, considering the return keyword as a variable,

Should I think about it this way?

return c c++

2022-09-20 13:12

1 Answers

The first line of functions in language C consists of:

return value format function name (argument list);

In int number (int*x), the return value type is int. A function can return a specific value to the location where the function was called within the function, which is called a return value. If you don't want to use the return value, you can set the return value format to void. If you write a value after the return inside the function, the value becomes a return value and returns it to the place where the function was called.

Therefore, the main function is When number(&a) is called, the number function is executed, and *x, the value after the return inside the number function, becomes a return value and returns to the position of number(&a) in the main statement. Because the return value is int type, the number 1, which is the value of *x, is returned to the number (&a) position of the main function Eventually, b=number(&a) becomes b=1.


2022-09-20 13:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.