M
PRO
last year
Manfredcountry asked

In C, can we define a function after main()? If yes, what is the difference?

Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Manfred,

Yes, you can define a function after main(). The only difference is that if main() calls that function, you should write a function prototype above main() so the compiler knows the function exists.

Example:

int findSquare(int n);  // prototype

int main() {
    printf("%d", findSquare(5));
    return 0;
}

int findSquare(int n) {  // definition after main
    return n * n;
}

If the function is defined before main(), you usually do not need the prototype.

If you have more questions, I am here to help 😊

C
This question was asked as part of the Learn C Programming course.