Public Group

# C problem, probably very simple

This topic is 3716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, i have a BIG problem, and i do believe it is just an option to turn on. Which, however, i can't find :( When i make code like this: //------- part 1 ------ void main (void) { int x=5; x=MyFunction(x); printf ("res=%d\n", x); return(); } //------- part 2 ------ int MyFunction (int x) { x=x*2; return(x); } //------- end ------ Ok, this is VERY simple stuf. I am in downloading minigw,code::blocks etc after reinstall, in case you need error code, i will have it in couple of hours. The problem is next: if i call some function (MyFunction (int x) ) and that function is placed BELOW, compiler gives me error. But... If i place it ABOVE, all works ok. In this particular example, the above WILL not work, but the following WILL: //------- part 2 ------ int MyFunction (int x) { x=x*2; return(x); } //------- part 1 ------ void main (void) { int x=5; x=MyFunction(x); printf ("res=%d\n", x); return(); } //------- end ------ I hope you got the point. Every function i call, must be placed ABOVE in source, so it is kinda funny to read code downside-up :) I am using gcc and code::blocks. Help... ps: in this post, i wanted to use tags to mark my source, but couln't find sticky topic to read how. Sorry :(

##### Share on other sites
Well you try to use a function before the compiler even knows that it exists -> compile error.

You can place a Prototype above the main method so the compiler knows that there will be a function with such a name and type of parameters.

Prototype example:
int MyFunction (int x);

##### Share on other sites
It's perfectly normal: in C (and C++) a function must always be declared above the point where it is used. No exceptions.

Of course, although you have to declare it, you don't have to define it. Therefore, you can write:

int MyFunction(int);int main (void){   int x=5;  x=MyFunction(x);  printf ("res=%d\n", x);  return(0);}int MyFunction (int x){  x=x*2;  return(x);}

Also, void is not a valid return type for main in C. You should return an integer.

##### Share on other sites
you have to use function prototypes,example:

int MyFunc(int); /* this is a function header */

int main()
{
int r;
r=MyFunc(5);
return 0;
}

/* this is a function declaration */
int MyFunc(int x)
{
return 2*(x+5);
}

##### Share on other sites
[edit - slow slow]

You need to prototype your function:

int MyFunction (int x); // this is a function prototypevoid main(void){    int x=5;    x=MyFunction(x);    printf ("res=%d\n", x);    return();}int MyFunction (int x){     x=x*2;    return(x);}

Actually, in old style C, you didn't HAVE to, but it was always a pretty good idea because without a prototype (or an already fully defined function), the compiler could not error check the parameters and had to assume int for the return, even if you then defined a different return type later.

It's mandatory in C++. You are either compiling as C++, or using a C compiler that is being strict due to some default option or something.

##### Share on other sites
This is normal. The gcc compiler needs to know the function before you call it. You can also tell the compiler a certain function will come later on, this way you can use your own order. for example:

// declaration of function to comeint MyFunction (int x);//------- part 1 ------void main (void){   int x=5;  x=MyFunction(x);  printf ("res=%d\n", x);  return();}//------- part 2 ------int MyFunction (int x){  x=x*2;  return(x);}//------- end ------

##### Share on other sites
Thats perfectly normal. How should the compiler know where to find MyFunction()?

You can use function prototypes in either the source file or preferrable in the header file.

int MyFunction(int);
.
.
void main()
{
}
.
.
int MyFunction(int x)
{
return blah;
}

The compiler knows there _is_ a function called MyFunction(), where it's located and if this function even compiles is unimportant at this point.

##### Share on other sites
geeezz... thanks guys, i really didn't expect so fast answer!

The problem is that i learned C on amiga (under emulator), and the example i wrote worked! It is CubicIDE package, gcc compiler, if someone knows. Obviously, compiler was making two passes, finding prototypes (among other stuff) at first, and later, compiling it.

This way you explained, the function definitions looks like they are doubled. I prefer amiga way :)

Kidding. I will fix things and ask two more questions; do i put prototypes in separate file or in my source? What would you recommend me? From experience.

Second is: what tags do i use to mark source in my post? game-dev.net is loading VERY slowly here, i can't find them, and would like to show it as it should be. According to TOS.

Thanks again guys....

##### Share on other sites
Quote:
 Original post by Srbin013Obviously, compiler was making two passes, finding prototypes (among other stuff) at first, and later, compiling it.

Oddly enough, it wasn't. Original C did not make function prototypes mandatory. However, in their absence, it assumed an int return type and turned off all parameter checking for the call. So:

// original Cvoid f(void){    int z=g(123.0f); // compiles okay}double g(const char *s){    printf("%s",s); // boom!    return 23.0f;}

The original C compiler, in f() above, effectively treats g() as having the signature int g(...). This can obviously lead to the kind of explosion above.

This is probably the reason it was outlawed in C++ (and possibly C99, not sure) in the interests of making software work properly [smile].

Quote:
 Original post by Srbin013Kidding. I will fix things and ask two more questions; do i put prototypes in separate file or in my source? What would you recommend me? From experience.

Doesn't strictly matter. When compiling multiple units, you would normally have the prototype in a .h and the function in a .c file. The .h would then be included in the .c file defining the function and in any units using the function. Since the preprocessor is still to this day just a complicated text pasting service, this is equivalent to typing the prototype at the top of each .c file (which is also valid, by the way).

Quote:
 Original post by Srbin013Second is: what tags do i use to mark source in my post? game-dev.net is loading VERY slowly here, i can't find them, and would like to show it as it should be. According to TOS.

[ source ][ /source ] (but without the spaces).

##### Share on other sites
One more thing; since that way you explained to me, the function definition is doubled. Right?

Isn't there some option in compiler that would make it double pass the source? Quite confusing this way, if i want to change parameters for function, i need to do it twice; first in source, and later hunt it in header file.

@loneranger

you told that i can write this in prototype:

int MyFunc(int); /* this is a function header */

So, there is no (int x).

If my function asks for more than 1 parametar, am i allowed to use something like this:

int MyFunc( int, double, int, float); // just an example

ie. don't have to use (int x, double y, int z, float g);

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 12
• 16
• 9
• 14
• 10
• ### Forum Statistics

• Total Topics
632657
• Total Posts
3007680
• ### Who's Online (See full list)

There are no registered users currently online

×