How to call the function ?

My dear friend,

How to call the function in different file. 1 main and 5 function.

I'am not sure to create the function to call each by one in different file.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
    
using namespace std;
int main()
    
{
double FA;
    
    
cout << "Welcome to Financial Ratio C++..!!" << endl;
    
cout << "\nPlease choose Financial Ratio:" << endl;
cout << "\n1.Profitability ratios \n2.Liquidity ratios \n3.Activity ratios \n4.Debt ratios \n5.Market ratios"<< endl;
cin >> FA;
    
};



Last edited on
To call functions defined in a different file you need to do two things.

1- Make the function visible in the point where you want to call it.
In practice this means that you need to supply a function prototype to the file that wants to call the function. Function prototypes are composed of the return type, name and parameters of the function. Example
void ProfitabilityRatios(int arg1, double arg2);
A function prototype doesn't define what the function does, only what it looks like. Pay attention to the semicolon.
What you want to do is create a header file where you will write all your prototypes, and then #include it in any file where you want to use them.

2- Create a new file where you will define the functions, then compile and link it to your main code.
Create a new .cpp file. In that file you will #include the header with the prototypes and then proceed to write what the functions do, just like when you define them in a single file.
To compile and link: if you are using an IDE (Visual Studio, Dev-C++) then you will have to add the file to the project and that will usually take care of everything. If you are using command line GCC simply add the file for cokmpilation. Like this
g++ main.cpp functions.cpp -o program
^ What he said.

Easiest way to manage it after that is to define an array of function pointers, and then make a function call to that array using the index chosen by the user.
I am not clear. Can you show me the coding?? How??

I can understand when you show me the code. Help me please??
Last edited on
Functions.h
1
2
void FunctionInOtherFile(int param); // Prototype, or declaration
// Etc 


Functions.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "Functions.h"

// Note that the parameter is named differently.
// It doesn't matter, the declaration doesn't check the name
// but you can put it there for clarification
void FunctionInOtherFile(int arg) // Definition
{
    // Your code here
}

// Etc 


Main.cpp
1
2
3
4
5
6
7
8
9
// Allows the main file to see that the functions exist
// so the compiler won't complain about undeclared functions.
#include "Functions.h"

int main(int argc, char** argv)
{
    FunctionInOtherFile(1); // Function call
    // Etc
}


Of course you still need to compile and link the definitions of the functions, but the procedure changes depending on what you use to code.
Last edited on
Now I can understand. Thank you Maeriden for help... :)
Topic archived. No new replies allowed.