Siya - 8 months ago 43
C Question

# How do I return a double?

1. Create a function called functionXYZ that returns a DOUBLE. This function should also accept a DOUBLE as input.

2. functionXYZ should take a DOUBLE as an input, times this value by 10, and then return the result.

3. In your MAIN method, test out your newly created function. Write some simple code that will use functionXYZâ€¦ In your MAIN, create a new DOUBLE variable and set it equal to 5.7. Pass this variable to your new function functionXYZ and save the returned value into a new DOUBLE variable. Print out this new variable that contains the returned value from functionXYZ Now you can verify that your function is working correctly.

So far, I have (transferred from a comment and formatted):

``````double functionXYZ (double data)
{
return *10;
}

int main(void)
{
double val = 5.7;
double val = 2.4;
double val = 9.5;
double val = 3.7
double result = functionXYZ (val);
double result1 = functionXYZ (val1);
double result2 = functionXYZ (val2);
double result3 = functionXYZ (val3);
printf(" Your total is: %if \ n " result1);
printf(" Your total is: %if \ n " result2);
printf(" Your total is: %if \ n " result3);
printf("n\Press ENTER to exit ");
return 0
}
``````

Ok, you basically have it right, but you are not yet multiplying the argument value. So your function should be

``````double functionXYZ (double data)
{
return data * 10;
}
``````

The type before the function name determines its 'return type' (what type of variables can be expected to be returned by this function). In this case, it is `double`, so as long as you use the `return` keyword with a double value, you are correctly returning a double.

Edit

There are also a number of bugs in your `main()` function. To get this to compile correctly, you must do the following:

Make sure variables have unique names; You have `double val` several times. That means you are trying to declare to the compiler that there is a `double` variable called `val` more than once, which is an error.

Change the line(s) that look like this

``````printf(" Your total is: %if \ n " result1);
``````

to

``````printf(" Your total is: %if \n ", result1);
``````

Note adding the missing comma. Also removing the space between the \ and the n is not a compile bug but it is an obvious typo.

Make sure that every time you refer to a variable, it is one you have actually defined. So for example

``````double result2 = functionXYZ (val2);
``````

calls your function with a variable called `val2`, except at no point in your code to you actually define `val2`.

Source (Stackoverflow)