people tell me that i should make all my goto stuff in my programs into functions but i dont know how. ive read the tutorials and stuff on this site but i just cant figure it out can someone please explain it.
people tell me that i should make all my goto stuff in my programs into functions but i dont know how. ive read the tutorials and stuff on this site but i just cant figure it out can someone please explain it.
Hooked On Phonics Didn't Work For Me!
instead of:
use:Code:goto reverse_array;
And, to declare reverse_array(), you do something like this:Code:reverse_array(array);
The basic format of a function is this:Code:char *reverse_array(char array[12]){ //code here }
Code:return_type function_name(parameters...){ commands }
Last edited by Inquirer; 10-29-2002 at 04:16 PM.
Compilers:
GCC on Red Hat 8.1 (Primary)
GCC on Mac OS X 10.2.4 (Secondary)
Others:
MinGW on XP
heh, I doubt any explanation is going to be much more than the tutorial, heck it would be a tutorial by definition . Why don't you try reading it again, and then tell us exactly what you don't understand about it.
Do you not understand the concept? Do you not understand the syntax? Passing?
Kermi3
i dont understand any of it i read the tutorial and i couldnt make heads or tails of it
Hooked On Phonics Didn't Work For Me!
not really
Hooked On Phonics Didn't Work For Me!
when i first learned how to use functions, it opened up a new world to me in C++. and the concept is surprisingly simple.
lets say you want to do this:
15 times within your program. no one wants to type that, or even copy and paste it, 15 times. so instead of having to write it more than once, you just define it as a function:Code:while ( x <= y ){ if ( x <= 10 ){ x = x + 2; y = y + 1; } else if ( x > 10 ){ x = x + 3; y = y + 1; } }
now everytime you want to put that whole formula in your code, instead of writing out the whole thing, you put:Code:void testX() { while ( x <= y ){ if ( x <= 10 ){ x = x + 2; y = y + 1; } else if ( x > 10 ){ x = x + 3; y = y + 1; } } }
saving you both time and effort. hope that helps.Code:int main(){ //... testX(); //... }
I came up with a cool phrase to put down here, but i forgot it...
does it go before or after the int main?
Hooked On Phonics Didn't Work For Me!
the way i do it is:
and that seems to work for me.Code:#include<whatever> void function(){ //... } int main{ //... }
i think you can also initialize the functions before the main, and then define them afterwards, like so:
Code:#include<whatever> void function(); int main(){ //... } void function(){ //... }
I came up with a cool phrase to put down here, but i forgot it...
i think i got this right so tell me if it is
Code:#include <iostream.h> int y; int x; int a; void testx() { y = x + a; } int main() { cin >> x; cin >> a; testx(); cout << y; return 0; }
Hooked On Phonics Didn't Work For Me!
you got it
now compile it and have fun...
I came up with a cool phrase to put down here, but i forgot it...
woo hoo!!!!!!!!!!
good bye long lists of goto commands!!!!!
Hooked On Phonics Didn't Work For Me!
Ok here's where you're going wrong:
Now the was this was done with one type of passing that, as I've already explained, just copied your variables x and a into the function.Code:#include <iostream.h> int testx(int b, int c); //This is your decleration, like variables. //Basically what you are doing is telling the compiler that //somewhere in all of the code is a definition for this function, //that way when it sees it in the program it knows that it is //something, not uncompilable junk, and to look for it. //This one is "int testx(...) because you want it to send an int // back to the program in the end. You could also do char, //bool, or any class int main() { int n; int x; int a; cin >> x; cin >> a; n = testx(x, a); //This is the call for the function, x and //a are passed into it and sets n equal //to the value testx returns cout << n; return 0; } //This is where you define the function, what it does //Do this after your main. It must be exactly like your //decleration or the compiler won't recoganize it, except no ; at the end int testx(int b, int c) //Now your funtion has been given the variables x and a, they //have been but such that b==x and c ==a //This is shown by the order that they are passed into the function in. //Since the function is defined as testx(int b, int c) the 2 vaiables //must be ints, and the first one passed in as b and the second as c { int y; //Declaring y, this will only exsist in this function, //once the function is over the variable will be gone effectively y = b + c; return(c); //This is sending the value back to the "function," in this cas main, that called it. }
Another way to do it is passing by reference:
In that case your decleration would look like this:
int testx(int &b, int &c);
Now whatever values you pass in as b and c will be changed forever by whatever is done by the function...for example:
Code:void testx(int &a, int &b,int &c); //It is void this time because I //don't have to return anything, I am changing the variables that I'm passing in. int main() { int x; int y; int z; cin >> x; cin >> y; testx(x, y, z); cout << z; //This will output the sum of x and y since that's //what the function (see below) changes z to return 0; } void testx(int &a, int &b,int &c) { c = a + b; //remeber, the order you pass it in as still matters, //they are the same thing to the computer, but to you // a = x, b = y, c = z. }
Your program before would work because you declared the variables b, x, and a (or whatever you used) gobally, that means that you defined them outside of any function. This means that any funcion in teh program can use then and change them. However this can be dangerous when you have a big program because it means any function can change them. It also means that you can never use those variables for anything else.
I hope this helps
Kermi3
the program he wrote compiles fine and will work for most small programs, so i wouldnt say he is doing anything particularly wrong. (though maybe a bit less efficient )
for larger projects, i would seperate the functions into their own files. i dont do many large projects, but from vague memories, i am pretty sure you have to initialize the function in the main file, so that the program will know to look for its definition.
Last edited by ...; 10-29-2002 at 05:06 PM.
I came up with a cool phrase to put down here, but i forgot it...