Home > Enterprise >  C function inside function
C function inside function

Time:11-13

/*I need to use the result from the (delta) function inside the (sol_ec_II) function for a school assignment.*/

#include <iostream>
#include <ctgmath>
using namespace std;


double delta(double a, double b, double c) {
    return (b * b) - (4 * a * c);/* so I need to take this value [(b * b) - (4 * a * c)]
                               and use it in sol_ec_II in the places where I wrote "delta". */


    }
void sol_ec_II(double a, double b, double c) {
    if (delta < 0) {//here
        cout << endl << "Polinomul NU are solutii.";
    }
    else {
        double x1 = -1 * b - sqrt(delta);//here
        double x2 = -1 * b   sqrt(delta);//here

    }
}

// I would also need to use the (delta) function inside the (sol_ec_II) so they use the same a, b, c values like this:

void sol_ec_II(double a, double b, double c) {
    delta(a, b, c);
    if (delta < 0) {
        cout << endl << "Polinomul NU are solutii.";
    }
    else {
        double x1 = -1 * b - sqrt(delta);
        double x2 = -1 * b   sqrt(delta);

    }
}

//so I don't understand how to get the value that results from delta(a, b, c) and use it inside the if statement and sqrt.

CodePudding user response:

here you should pass parameters to deleta function in order to execute it:

void sol_ec_II(double a, double b, double c) {
    if (delta(a,b,c) < 0) {//here
        cout << endl << "Polinomul NU are solutii.";
    }
    else {
        double x1 = -1 * b - sqrt(delta);//here
        double x2 = -1 * b   sqrt(delta);//here

    }
}

or you could save the result in a new variable called result for example, and after that use it, like that:

void sol_ec_II(double a, double b, double c) {
    double result = delta(a,b,c);
    if (result < 0) {//here
        cout << endl << "Polinomul NU are solutii.";
    }
    else {
        double x1 = -1 * b - sqrt(delta);//here
        double x2 = -1 * b   sqrt(delta);//here

    }
}

The Same thing for the second function, always to execute a function use parenthesis and pass between them the arguments that the function expects.

CodePudding user response:

The result "comes out" of the function call at the time you call it. Look, you already know how sqrt works. sqrt is a function! You write sqrt(something) and that calls the function sqrt and it calls the function sqrt with the argument something and then the return value from sqrt gets used in the place where you wrote sqrt(something). e.g. 1 sqrt(4) calculates 3.

Likewise the return value from delta gets used in the place where you wrote delta(a, b, c). If you want to call delta and then call sqrt (i.e. calculate the square root of the delta) you write sqrt(delta(a, b, c)).

Obviously, just calculating a number is pretty useless. You probably want to do something with the number, like saving it in a variable or printing it. Examples:

cout << "the square root of the delta is " << sqrt(delta(a,b,c)) << endl;
cout << "the delta plus one is " << (delta(a,b,c)   1) << endl;

double the_delta = delta(a,b,c);
cout << "the delta is " << the_delta << " and the square root of the delta is " << sqrt(the_delta) << endl;

if (delta(a,b,c) < 0)
    cout << "the delta is negative" << endl;
else
    cout << "the delta isn't negative" << endl;

Note: Every time the computer runs delta(a,b,c) it calls the delta function. It doesn't remember the calculation from last time. You can see this because if you put cout instructions inside the delta function, they get printed every time the computer runs delta(a,b,c).

Of course I will not give you the solution for your program. I hope this helps you understand how functions work.

CodePudding user response:

To reuse the value you get from calling a function multiple time use a variable:

double delta(double,double,double) { return 1.2; /*ignore this for now*/ }

void sol_ec_II(double a, double b, double c) {
  const auto kDelta = delta(a, b, c);
  if (kDelta < 0.0) {
    // do stuff
  } else {
    const auto kRootD = sqrt(kDelta); // same idea
    const auto x1 = -b - kRootD;
    const auto x2 = -b   kRootD;
    // use the variables
  }
}

I use auto out of habit, you don't need to, double is fine.

  • Related