Home > Software engineering >  Adding onto a variable with each recursive call?
Adding onto a variable with each recursive call?

Time:08-10

The aim is for the the function to end the recursion once 10 has been reached. I have tried printing out cartLimit to get an understanding on what is going wrong. At the moment in every recursion call the cartLimit is just overwritten by the new itemQuant inputted rather than the itemQuant being added onto the previous itemQuant within the cartLimit variable. Also even when I outright enter in 10 or a number above 10 as the itemQuant the function still continues to call itself.

double buyingItems(){

    double itemPrice;
    cout << "Please enter the item price: ";
    cin >> itemPrice;
    
    int itemQuant;
    cout << "Please enter item quantity: ";
    cin >> itemQuant;

    double totalCost = totalCost   itemPrice*itemQuant;

    int cartLimit = cartLimit   itemQuant;
    if(cartLimit != 10) {
    buyingItems();
    }else{
    return totalCost;
}
}

CodePudding user response:

Every function call has its own set of local variables.

Recursion is not special.

Here's that code again, but I hid the function name so you can't tell which function it is. Also your code is missing a } so I fixed that.

double XXXXXXXXX(){

    double itemPrice;
    cout << "Please enter the item price: ";
    cin >> itemPrice;
    
    int itemQuant;
    cout << "Please enter item quantity: ";
    cin >> itemQuant;

    double totalCost = totalCost   itemPrice*itemQuant;

    int cartLimit = cartLimit   itemQuant;
    if(cartLimit != 10) {
        buyingItems();
    } else {
        return totalCost;
    }
}

So what does this function do? It asks for the price and quantity. Then it makes a new variable totalCost, then calculates totalCost itemPrice*itemQuant (a random garbage number, possibly, because totalCost has no sensible number in it yet) and saves that in the variable totalCost.

Then it calculates the cartLimit (using the same approach including the random garbage number), and it maybe calls buyingItems - if buyingItems does any interesting stuff then it happens at this step - and or maybe it returns the totalCost that it calculated earlier. If it doesn't return totalCost then it possibly returns a garbage number or crashes, because there's no return instruction.

Notice: None of the code inside { buyingItems(); } has anything to do with the totalCost or the cartLimit variables. It can't because it doesn't have any way to access those variables. Maybe buyingItems has its own variable inside it called cartLimit, but that's a different variable because it's declared in a different function call.


With that in mind, can you figure out why the function XXXXXXXXX doesn't do what you want?

CodePudding user response:

C and C and almost every programming language in the last 50 years has a concept of a "stack" and a "function local context".

Each function call has its own state of local variables. When you recursively call the same function, the new call gets a completely separate copy of those local variables.

Now, in some older languages, this wasn't true. But it is true in almost every modern language.

double buyingItems(){
  double itemPrice;
  cout << "Please enter the item price: ";
  cin >> itemPrice;

  int itemQuant;
  cout << "Please enter item quantity: ";
  cin >> itemQuant;

  double totalCost = totalCost   itemPrice*itemQuant;

  int cartLimit = cartLimit   itemQuant;
  if(cartLimit != 10) {
    buyingItems();
  }else{
    return totalCost;
  }
}

And yes, this means no addition beyond what your code says happens goes on.

What I'd suggest you do is pass in the remaining cart limit, and pass out the cost. Then add it up.

double buyingItems(int cartLimit = 10, double existingCost = 0.)
{
  double itemPrice;
  std::cout << "Please enter the item price: ";
  std::cin >> itemPrice;

  int itemQuant;
  std::cout << "Please enter item quantity: ";
  std::cin >> itemQuant;

  double totalCost = itemPrice*itemQuant   existingCost;

  if(itemQuant < cartLimit)
  {
    return buyingItems(cartLimit - itemQuant, totalCost);
  }

  return totalCost;
}

Here, I pass in the accumulated cost and the room left in the cart as arguments, defaulted to 10 and 0 respectively.

  • Related