Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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[] )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading