# Input problem when using user-defined functions.

• 10-16-2010
843
Input problem when using user-defined functions.
I'm scratching my head over this one. My input for feet is always recorded as 0 when I print within the calc() function, but it is correct when printed in the main() function. What's going on?

Code:

```#include <stdio.h> float input (void) {     float x;     scanf("%f", &x);     getchar();     return x; } float calc () {     float y, feet, inches;     y = 30.48 * feet + 30.48 * (inches / 12);     printf("%f and %f", feet, inches);     printf("%f", y);     return y; } void output () {     int meters;     float centimeters;     printf("\nThe value is equivalent to %d meters and %.2f centimeters.", meters, centimeters); } int main (void) {     int meters;     float feet, inches, metric, centimeters;     char ans;     printf("This program converts a length in feet and inches into meters and centimeters.\n"); do {     printf("Insert length in feet: ");     feet = input ();     printf("Insert length in inches: ");     inches = input ();     metric = calc (feet, inches);     meters = metric / 100;     centimeters = (metric - meters) * 100;     output (meters, centimeters);     printf("\nWould you like to try again? [Y/N]");     scanf("%c", &ans);     getchar(); } while (ans == 'y' || ans == 'Y');     return 0; }```
• 10-16-2010
laserlight
In the calc function, the local variables feet and inches were not given an initial value before use.
• 10-16-2010
ssharish2005
I'm a bit confused, calc is a function which takes no parameters but your calling it with two parameters from main?

ssharish
• 10-16-2010
843
Thanks! When do we need to initialize the values? Other functions seem to work fine without initialization.
• 10-16-2010
843
Quote:

Originally Posted by ssharish2005
I'm a bit confused, calc is a function which takes no parameters but your calling it with two parameters from main?

ssharish

From what I read, using () implies unknown number of arguments, whereas having no argument is denoted by (void).

Correct me if I'm wrong.
• 10-16-2010
laserlight
Quote:

Originally Posted by 843
From what I read, using () implies unknown number of arguments, whereas having no argument is denoted by (void).

Yeah, but here you have a function definition, so the number of parameters is known to be zero. If you used those empty parentheses with a function declaration that was not also a function definition, then indeed the number of parameters would be unknown at that point.
• 10-16-2010
843
I initialized all the values and rearrange some functions but now I'm getting a bunch of 'incompatible' error messages. :(

Code:

```#include <stdio.h> float input (void) {     float x;     scanf("%f", &x);     getchar();     return x; } float calc (float feet, float inches) {     return 30.48 * feet + 30.48 * (inches / 12); } int meters (float metric) {     return metric / 100; } float centimeters (float metric, int meters) {     return (metric - meters) * 100; } void output (int meters, float centimeters) {     printf("\nThe value is equivalent to %d meters and %.2f centimeters.", meters, centimeters); } int main (void) {     int m;     float feet, inches, metric, cm;     char ans;     printf("This program converts a length in feet and inches into meters and centimeters.\n"); do {     printf("Insert length in feet: ");     feet = input ();     printf("Insert length in inches: ");     inches = input ();     metric = calc (feet, inches);     m = meters (metric);     cm = centimeters (metric, meters);     output (meters, centimeters);     printf("\nWould you like to try again? [Y/N]");     scanf("%c", &ans);     getchar(); } while (ans == 'y' || ans == 'Y');     return 0; }```
EDIT: Ah, I see the problem now...
• 10-16-2010
843
Quote:

Originally Posted by laserlight
...function declaration that was not also a function definition...

Could you please clarify this part?
• 10-16-2010
ssharish2005
>From what I read, using () implies unknown number of arguments, whereas having no argument is denoted by (void).
Yeah i know that and your right.

This is interesting, the compiler wouldn't complain if you send parameters to a function which dosn't take any parameters.

ssharish
• 10-16-2010
843
Got it! I didn't realize the functions' arguments are private to those functions only.

Code:

``` #include <stdio.h> float input (void) {     float x;     scanf("%f", &x);     getchar();     return x; } float calc (float x, float y) {     return 30.48 * x + 30.48 * (y / 12); } int meters (float x) {     return x / 100; } float centimeters (float x, int y) {     return x - y * 100; } void output (int x, float y) {     printf("\nThe value is equivalent to %d meters and %.2f centimeters.", x, y); } int main (void) {     int m;     float feet, inches, metric, cm;     char ans;     printf("This program converts a length in feet and inches into meters and centimeters.\n"); do {     printf("Insert length in feet: ");     feet = input ();     printf("Insert length in inches: ");     inches = input ();     metric = calc (feet, inches);     m = meters (metric);     cm = centimeters (metric, m);     output (m, cm);     printf("\nWould you like to try again? [Y/N]");     scanf("%c", &ans);     getchar(); } while (ans == 'y' || ans == 'Y');     return 0; }```
• 10-16-2010
laserlight
Quote:

Originally Posted by 843
Could you please clarify this part?

Take your most recent code as an example. You could have written:
Code:

```float calc(); /* ... */ int main(void) {     /* ... */     metric = calc(feet, inches);     /* ... */ } /* ... */ float calc(float x, float y) {     return 30.48 * x + 30.48 * (y / 12); }```
But then you might as well just leave the parameters in the function prototype (which is a declaration that is not also a definition).
• 10-16-2010
843
What's the point of a prototype since you can just declare the function and its definition?
• 10-16-2010
laserlight
Quote:

Originally Posted by 843
What's the point of a prototype since you can just declare the function and its definition?

You may wish to use the function across different source files, so its prototype may go into a header file.