Andrew Backes
Andrew Backes

Reputation: 1904

Pass by reference in C: how is result variable initialized?

I am new to C, and I have to make a mini calculator program (this is homework, but I'm not looking for the answer, just a little more understanding). Basically, one function must look like this:

int add(double d, double dd, double *result);

It will return a 0 if there are no errors, and -1 if an error occurred (in the case of addition, there wouldn't be many errors - but division for example, divide by 0 would be an error).

A user has to input two numbers into the terminal, those numbers then get used as the parameter values in the add method. What I don't understand is what is result initially when the method is called? Is it just null? And why would I want to return 0 or -1 and not result instead? For example:

double result;
returnValue = add(2.0, 5.0, &result); 

Obviously I'll get 7 as the result, but how will I print that out without returning the result? returnValue is 0, so I know there were no errors, so now I need to print result.

Upvotes: 5

Views: 207

Answers (3)

J Nance
J Nance

Reputation: 21

The code you have written does not assign a value to result when the variable is declared. Thus it contains some random number. It is obviously bad to use this number to do anything, as it will cause your program to have unpredictable results. This would be what is referred to as an uninitialized variable error.

Your code does not refer to the variable before it is assigned, so I am not saying you have a bug. I am simply answering the question of what result contains before it is assigned to. If you want it to have a particular value, you can declare it like this:

double result = 7;

and it would always have some predefined value. Again, there is no need to do this in your case, I'm just saying you could if you wanted an always defined value.

Upvotes: 0

Adam Mihalcin
Adam Mihalcin

Reputation: 14478

C doesn't have pass by reference. You can pass in a pointer, which is what you're doing here, but C only has pass by value.

Now to your actual questions:

What I don't understand is what is result initially when the method is called? Is it just null?

No, the value of result is undefined before the add function is called. You have no guarantees whatsoever if you try to use the value of result before assigning to it, either by assigning to it in the function where it's declared or by assigning to it in add with code like *result = d + dd.

For that matter, a double can never be null. Null is a possible pointer value, not a possible floating-point number.

And why would I want to return 0 or -1 and not result instead?

If you were to return result directly, you'd have to have some kind of distinguished "calculation failed" return value, which is kind of messy and leaves the caller to check the result before using it. This way forces the caller of add to notice that there is status code as the return value, and if the caller wants to ignore it then they can (although you shouldn't ignore status codes).

Obviously I'll get 7 as the result, but how will I print that out without returning the result?

Ed Heal is right to suggest printf. If you're using Linux or Mac OS X, though, I'd also recommend running man printf from the terminal - it's often more convenient than opening a web browser.

Upvotes: 7

Ed Heal
Ed Heal

Reputation: 60027

Thank you for your honesty. It is refreshing.

The line should read

returnValue = add(2.0, 5.0, &result);

And to print out the result look up printf - That will do the trick.

Upvotes: 2

Related Questions