Second user-defined function returns garbage value?
- by mintyfresh
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);
}