Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 473
Andrew Backes Avatar asked Sep 08 '12 02:09

Andrew Backes


People also ask

How do you pass a variable as a reference?

Pass-by-reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the argument by using its reference passed in. The following example shows how arguments are passed by reference.

How does pass by reference work in C?

Passing by by reference refers to a method of passing the address of an argument in the calling function to a corresponding parameter in the called function. In C, the corresponding parameter in the called function must be declared as a pointer type.

What happens when you pass a variable by reference?

Defining Pass by Reference Pass means to provide an argument to a function. By reference means that the argument you're passing to the function is a reference to a variable that already exists in memory rather than an independent copy of that variable.

Can you pass variables by reference in C?

To pass a value by reference, argument pointers are passed to the functions just like any other value. So accordingly you need to declare the function parameters as pointer types as in the following function swap(), which exchanges the values of the two integer variables pointed to, by their arguments.


2 Answers

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.

like image 54
Adam Mihalcin Avatar answered Nov 09 '22 23:11

Adam Mihalcin


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.

like image 38
Ed Heal Avatar answered Nov 10 '22 01:11

Ed Heal