196k views
4 votes
Consider these functions:_________.

def f(x) :
return g(x) + math.sqrt(h(x))
def g(x):
return 4 h(x)
def h(x):
return x x + k(x)-1
def k(x):
return 2 (x + 1)
Without actually compiling and running a program, determine the results of the following function calls.
a. x1 = f(2)
b. x2 = g(h(2)
c. x3 = k(g(2) + h(2))
d. x4 - f(0) + f(l) + f(2)
e. x5 - f{-l) + g(-l) + h(-1) + k(-l)

User Karhgath
by
6.0k points

1 Answer

3 votes

Answer:

x1 = 39

x2 = 400

x3 = 92

x4 = 62

x5 = 0

Step-by-step explanation:

a. x1 = f(2)

This statement calls the f() function passing 2 to the function. The f(x) function takes a number x as parameter and returns the following:

g(x) + math.sqrt(h(x))

This again calls function g() and h()

The above statement calls g() passing x i.e. 2 to the function g(x) and calls function h() passing x i.e. 2 to h() and the result is computed by adding the value returned by g() to the square root of the value returned by the h() method.

The g(x) function takes a number x as parameter and returns the following:

return 4*h(x)

The above statement calls function h() by passing value 2 to h() and the result is computed by multiplying 4 with the value returned by h().

The h(x) function takes a number x as parameter and returns the following:

return x*x + k(x)-1

The above statement calls function k() by passing value 2 to k() and the result is computed by subtracting 1 from the value returned by k() and adding the result of x*x (2*2) to this.

The k(x) function takes a number x as parameter and returns the following:

return 2 * (x + 1)

As the value of x=2 So

2*(2+1) = 2*(3) = 6

So the value returned by k(x) is 6

Now lets go back to the function h(x)

return x*x + k(x)-1

x = 2

k(x) = 6

So

x*x + k(x)-1 = 2*2 + (6-1) = 4 + 5 = 9

Now lets go back to the function g(x)

return 4*h(x)

As x = 2

h(x) = 9

So

4*h(x) = 4*9 = 36

Now lets go back to function f(x)

return g(x) + math.sqrt(h(x))

As x=2

g(x) = 36

h(x) = 9

g(x) + math.sqrt(h(x)) = 36 + math.sqrt(9)

= 36 + 3 = 39

Hence

x1 = 39

b. x2 = g(h(2) )

The above statement means that first the function g() calls function h() and function h() is passed a value i.e 2.

As x=2

The function k() returns:

2 * (x + 1) = 2 * (2 + 1) = 6

The function h() returns:

x*x + k(x)-1 = 2*2 + (6-1) = 4 + 5 = 9

Now The function g() returns:

4 * h(x) = 4 * h(9)

This method again calls h() and function h() calls k(). The function k() returns:

2 * (x + 1) = 2 * (9 + 1) = 20

Now The function h() returns:

x*x + k(x)-1 = 9*9 + (20-1) = 81 + 19 = 100

h(9) = 100

Now The function g() returns:

4 * h(x) = 4 * h(9) = 4 * 100 = 400

Hence

x2 = 400

c. x3 = k(g(2) + h(2))

g() returns:

return 4 h(x)

h() returns:

return x*x + k(x)-1

k(2) returns:

return 2 (x + 1)

= 2 ( 3 ) = 6

Now going back to h(2)

x * x + k(x)-1 = 2*2 + 6 - 1 = 9

Now going back to g(2)

4 h(x) = 4 * 9 = 36

So k(g(2) + h(2)) becomes:

k(9 + 36 )

k(45)

Now going to k():

return 2 (x + 1)

2 (x + 1) = 2(45 + 1)

= 2(46)

= 92

So k(g(2) + h(2)) = 92

Hence

x3 = 92

d. x4 = f(0) + f(1) + f(2)

Compute f(0)

f() returns:

return g(0) + math.sqrt(h(0))

f() calls g() and h()

g() returns:

return 4 * h(0)

g() calls h()

h() returns

return 0*0 + k(0)-1

h() calls k()

k() returns:

return 2 * (0 + 1)

2 * (0 + 1) = 2

Going back to caller function h()

Compute h(0)

0*0 + k(0)-1 = 2 - 1 = 1

Going back to caller function g()

Compute g(0)

4 * h(0) = 4 * 1 = 4

Going back to caller function f()

compute f(0)

g(0) + math.sqrt(h(0)) = 4 + 1 = 5

f(0) = 5

Compute f(1)

f() returns:

return g(1) + math.sqrt(h(1))

f() calls g() and h()

g() returns:

return 4 * h(1)

g() calls h()

h() returns

return 1*1 + k(1)-1

h() calls k()

k() returns:

return 2 * (1 + 1)

2 * (1 + 1) = 4

Going back to caller function h()

Compute h(0)

1*1 + k(1)-1 = 1 + 4 - 1 = 4

Going back to caller function g()

Compute g(1)

4 * h(1) = 4 * 4 = 16

Going back to caller function f()

compute f(1)

g(1) + math.sqrt(h(1)) = 16 + 2 = 18

f(1) = 18

Compute f(2)

f() returns:

return g(2) + math.sqrt(h(2))

f() calls g() and h()

g() returns:

return 4 * h(2)

g() calls h()

h() returns

return 1*1 + k(2)-1

h() calls k()

k() returns:

return 2 * (2+1)

2 * (3) = 6

Going back to caller function h()

Compute h(2)

2*2 + k(2)-1 = 4 + 6 - 1 = 9

Going back to caller function g()

Compute g(2)

4 * h(2) = 4 * 9 = 36

Going back to caller function f()

compute f(2)

g(2) + math.sqrt(h(2)) = 36 +3 = 39

f(1) = 13.7

Now

x4 = f(0) + f(l) + f(2)

= 5 + 18 + 39

= 62

Hence

x4 = 62

e. x5 = f(-1) + g(-1) + h(-1) + k(-1)

Compute f(-1)

f() returns:

return g(-1) + math.sqrt(h(-1))

f() calls g() and h()

g() returns:

return 4 * h(-1)

g() calls h()

h() returns

return 1*1 + k(-1)-1

h() calls k()

k() returns:

return 2 * (-1+1)

2 * (0) = 0

Going back to caller function h()

Compute h(-1)

-1*-1 + k(-1)-1 = 1 + 0 - 1 = 0

Going back to caller function g()

Compute g(-1)

4 * h(-1) = 4 * 0 = 0

Going back to caller function f()

compute f(-1)

g(-1) + math.sqrt(h(-1)) = 0

f(-1) = 0

Compute g(-1)

g() returns:

return 4 * h(-1)

g() calls h()

h() returns

return 1*1 + k(-1)-1

h() calls k()

k() returns:

return 2 * (-1+1)

2 * (0) = 0

Going back to caller function h()

Compute h(-1)

-1*-1 + k(-1)-1 = 1 + 0 - 1 = 0

Going back to caller function g()

Compute g(-1)

4 * h(-1) = 4 * 0 = 0

g(-1) = 0

Compute h(-1)

h() returns

return 1*1 + k(-1)-1

h() calls k()

k() returns:

return 2 * (-1+1)

2 * (0) = 0

Going back to caller function h()

Compute h(-1)

-1*-1 + k(-1)-1 = 1 + 0 - 1 = 0

h(-1) = 0

Compute k(-1)

k() returns:

return 2 (x + 1)

k(-1) = 2 ( -1 + 1 ) = 2 ( 0 ) = 0

k(-1) = 0

x5 = f(-1) + g(-1) + h(-1) + k(-1)

= 0 + 0 + 0 + 0

= 0

Hence

x5 = 0

User Ridae HAMDANI
by
5.3k points