Computer Science Canada

procedures

Author:  Blade [ Sat Jun 28, 2003 2:24 pm ]
Post subject:  procedures

how do you do procedures in C++? i know its something like
code:
void procname(){
//contents
}


but i dont understand how to use them... you have your main program in
code:
int main(){
//stuff
}
.. so where exactly are the procedures supossed to be declared, and how do you call them? i tried declaring them in main, above main, belo main and using procname(); just to call but, i dunno i think i confused myself, or i'm doing something wrong...

Author:  Catalyst [ Sat Jun 28, 2003 2:30 pm ]
Post subject: 

you can decalre your procedure anywhere outside of main( but if u declare it below main ur gonna have to have a prototype above main)
the easiest way to do it would be before main

then you can call them in main or in another function like so:

code:

int x;
void myfcn(int y)
{
x=y;
}

int main ()
{
   myfcn(5);
}

Author:  SilverSprite [ Sat Jun 28, 2003 2:47 pm ]
Post subject: 

ok so then what are inline functions? arent they supposed to be sortof procedure like? they insert a bit of code into the part of the program where they are called right?

Author:  Mazer [ Sat Jun 28, 2003 2:52 pm ]
Post subject: 

inline functions are just the same as normal functions except instead of having your program jump to the function, the compiler will copy the function into your program wherever it's called.

so if i made a function and called it somewhere in my program, the program would jump to that function (wherever it is in the code) and run it. but if i called the function using inline, then when the program is compiled it'll copy the contents of the function into the position where it's called. it's supposed to be more efficient in cases where your function is really short so that you aren't adding too many lines to your program by copying the function over and over, but at the same time you aren't wasting run time by jumping back and forth.

i don't know if i explained that very well, but i hope that i did! Wink

Author:  SilverSprite [ Sat Jun 28, 2003 3:41 pm ]
Post subject: 

oh i c so it saves runtime.. i didnt know the real purpose:S


: