In addition to Solution 2:
Void function without a pointer parameters make no sense at all, because you would not be able to return anything. Such void function still can be used, but only for its
side effect, such as with
puts
. But in your case, it makes no sense at all. Let's fix you syntax and try
void power(double x,double b, double a) { a = pow(x,b) }
Look, what happens: first, the calls passes some value to
a
, it is copied on stack and then ignored. Instead, new value is assigned to
a
, which is later discarded. All data and, value and return address are put on
stack, and the call ends, and other functions are called, and the stack memory is reused. All the power :-) goes nowhere.
Note that you cannot afford to keep ignorance on how stack works. All this mechanism of calls and returns, as well as local variables and all function parameters, all that is based on the stack mechanism, which is cast in the architecture of all those CPUs. You need to learn it before you move any further.
Now, next variant, which will work:
void power(double x, double b, double *a) {
*a = pow(x,b); }
double a; power (2, 3, &a);
Will it work? Yes. Is it good enough? No! This is what can really make sense:
double power(double x, double b)
{
return pow(x,b);
}
But of course, it makes real sense if you not just rename
pow(double, double)
; which is the only semantic meaning of this code sample, but do something really useful. :-)
—SA