# problem with functions

• 10-10-2012
Sixx
problem with functions
Hey guys! im having some problems with functions. Just started going over them today in class and i was doing great throughout the semester until now! totally lost and confused! we were asked to make a program that contains 10 different functions for conversion and i cant even figure the first one out! This is what i have so far but i cant seem to get it to compile correctly...

Code:

```#include<stdio.h> float milesToKilo(float input, float output) {     output=0;        output=input*1.609344;     printf("Thats %f kilometers!", output);     return output; } float kiloToMiles(); float inchesToCent(); float centToInches(); float milesToFeet(); float feetToMiles(); float cupsToQuarts(); float quartsToCups(); float litersToQuarts(); float quartsToLiters(); int main (void) {     int choice=0;     float input=0;     float output=0;     printf("Convert Miles to Kilometers................1\n");     printf("Convert Kilometers to Miles................2\n");     printf("Convert Inches to Centameters..............3\n");     printf("Convert Centameters to Inches..............4\n");     printf("Convert Miles to Feet......................5\n");     printf("Convert Feet to Miles......................6\n");     printf("Convert Cups to Quarts.....................7\n");     printf("Convert Quarts to Cups.....................8\n");     printf("Convert Liters to Quarts...................9\n");     printf("Convert Quarts to Liters..................10\n");     printf("What would you like to do?\n");     scanf("%d", &choice);     switch (choice)         {            case 1:             printf("How many miles?\n");             scanf("%f", &input);             output=milesToKilo();                        printf("That would be %f Kilometers!\n", milesToKilo);             break;         case 2:             printf("Kilometers to Miles!\n");             break;         case 3:             printf("Inches to Centameters!\n");             break;         case 4:             printf("Centameters to Inches!\n");             break;         case 5:             printf("Miles to Feet!\n");             break;         case 6:             printf("Feet to Miles!\n");             break;         case 7:             printf("Cups to Quarts!\n");             break;         case 8:             printf("Quarts to Cups!\n");             break;         case 9:             printf("Liters to Quarts\n");             break;         case 10:             printf("Quarts to Liters\n");             }    return 0; } ```
any help would be great! ive been stuck for hours!
• 10-10-2012
Salem
output=milesToKilo();
printf("That would be %f Kilometers!\n", milesToKilo);
• 10-10-2012
Matticus
In addition to Salem's correction - You're printing the result twice. Either print the message within the function, or after the function has been called. (If you're returning a value from the function, then you probably want the latter.)

Code:

```float milesToKilo(float input, float output) {     output=0;        output=input*1.609344;     printf("Thats %f kilometers!", output);     return output; } ...     case 1:         printf("How many miles?\n");         scanf("%f", &input);         output=milesToKilo();                    printf("That would be %f Kilometers!\n", milesToKilo);         break;```
• 10-10-2012
Sixx
So what exactly is your correction Salem? Thanks matticus, ill remove it from the function :-)
• 10-10-2012
Matticus
Quote:

Originally Posted by Sixx
So what exactly is your correction Salem? Thanks matticus, ill remove it from the function :-)

Look at the two lines in red in my post #3 above.
The one inside the function is correct, matching the %f format specifier with the variable "output."
The one in the switch statement is not correct, as you don't have a variable called "milesToKilo."

You can use the "milesToKilo()" function (with parenthesis) in the "printf()" statement also.
• 10-10-2012
Matticus
Hmm, there's more to be said about how you're declaring and using your functions. Perhaps a brush up is in order.
Functions in C - Cprogramming.com