[comp.lang.c] Passing values to functions

pjh@mccc.UUCP (Peter J. Holsberg) (02/25/88)

Dumb question:  if in an assignment like
		x = 3;
where x is declared a double, the 3 is simply widened to the size of a
double, why does this fail?
		min = minimum( min, x );
where
		double minimum( double, double );

Thanks.

-- 
Peter Holsberg                  UUCP: {rutgers!}princeton!mccc!pjh
Technology Division             CompuServe: 70240,334
Mercer College                  GEnie: PJHOLSBERG
Trenton, NJ 08690               Voice: 1-609-586-4800

gwyn@brl-smoke.ARPA (Doug Gwyn ) (02/26/88)

In article <239@mccc.UUCP> pjh@mccc.UUCP (Peter J. Holsberg) writes:
>Dumb question:  if in an assignment like
>		x = 3;
>where x is declared a double, the 3 is simply widened to the size of a
>double, why does this fail?
>		min = minimum( min, x );
>where
>		double minimum( double, double );

The key is, the conversion from int to double has to be done by code
generated by the compiler, which means that the compiler has to know
that it is needed.  If you haven't previously declared the function
prototype, or if you have declared it a la "old style":
	double minimum();
, or if you have declared it before the use of the function but your
compiler does not fully support (proposed) ANSI C rules for function
prototypes, then the types of the function parameters have no effect
on the code that is generated to pass the parameters to the function.
In such a case, the compiler will simply apply "default widening
conventions" (all integer types < long get widened to int, float to
double) to the parameters.  Then, on the other end the function is
always implemented by code that is prepared to receive the correct
(widened) parameter types; it never tries to perform further
conversions when picking up the parameters.  Therefore, the default-
widened type must agree with the type declared in the beginning of
the actual function definition.

The main reason for this rigamarole is to support separate compilation
of functions without having to pass type information (known in the
trade as "dope vectors") along with the data values for function
parameters.  In ANSI C as proposed, you'll be able to have function
prototypes in scope and thereby get the compiler to automatically
convert all parameters to matching types.  (Personally I don't think
this is a good idea.)