Thread: Functions

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    12

    Functions

    from the function tutorial on this site, I understand how they work but what I dont get is the placement of it, for example
    Code:
    #include <iostream>
    int mult(int x, int y) {return x*y;}
    int main()
    {
        int x,y;
        std::cin>>x>>y;
        std::cout<<mult(x,y)<<std::endl;
        system("PAUSE");
        return 0;
    }
    In the example on the tutorial it had return at the end but that's for making blueprints of a program so I put it with the mult function before main() and it worked but I tried sticking main in front of the mult function and the program no longer works and im getting errors such as a parse error before int on the 3rd line, but why?
    Last edited by LegendsEnd; 04-18-2004 at 05:26 PM.
    Rawr.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A name must be declared before it can be used by the rest of the program. If main is first:
    Code:
    int main()
    {
      mult ( 2, 2 );
    }
    
    int mult ( int x, int y )
    {
      ...
    }
    Then main has no idea what mult is because mult is declared after main. To fix this you make sure that mult exists before main either by declaring mult with a prototype:
    Code:
    int mult ( int x, int y );
    
    int main()
    {
      mult ( 2, 2 );
    }
    
    int mult ( int x, int y )
    {
      ...
    }
    Or by declaring mult before main with a definition:
    Code:
    int mult ( int x, int y )
    {
      ...
    }
    
    int main()
    {
      mult ( 2, 2 );
    }
    My best code is written with the delete key.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Basically put: before you can use a function you have to tell the compiler that its there, its return type, and the types of it's inputs. However you don't actually have to say what the function does.

    As such the following are allowed:
    Code:
    #include <iostream>
    int mult(int x, int y) {return x*y;}
    int main()
    {
        int x,y;
        std::cin>>x>>y;
        std::cout<<mult(x,y)<<std::endl;
        return 0;
    }
    Code:
    #include <iostream>
    int mult(int, int);
    int main()
    {
        int x,y;
        std::cin>>x>>y;
        std::cout<<mult(x,y)<<std::endl;
        system("PAUSE");
        return 0;
    }
    int mult(int x, int y) {return x*y;}

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    I understand part of the reasoning but isnt the code read downwards by the compiler? So cant you declare main then tell the compiler about mult?
    Rawr.

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    In general you should create prototypes for all your functions and place them in a header file. That way, you know every function will be able to call every other function, and you can also include that header file in another object to use those functions.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I understand part of the reasoning but isnt the code read downwards by the compiler? So cant you declare main then tell the compiler about mult?
    Imagine you are the compiler with the following code:
    Code:
    void foo (void)
    {
      bar();
    }
    void bar (void)
    {
      cout<<"Hi"<<endl;
    }
    
    int main (void)
    {
      foo();
      return 0;
    }
    So you start off at the top and see foo() nothing wrong lets keep going. Now we hit a call to bar() but we don't know anything about bar(). I don't know where it is, what it is suppose to take, NOTHING. Now lets change it a bit:

    Code:
    void bar(void);
    
    void foo (void)
    {
      bar();
    }
    void bar (void)
    {
      cout<<"Hi"<<endl;
    }
    
    int main (void)
    {
      foo();
      return 0;
    }
    So we start again and see bar, ok now we know that bar is a function that accepts and returns nothing. Now we move onto foo() and come across bar() which is fine since we know all we need to know about bar() to call it. Now we run into bar() where we learn what it does. But now we have another problem, we have this symbol cout but don't know anything about it either.

    See where I'm going with this? The compiler has to know the very basic information about a function or heck any symbol before it can use it.

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    but isnt the code read downwards by the compiler?
    Yes, it is read downwards, for the exact reason that Thantos gave. It reads downwards, and notes all the functions it finds, until it gets to int main(). Then it starts executing.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    until it gets to int main(). Then it starts executing.
    You are thinking of execution which is a completly different stage and different setup than compiling.

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Good point.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    so once it hits main everything else after it at that scope is ignored? I also noticed how I can't divide a number by a larger number =/ like 3/4 = 0, and the variable was a float type, but things like 4/4 and 3/1 work
    Last edited by LegendsEnd; 04-20-2004 at 03:02 PM.
    Rawr.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok you need to be able to seperate "compiling" into the three parts: preprocessor, compiling, and linking.

    Preprocessor: Basically deal with #include and #define

    Compiling: This create the object code. This is where the source file is parsed, syntax is checked, and any optimization is done. Basically when when it encounters a function is puts a symbol there that says "goto this function where ever it my be."

    Linking: This is where it takes all those "where ever it may be" symbols and replaces it with the actual offset.

    At this point none of the code has actually been executed. Its all about getting the compiler and linker to understand what you want to do.

    The reason:
    Code:
    float x = 3/4;
    is because 3 and 4 default to integer which means it will use integer division and then put it into the float.
    Code:
    float x = (float)3/4;
    float y = 3.0/4;
    Both of these will result in .75

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM