-3

I am new to programming if you could not tell by the advanced code I have laid out for you. I am reading a book on C programming and I copied this code out of it as an exercise and I have no idea why I am getting the error I am. PLEASE HELP!!!

I am getting an error that states "file: main.c" "message: undefined reference to calcyear"

/*bigyear.c*/

 #include <stdio.h>
 #define TARGET_AGE 88

int year1, year2;

int calcYear(int year1);

int main(void)
{
    printf("What year was the subject born?");
    printf("Enter as a four digit year (YYYY):");
    scanf("%d", &year1);

    /*calculate the future year and display it*/
    year2 = calcYear(year1);

    printf("someone born in %d will be %d in %d.", year1, TARGET_AGE, year2);

    return 0;

    int calcYear(int year1)
    {
        return(year1+TARGET_AGE);
    }


}
JBentley
  • 6,099
  • 5
  • 37
  • 72
gdubs
  • 11
  • 2

1 Answers1

1

Define int calcYear(int year1) outside of the main() function.

You cannot define a function within another function in standard C.

quantdev
  • 23,517
  • 5
  • 55
  • 88
  • A function doesn't need to be defined before being called, it just has to be declared before being called; which OP has done correctly. The problem is that the nested function is a definition of a different function to the one being called. – M.M Jun 05 '14 at 02:08
  • Yes I typo'ed `declared`, thx @Matt. "The nested function" : you agree that it is invalid, right? – quantdev Jun 05 '14 at 02:11
  • The declaration (before `main`) is correct, the attempted definition is invalid – M.M Jun 05 '14 at 02:15