Calling a function from another file

This is a discussion on Calling a function from another file within the C++ Programming forums, part of the General Programming Boards category; Hello, im having trouble using a function from another file in a project, this is a example from my book: ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    70

    Post Calling a function from another file

    Hello, im having trouble using a function from another file in a project,
    this is a example from my book:
    Calling a function with only a prototype:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void BigDog(int KibblesCount);
    
    int main()
    {
        BigDog(3);
        return 0;
    }
    Using a function from a seperate file:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void BigDog(int KibblesCount)
    {
        cout << "I'm a lucky dog" << endl;
        cout << "I have " << KibblesCount << " pieces of food" << endl;
    }
    i know how this example works but, i tried making my own:

    I make a prototype and made the function:
    Code:
    #include <iostream>
    using namespace std;
    
    int Sum(int x,int y);
    
    int main(){
    cout << "Type 2 numbers" << endl;
    cout << Sum(30,20) << endl;
    }
    int Sum(int x,int y){
    
     int result;
    
     result = x + y;
     return result;
        
    }
    Here i tried making the file where i was going to use the Sum(int x,int y) function:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int Sum(int x,int y)
    {
        cout << "The number is " << Sum(x,y) << endl;
    }
    but i dont know how to program this file so it'll be able to use the Sum function.

    Any help?

  2. #2
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    What IDE are you using? If you use Visual C++ or CodeBlocks, just add a new header and source files (a .h and a .cpp files) into the project. And then in your main source file (ie. the one with your "int main()" block), insert an include statement of the header file (eg. #include "foo.h") and you're all set. However, if you are using vi or notepad for this (well I know some people who swear that vi is da bomb :P ), you need to manually link the files yourself.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    70
    Quote Originally Posted by g4j31a5 View Post
    What IDE are you using? If you use Visual C++ or CodeBlocks, just add a new header and source files (a .h and a .cpp files) into the project. And then in your main source file (ie. the one with your "int main()" block), insert an include statement of the header file (eg. #include "foo.h") and you're all set. However, if you are using vi or notepad for this (well I know some people who swear that vi is da bomb :P ), you need to manually link the files yourself.
    i just reread my book and now i get it LOL, i should read slower next time ;P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

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