Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!hc!lll-winken!uunet!portal!cup.portal.com!Tim_CDC_Roberts From: Tim_CDC_Roberts@cup.portal.com Newsgroups: comp.lang.c Subject: Re: Problem returning doubles from a function Message-ID: <16197@cup.portal.com> Date: 25 Mar 89 00:46:13 GMT References: <39722@csvax1.cs.tcd.ie> Organization: The Portal System (TM) Lines: 50 In <39722@csvax1.cs.tcd.ie>, omahony@csvax1.cs.tcd.ie (Donal O'Mahony) writes: > Can anone explain the problem with the following program (made up of > 2 files). > > ::main.c > #include > main() > {double a,b; > > a = 1.234; > b = store_length(a); > printf("b=%f\n",b); > } > > ::rt1.c > double store_length( double measurements) > { measurements= 2; > printf("Returning measurements=%f\n",measurements); > return(measurements); > } > > When these two programs are linked and run, they do not print 'b=2,' > as I would expect. Why? Once again, I am sure there will be 24 other replies to this, all of which will cross in the net. In main.c, since you did not declare that store_length returns a double, the compiler will assume it returns an int. It will then try to cast that to a double, thereby rendering it undecipherable. Modify main.c thusly: #include double store_length (double); main() {double a,b; a = 1.234; b = store_length(a); printf("b=%f\n",b); } (Note to those who will warn me about the ANSI header: it can be deduced from his file rt1.c that he is using a compiler which supports the prototypes.)