92.4k views
5 votes
What will be returned if f(a,b) is called in the following functions? function g(int n) { if (n>0) return 1; else return -1; function f(int a, int b) if (a>b) return gla-b); if (a

User WeZZard
by
8.1k points

2 Answers

5 votes

Final answer:

When f(a, b) is called, it invokes g(a - b) if a is greater than b, with g returning 1 if the argument is positive, and -1 if negative. If a and b are equal, f(a, b) doesn't return an explicit value.

Step-by-step explanation:

The function f(a, b) calls another function g(n) inside of it. If a is greater than b, then f(a, b) evaluates to g(a - b). The function g(n) returns 1 if n is greater than 0, otherwise it returns -1. If a is not greater than b, the function f(a, b) would execute the second if statement and the resulting value depends on the evaluation of g(b - a).

If both a and b are equal, neither if condition in f is satisfied, and the function does not explicitly return a value. In many programming languages, this might imply an implicit return of undefined, null, or a similar value indicating the absence of a return value, or an error might be raised for missing return statement.

User Xjshiya
by
7.8k points
4 votes

Final answer:

Depending on the values of 'a' and 'b', function f(a, b) may return 1 if a > b and a - b > 0, or -1 if a > b and a - b ≤ 0. Without the full details for when a ≤ b, the exact return value in that scenario cannot be determined.

Step-by-step explanation:

If f(a, b) is called in the given functions, there are multiple possible outcomes depending on the values of a and b. The function g(int n) will return 1 if n is greater than 0, otherwise, it will return -1. Now when you call f(a, b), it first checks if a is greater than b. If so, it calls g(a - b), which will return 1 if a - b is positive, or -1 if a - b is zero or negative.
If a is not greater than b, nothing is immediately returned, and you have a second condition which hasn't been fully described in the question. Assuming that condition is meant to compare a and b, and possibly call g(b - a), the return would depend on whether b - a is positive or not. Without the full description of the f function, we can't definitively say what will be returned if a is not greater than b.

User DrCopyPaste
by
9.1k points