## Calling a function only when needed

## Calling a function only when needed

(OP)

Hi,

In a Mathcad program, I noticed that a function is recalculated while it does not need to be. Here is an example:

f(x):= x^2 + 1

g(x,y):= f(x) + y

y:= 0,1 .. 10

[graph of g(x,y) vs. y]

Normally, when y is varied, f(x) should not be recalculated, it should be calculated only for the first value of y. But I noticed that f(x) is calculated for each value of y.

Is there a way to force the program to calculate f(x) only once for all?

Thanks a lot,

JC

In a Mathcad program, I noticed that a function is recalculated while it does not need to be. Here is an example:

f(x):= x^2 + 1

g(x,y):= f(x) + y

y:= 0,1 .. 10

[graph of g(x,y) vs. y]

Normally, when y is varied, f(x) should not be recalculated, it should be calculated only for the first value of y. But I noticed that f(x) is calculated for each value of y.

Is there a way to force the program to calculate f(x) only once for all?

Thanks a lot,

JC

## RE: Calling a function only when needed

TTFN

FAQ731-376: Eng-Tips.com Forum Policies

Chinese prisoner wins Nobel Peace Prize

## RE: Calling a function only when needed

i=0,1;10

g(f(x),i)=fx+y

?

## RE: Calling a function only when needed

TTFN

FAQ731-376: Eng-Tips.com Forum Policies

Chinese prisoner wins Nobel Peace Prize

## RE: Calling a function only when needed

(Your example would be f[x:=x^2+1 and g[x,y:=f[x +y)