Suppose that the function f has the following definition int

Suppose that the function f has the following definition:

int f(int a, int b) { … }

which of the following statements are legal? (Assume that i has type int , x has type

double, m has type double, n has type float)

(a) i = f(83,12);

(b) x = f(83,12);

(c) i = f((int)m,(int)n);

(d) x = f(3.15,9.28);

(e) f(83,12)

Solution

(a) i = f(83,12);: 83 and 12 are integers, and therefore, calling the function f with 2 integer input parameters, and storing the returned value in integer value is legal.

(b) x = f(83,12);: 83 and 12 are integers, and therefore, calling the funciton f with 2 integer input parameters is legal. The returned integer value is being stored into a double variable and therefore, the integer value will be casted to double and will be stored.

(c) i = f((int)m,(int)n);: m is a double variable and is casted to int. n is a float variable and is casted to int. Both the casted variables which are now integers are passed as input to the function f() which is expecting 2 integers are input. So, this is also legal. The returned integer value is stored into an integer value.

(d) x = f(3.15,9.28);: The float / double variables are being passed as input to the function f() which is expecting an integer variables as input. So, there is a possible loss of precision. In some basic languages like C, and C++ it just generates a warning about loss of precision, and only the integral part is passed as input to the function f(), and this is pretty much legal, with just a warning message. In some languages like Java, it is illegal to pass a double variables to a function which is expecting an integer and therefore, it could be illegal. The return value is an integer and therefore will be casted and stored into the double variable x.

(e) f(83,12): f is function which is expecting 2 integer variables, and therefore, this function call is also legal. Ofcourse the returned value is not being stored into an variable, But this still stands a legal statement.

Suppose that the function f has the following definition: int f(int a, int b) { … } which of the following statements are legal? (Assume that i has type int , x

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site