鉴于此递归函数将base
的数字提高为exponent
的幂:
function power(base, exponent) {
if (exponent == 0)
return 1;
else return base * power(base, exponent - 1);
}
在内存中以什么顺序处理调用堆栈。
答案 0 :(得分:0)
假设您用 $charges = [
$row->StandardConcretePanel * $standardConcretePanelCharge,
$row->ADAPedestrianRampWithDomes * 1200,
$row->ADAPedestrianRampNoDomes * 800,
$row->OversizedConcretePanel * 340,
$row->ThickConcretePanel8to10 * 320,
$row->ThickConcretePanel10to12 * 340,
$row->ThickConcretePanel12orMore * 360,
$row->HighEarlyConcretePanels * 1100,
$row->WinterMix * 75,
$row->ConcreteBlanketInstallation * 50,
$row->PanelRemovalFillWithClassV * $panelRemovalCharge,
$row->BarricadedAndProtectedPanels * 20,
$row->HighProfilePanelsFrontOfBusinesses * 1100,
$row->BeforeOrAfterHoursPanels * 1500,
$row->AlleyOrDrivewayPanelWithIntegralCurb * 1200,
$row->ConcreteShortLoads4PanelsOrLess * 400,
$row->CurbAndGutter * 1200
];
调用它等于两个或更多,
最终exponent
将为零,并且函数的最后一次调用将返回exponent
。然后倒数第二个调用将返回1
,即base * 1
。那么倒数第二个呼叫将返回base
(这是base * base * 1
的平方),依此类推,直到原始呼叫将返回您请求的功率为止。
答案 1 :(得分:0)
这是调用该函数的示例:
power (2,3) // base =2 , exponent = 3
first call => (exponent is 3) !=0 // if is not true
return base * power (2 , 2) //exponent now equals 2
second Call => (exponent is 2) != 0
return base * power(2,1) // exponent now equals 1
third call => (exponent is 1) != 0
return base * power(2,0) // exponent now equals 0
fourth call => (exponent is 0) == 0
return 1;
now replace the variables with its values
fourth = 1
third = base * fourth
second = base * third
first = base * second
third = 2 * 1
second = 2 * 2
first = 2 * 4