James R. Kuyper

2016-06-20 22:22:33 UTC

Permalink

Raw Message

... "In preparing for the call to a function, the arguments are

evaluated, and each parameter is assigned the value of the

corresponding argument."

"On entry to the function, the size expressions of each variably

modified parameter are evaluated and the value of each argument

expression is converted to the type of the corresponding parameter as

if by assignment."

The last quote confuses me - are the argument expressions available

at function entry? Besides what are those conversions that are talked

about?

The argument expressions are not available from within the function, butevaluated, and each parameter is assigned the value of the

corresponding argument."

"On entry to the function, the size expressions of each variably

modified parameter are evaluated and the value of each argument

expression is converted to the type of the corresponding parameter as

if by assignment."

The last quote confuses me - are the argument expressions available

at function entry? Besides what are those conversions that are talked

about?

they are available while preparing to call the function.

Consider the following code:

int func(unsigned short us, float f) { return f/us; }

long l = 300;

double d = 24.3;

int i = func(l*500L, d*2.0);

l*500L and d*2.0 are the argument expressions that it refers to.

When it says "the arguments are evaluated", it means that the expression

l*500L is evaluated to give a value of 150000L, and that d*2.0 is

evaluated to give a value of 48.6.

"assigned the value of the corresponding argument" means that func()

behaves as if the following two lines were executed before the start of

the program:

us = 150000L;

f = 48.6;

"converted to the type of the corresponding parameter as if by

assignment" is made very clear in the above code, because I've

re-written it "as if by assignment". The value 150000L has the type

'long', while 'us' has the type 'unsigned short'. This means that

150000L must be converted to "unsigned short" before saving the value in

'us'. If USHRT_MAX has a value of 65535, then conversion to unsigned

short involves subtracting 65536 from 150000L twice, giving a value of

18928.

Similarly, 48.6 has the type double, while 'f' has the type float, so

48.6 has to be converted to type float. The conversion will generally

result in a small change in the value: the double constant 24.3 cannot

be represented exactly unless FLT_RADIX is an integer multiple of 10.

The actual value of that constant will be either the largest

representable double that is less than 24.3, or the smallest

representable double that is greater than 24.3. When d*2.0 is converted

to float, it will, at best, be converted to either the largest

representable float that is smaller than the double value, or the

smallest representable float that is larger than the double value. In

general, this will not be precisely the same value as d*2.0.