0

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;
}

int 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);
}
  • If you have a helpful answer you should accept it. Click on checkmark under upvote/downvote counter. This will mark the question as "answered" and provide you with small reputation bonus. See [How do I ask](http://stackoverflow.com/faq#howtoask) FAQ article. – Artemix Sep 22 '12 at 20:51

1 Answers1

1

Simple. you pass birthYear into askBirthYear by value, not by reference. And then you just drop on the floor its return value. Also you have disagreement in declaration of askBirthYear and its definition.

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

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

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

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

void sayAgeInYears(int birthYear)
{
    int age;
    age = 2012 - birthYear;
    printf("You are %d years old.\n", age);
}
Serge
  • 6,088
  • 17
  • 27
  • Yeah, I typed the wrong return type on that function. It's correct in my source file, though. However, I don't know how to pass a variable by value instead of reference. I also don't understand what you mean by "dropping on the floor". Sorry, I have no prior programming experience, so this is all new to me. – mintyfresh Sep 21 '12 at 21:41
  • The code won't compile now... I thought it would be fine for askBirthYear to receive an integer instead of void, too... I'm really confused here. – mintyfresh Sep 21 '12 at 22:05
  • Sorry, I missed a semicolon. I fixed it try again. – Serge Sep 21 '12 at 22:12
  • Oh wow, I can't believe I didn't see that one. The program works just fine now, thanks a lot! This is beginning to make much more sense. – mintyfresh Sep 21 '12 at 22:14
  • I intentionally modified your program this way as you mentioned you just started to learn C. You will have to new what is passing by reference, what is a pointer and what are consequences of improper pointer use. But first you need to get more familiar with basic things) – Serge Sep 21 '12 at 22:17