Thread: function help

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    77

    Talking function help

    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!

  2. #2
    instead of:
    Code:
    goto reverse_array;
    use:
    Code:
    reverse_array(array);
    And, to declare reverse_array(), you do something like this:

    Code:
    char *reverse_array(char array[12]){
    //code here
    }
    The basic format of a function is this:
    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

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    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
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    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!

  5. #5
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    do you understands the concept of what a function is?
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    not really
    Hooked On Phonics Didn't Work For Me!

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    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:
    Code:
    while ( x <= y ){
      if ( x <= 10 ){
        x = x + 2;
        y = y + 1;
      }
      else if ( x > 10 ){
        x = x + 3;
        y = y + 1;
      }
    }
    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:
    void testX() {
     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:
    int main(){
      //...
      testX();
      //...
    }
    saving you both time and effort. hope that helps.
    I came up with a cool phrase to put down here, but i forgot it...

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    does it go before or after the int main?
    Hooked On Phonics Didn't Work For Me!

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    the way i do it is:

    Code:
    #include<whatever>
    
    void function(){
    //...
    }
    
    int main{
    //...
    }
    and that seems to work for me.

    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...

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    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!

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    you got it

    now compile it and have fun...
    I came up with a cool phrase to put down here, but i forgot it...

  12. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    woo hoo!!!!!!!!!!
    good bye long lists of goto commands!!!!!
    Hooked On Phonics Didn't Work For Me!

  13. #13
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Ok here's where you're going wrong:

    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.
    }
    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.

    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
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  14. #14
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    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...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM