-5

Is it possible to write a function in a file and then use it inside code that is contained in another file ? I am using c++ . If it is,can anyone give an example ? Thanks !

  • Yes, it is possible. Do you happen to have a [good c++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? Any good introduction to C++ should include details on how to do this. – David Brown Feb 15 '13 at 20:02
  • are you looking for a script-able C++? – corn3lius Feb 15 '13 at 20:03
  • "Code usability in C++" - Try researching libraries. – Ed Heal Feb 15 '13 at 20:03
  • I'm not 100% sure what the point is: Using a C++ function from another C++ source file, that are compile together: Yes, very much possible. Using C++ generated from C++, and using it in the program that generated it, without recompiling - not possible. – Mats Petersson Feb 15 '13 at 20:05
  • It is hard to tell what exactly you are trying to accomplish. Do you want to build a program from more than one source file, or do you want to use a source file in a program that is already built? – n. m. could be an AI Feb 15 '13 at 20:06
  • I am trying to use more than one function on the code inside one file and this would help with the readability of the code . Can anyone please give an example or a link to where i can find one ? Thanks ! – Roman Ovidiu-Robert Feb 15 '13 at 20:12

2 Answers2

0

This is trivial to accomplish and a basic part of writing any C++ program of any complexity. The details of how you do it can vary a bit depending on which platform you're on. But, all of them are variants of the same basic abstract procedure.

You have to declare your function in a header file, and you have to include that header file in each source file that either uses or defines the function. Then you have to compile each source file and link the resulting 'object' files together into an executable.

In order to give more specific detail than that you will have to be specific about your platform. Is it Visual Studio? Is it g++ on a Linux box?

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
0

Yes of course it is. How to make it happen depends on what these two files are (header files or main code files). Let's call them function_defined.___ and function_used.___.

There are two cases, depending on what each is.

  • function_defined.hpp

    The simplest case -- In function_defined.hpp, put

    int funct(int argument) {return 1}
    

    and in function_used.(c/h)pp, just

    #include "function_defined.hpp"
    ...
    int c = funct(1);
    
  • function_defined.cpp

    In this case, you first need a declaration in function_used.(c/h)pp :

    int funct(int argument);
    

    You can call the function just like above. Do not do #include "function_defined.cpp". You should just compile all the .cpp files and link them together, which automatically finds and links the desired function.

As Omnifarious said, the details of compiling and linking depend on your platform and IDE and/or compiler.

Milind R
  • 676
  • 1
  • 8
  • 20