C++ Procedure to Function transition

Hello, I started learning C/C++ by taking a class and everything was going smoothly until we had to refactor the Procedures we've done into Functions to return a value.

The program is working fine with Procedures.
I did the first transitioning of a Procedure into Function and it wasn't detected as an error (it still won't compile nonetheless).
Onto the next procedure, I did the same thing as I did and compile...I got tax_rate: 'undeclared identifier'

Below is just a bit of the code I'm working on and where I'm currently stuck at.

I hope to receive some hints or advice to solve this problem I'm currently on.
Thanks for the help.

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

#define TAX_RATE

    double tax (double gross_pay, double tax_rate); 

int main()
{
    tax (gross_pay, tax_rate);  // Call module


    double tax (double gross_pay, double tax_rate)  // Function Module
    {
    double  tax;           

    tax = gross_pay * TAX_RATE;

    return  tax;
    }
}
Last edited on
closed account (SECMoG1T)
i think in c++ functions and procedures mean the same thing,

your errors:
1. you shouldn't define functions within main -> your line 12 does that
2. declare variables before using them, tax doesn't do that
3. don't use conflicting names for functions and variables, let each be unique,
seems like you have a local variables and a function called tax


well you overall code structure should be something close to this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
include <iostream>

double get_tax (double gross_pay, double tax_rate);//function prototype

int main()
{
    double my_tax=0.0,gross_pay=/*value*/;  ///declared variables
    const double tax_rate = /*value*/;

   my_tax =  get_tax (gross_pay, tax_rate);  // Call function


    std::cout<<"you tax is: "<<my_tax<<std::endl;
}

double get_tax (double gross_pay, double tax_rate)  // Function definition
{
    double  tax{};

    tax = gross_pay * TAX_RATE;

    return  tax;
}
Thanks for the reply,

after looking carefully again, I realized where I got it wrong.

Thanks a bunch.
Topic archived. No new replies allowed.