Second user-defined function returns garbage value?

Posted by mintyfresh on Stack Overflow See other posts from Stack Overflow or by mintyfresh
Published on 2012-09-21T21:18:43Z Indexed on 2012/09/21 21:38 UTC
Read the original article Hit count: 107

Filed under:
|

I have been teaching myself C programming, and I've come to a difficult point with using variables across functions.

When, I compile this program and run it, the function askBirthYear returns the correct value, but sayAgeInYears returns either 0 or a garbage value. I believe it has something to do with how I used the variable birthYear, but I'm stumped on how to fix the issue.

Here is the code:

#include <stdio.h>
#include <stdlib.h>

int askBirthYear(int);
void sayAgeInYears(int);
int birthYear;

int main(void)
{    askBirthYear(birthYear);
     sayAgeInYears(birthYear);
     return EXIT_SUCCESS;
}

void askBirthYear(int birthYear)
{
    printf("Hello! In what year were you born?\n");
    scanf("%d", &birthYear);
    printf("Your birth year is %d.\n", birthYear);
    return birthYear;
}

void sayAgeInYears(int birthYear)
{
    int age;
    age = 2012 - birthYear;
    printf("You are %d years old.\n", age);
}

© Stack Overflow or respective owner

Related posts about variables

Related posts about garbage