# [SOLVED] Potential issues for recursive function not being called in another function

## Issue

I’m trying to build a cash register (an algorithm project in FreeCodeCamp) and I’ve created a function (giveChange(), a recursive function) to be invoked in another separate function (checkCashRegister).

However, the function is not being called. I would like function checkCashRegister to return 0 in my case.

I checked whether it’s the problem of placing "return" in the wrong place in for loop or problems of scope. Doesn’t seem either.

``````function giveChange(diff, cid) {
for(let i = cid.length - 1; i > 0; i--) {
if(cid[i-1] <= diff && diff <= cid[i]) {
diff = diff - Math.min(cid[i-1], cid[i-1]*Math.ceil(((diff-cid[i-1])/cid[i-1])))
if(diff !== 0) {
giveChange(diff, cid)
} else {
return diff
}
}
}
return diff
}

function checkCashRegister(price, cash, cid) {

let diff = cash - price
cid = 0.01
cid = 0.05
cid = 0.1
cid = 0.25
cid = 1
cid = 5
cid = 10
cid = 20
cid = 100

giveChange(diff, cid)

return diff
}

checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])
``````

## Solution

You are missing `return` keyword before invoking `givechange(diff, cid)`,

Note: I’ve updated the condition and used a ternary to not repeat the `return` keyword

``````function giveChange(diff, cid) {
for (let i = cid.length - 1; i > 0; i--) {
if (cid[i - 1] <= diff && diff <= cid[i]) {
diff = diff - Math.min(cid[i - 1], cid[i - 1] * Math.ceil(((diff - cid[i - 1]) / cid[i - 1])))
return diff !== 0 ? giveChange(diff, cid) : diff
}
}
console.log(diff)
return diff
}

function checkCashRegister(price, cash, cid) {

let diff = cash - price
cid = 0.01
cid = 0.05
cid = 0.1
cid = 0.25
cid = 1
cid = 5
cid = 10
cid = 20
cid = 100

return giveChange(diff, cid)
}

checkCashRegister(3.26, 100, [
["PENNY", 1.01],
["NICKEL", 2.05],
["DIME", 3.1],
["QUARTER", 4.25],
["ONE", 90],
["FIVE", 55],
["TEN", 20],
["TWENTY", 60],
["ONE HUNDRED", 100]
])``````