#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    C++parameter help


    I'm having problems with my parameter in doing a program for phythagoras theorem with a function called rightriangle.

    #include <iostream>
    #include <cmath>
    using namespace std;
    void righttriangle(double,double,double);
    int main()
    {

    double a,b,c;
    cout<<"please enter a:"<<endl;
    cin>>a;

    cout<<"please enter b:"<<endl;
    cin>>b;

    righttriangle(a,b,c);

    return 0;


    }

    void righttriangle(double x,double y, double z)
    {
    double x,y,z;
    z=sqrt(pow(x,2)+pow(y,2));

    cout<<"z is "<<z<<endl;

    return ;
    }


    F:\David_JohnstonCIS251\DavidJohnstonCis251Chapter5work\DavidJohnstonChapter6Problem3Page254\DavidJo hnstonChapter6Problem3Page254.cpp(26) : error C2082: redefinition of formal parameter 'x'

    F:\David_JohnstonCIS251\DavidJohnstonCis251Chapter5work\DavidJohnstonChapter6Problem3Page254\DavidJo hnstonChapter6Problem3Page254.cpp(26) : error C2082: redefinition of formal parameter 'y'

    F:\David_JohnstonCIS251\DavidJohnstonCis251Chapter5work\DavidJohnstonChapter6Problem3Page254\DavidJo hnstonChapter6Problem3Page254.cpp(26) : error C2082: redefinition of formal parameter 'z'


    please tell me how to fix this thanks!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,090
    Rep Power
    2222
    Use code tags.

    Here:
    Code:
    void righttriangle(double x,double y, double z)
    {
        double x,y,z;
    x, y, and z are defined as the parameters. Then you go and redefine them in the declaration in red. That is what the error messages are telling you. Solution: don't do that.

    Though I have a question for you. In main(), only a and b get initialized, but c does not. Once you correct the errors, righttriangle() uses the values of a and b that are passed in through x and y, but it makes no use of the garbage value of c that's being passed in through z. Rather, righttriangle calculates the value of z.

    In other words, why even have the z parameter? Why not just pass in two arguments instead of three? Why not take away the z parameter and keep the local declaration of z?

IMN logo majestic logo threadwatch logo seochat tools logo