Which functions come first? [Archive] - C Board

PDA

View Full Version : Which functions come first?


chaser
07-27-2002, 10:00 PM
I just realized that if you have a function which calls another funtion, the one it calls must be written in the code prior to the calling function so that the compiler knows what you're looking for.

Why is that? What if it gets to be a mess of arranging functions in a proper order, or what if it's impossible?

Is there some better way of handling this?

tgm
07-27-2002, 10:08 PM
Write function declarations at the top (or in a header file):

int function( int, float );

then later in the file write the definitions (or in a separate file):

int function( int foo, float bar )
{
/* function code */
}

This way is doesn't matter what order the functions are defined in.

chaser
07-27-2002, 10:31 PM
I had just read that in the tutorial after I posted the question. Thanks anyhow, I guess it's called Prototyping :)

I've got all my functions done that way now, I suppose it's a good habbit.

d00b
07-28-2002, 12:31 AM
I'll admit I still forget to prototype lots of stuff sometimes :)

master5001
07-29-2002, 02:11 AM
The reason you need to prototype or declare functions before you use them is because the compiler needs to know what a function is before you can use it. You would think by now you wouldn't need to do this. Look at JAVA.

GrNxxDaY
07-29-2002, 08:38 AM
Try switching from JavaScript to C++! You don't prototype in JS, so when i made the switch I pulled my hair out at all these c++ cause I thought they were all wrong! *bonks self* Now JavaScript looks funny to me. No semi-colons? WTF

d00b
07-29-2002, 09:54 AM
I was the same way... before C++ I was heavily into JavaScript, which is why I still forget sometimes to prototype :)

prototyping is a result of C++'s type-checking and function overloading

red_baron
07-29-2002, 01:04 PM
heh i guess i'm going in the opposite direction than you guys i started with c and now i'm trying to learn java (applets and applications), but i havn't tried javascript so i dont know if its the same.

kas2002
08-01-2002, 12:09 PM
I started with a little javascript then java then I moved on to C++ and next came PERL. I dunno why but fer sum reason I can't just learn one thing at a time. Oh well I guess I mostly program C++ though

lightatdawn
08-02-2002, 07:01 PM
>>
Why is that? What if it gets to be a mess of arranging functions in a proper order, or what if it's impossible?
>>Is there some better way of handling this?

Oh yes, most definatly. Much better than prototypes: Take advantage of C++ and use Classes. You'll never regret it.

Inquirer
08-02-2002, 07:13 PM
I do love classes. I learned Javascript, then basic, then JAVA, then i learned C. When i got a C book i almost screamed because i wanted to make classes, not wimpy structs. Then i got C++ into my brain, and classes dominate 2/3 of my header files. YES!