Home > Blockchain >  Why is my function returning 1 and not the variable value?
Why is my function returning 1 and not the variable value?

Time:03-02

I am new to C and learning it for my University course. I am learning about functions and have to create a function that doesn't have any printf or scanf in it, just a function that calculates how many days are in a week.

int main(days)
{

    int weeks;

    printf("\nPlease enter a number of weeks: ");
    scanf("%i", &weeks);

    weekstodays(weeks);

    printf("\nThere are %i days in %i weeks.\n", days, weeks);
    return 0;
}
int weekstodays(weeks){

    int days;

    days = weeks * 7;
    printf("%i", days);

    return(days);

}

Whenever I build and run this, the main function outputs 1 day, but the weekstodays function outputs the desired result. (The printf in the weekstodays function is just to see the value of days) Does anyone know why the weekstodays function is not returning the day variable correctly?

CodePudding user response:

You are not using the returned value of the function in this statement

weekstodays(weeks);

Write

int days = weekstodays(weeks);

Pay attention to that the function declaration is incorrect

int weekstodays(weeks){

Write

int weekstodays(int weeks){

Also place one more function declaration before main.

Pay attention to that also the declaration of main is incorrect

int main(days)

According to the C Standard the function shall be declared either like

int main( void )

or

int main( int argc, char * argv[] )
  • Related