calling functions from functions

This is a discussion on calling functions from functions within the C++ Programming forums, part of the General Programming Boards category; I know how to call a function from main, but it doesn't work from functions. Heres an example code. Code: ...

  1. #1
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161

    Post calling functions from functions

    I know how to call a function from main, but it doesn't work from functions. Heres an example code.

    Code:
    #include <iostream.h>
    
    void functiona()
    {
      cout<<"Hello there";
      functionb();           //this doesn't work!!!
    }
    
    void functionb()
    {
    cout<<"Another hello";
    cin.get();
    }
    
    int main()
    {
      functiona();           //this works
    }
    The compiler says that the is no comparison with int functionb()

    So how do I tell it that its void functionb()

    Thanks in advance.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You just need to prototype it. Try this
    Code:
    void functiona();
    void functionb();
    
    int main()
    {
      functiona();
    }
    
    void function a()
    {
      cout<<"Hello there";
      functionb();
    }
    
    void functionb()
    {
      cout<<"Another Hello";
      cin.get();
    }
    Prototyping just lets the compiler know that there is a function defined somewhere that has a certain name, return type, and parameters. Once it knows it exists somewhere it can make the call and then it lets the linker resolve it.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Use prototypes:
    Code:
    #include <iostream>
    using namespace std;
    
    void functiona();
    void functionb();
    
    int main()
    {
      functiona();           //this works
    }
    
    void functiona()
    {
      cout<<"Hello there";
      functionb();           //this doesn't work!!!
    }
    
    void functionb()
    {
    cout<<"Another hello";
    cin.get();
    }
    Or put functionb() before functiona().

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    The scope of functionb() in your code is only from the point it is implemented and down. Since functiona() is outside of said scope, it doesn't know what you're talking about. Put a function prototype (this is just a declaration of a function defined somewhere else) above functiona().
    Code:
    void functionb();// function prototype
    
    void functiona()
    {
      cout << "Yo yo";
      functionb();
    }
    [edit]Wow, three responses at once. Nice.[/edit]
    Last edited by LuckY; 12-14-2004 at 02:09 PM.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,208
    Just do some small changes:
    Code:
    Code:
    #include <iostream>   // Drop the .h to comply to the standard
    using namespace std;    // Tell the compiler that we want to use what is in the standard namespace.
    
    // These are called function prototypes and basicly tells the compiler that the functions exists
    void functiona();    
    void functionb();
    
    int main()
    {
      functiona();           //this works
    }
    
    // Here comes the function definition/decleration (dont know what they are called lol) and just tells what the function does. 
    
    void functiona()
    {
      cout<<"Hello there";
      functionb();           //this doesn't work!!!
    }
    
    void functionb()
    {
     cout<<"Another hello";
     cin.get();
    }
    Edit: damn! beaten!

  6. #6
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161
    thanks... I tried it on my programme and it works

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Wow.. The power of Cboard at its prime.
    Four answers within almost no time.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    then I come along and say:

    V try the code in my sig V
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  2. Replies: 9
    Last Post: 01-26-2008, 02:12 AM
  3. calling functions: exit and return
    By 911help in forum C Programming
    Replies: 3
    Last Post: 12-28-2007, 12:24 PM
  4. I Need Help Defining and Calling Functions
    By jonbuckets in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2007, 09:46 AM
  5. Calling functions help
    By ForlornOdium in forum C++ Programming
    Replies: 14
    Last Post: 09-29-2003, 08:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21