Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird number with simple program 1

Status
Not open for further replies.

Astrocloud

Technical User
May 29, 2007
13
Hi, I am revisiting C programming after being away from it for about 10 years. I have since programmed in other languages but I forget the exact issues with C. For a refresher -I thought I would implement the following proc. After I compile it with gcc -it returns a wild number. How can I do this differently and why is it returning this weird number? Here is my rather simple program:


____________________

#include <stdio.h>

int calcKey(long Speed, long Est, long Cost, long Key) {

Key = Speed * Est + Speed * Cost + Est;

return;
}

int main() {
long heat;
calcKey(10,3,4, heat);

printf("result is %d \n", heat);
};


Thanks!
 
You can't compile this functions with a good compiler. It gives a promise to return int but return void (does not return any value).

Apropos: why int calcKey()? The function has long parameters and performs long type calculations...

The only statement in the function body do nothing. It sets Key argument copy (arguments passed by value in C). So the heat variable in the main has its old value (garbage - it was not initialized).

It seems 10 years == eternity?..

Code:
long calcKey(long Speed, long Est, long Cost) {
    return Speed * Est + Speed * Cost + Est;
}

int main() {
long heat;
heat = calcKey(10,3,4);
/* %ld format specificator for long: */
printf("result is %ld \n", heat);
};

Oh, the last question: why integral type for speed, time and cost? Obviously, you need double type for numbers...
 
No, I don't need double. I'm only using integers.

I thought that the difference between a function and a procedure -was that a function returns a value from the function call (as you wrote it) and a procedure allows you to pass values as an argument to the procedure...
(Visual Basic has ruined my C).


So what if I wanted TWO values returned from calcKey? Why would one always be forced to call calcKey as a function?


Thanks,
 
IN OTHER WORDS:

So if I wanted to calculate two values in the PROCEDURE whether or not they return int's double's or strings -I would always be forced to calculate exactly one value? This is not my recollection.


#include <stdio.h>

void calcKey(long Speed, long Est, long Cost, long Key, Double SecondValue) {

Key = Speed * Est + Speed * Cost + Est;

SecondValue = Est/Cost;
}
 
If you want to return a value, then you have to pass its address.
Code:
void calcKey(long Speed, long Est, long Cost, long* Key, Double* SecondValue) {

*Key = Speed * Est + Speed * Cost + Est;

*SecondValue = Est/Cost;
}
...
calcKey (10, 3, 4, &key, &second);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top