PDA

View Full Version : Help with C Code, Plz


planecrazy.eu
8th Apr 2008, 16:45
Hey

I know this is not really the forum, but i cant help it if you guys are so helpful compared to related forums...

Anyways

I am writing a c program.

Its has three functions.

How do i get the data from one function, to another function?

I have

Function 1

has int, float, needs to return float

Function 2

has int, float, needs to return int and float

Function 4

has int, float, no return

The data is entered in function 1, then passed to function 2 to calcualte, then to function 3 to display.

Any help, or examples, are most appreciated.

A massive thanks...

cats_five
8th Apr 2008, 16:53
You need a book on programming...

Basically you define the function as the type of data it is to return e.g.:

float myfunc()
{
....
return myfloat;
}

Can't remember the fine details, but a basic book will cover this sort of stuff.

under_exposed
9th Apr 2008, 08:08
you can pass parameters in by value or reference.

method 1, by value

float myfunc(int paramone,float param2)
{
return(2.3);
}

int mycallingfunc()
{
float myreturnval;
int firstparam = 2;
float secondparam = 4.5;

myreturnval = myfunc(firstparam,secondparam);
}



method 2, by reference

/* * means this is a pointer to this type of variable */
void myfunc(int paramone,float param2, float *retval)
{

*retval = 2.3;
}

int mycallingfunc()
{
float myreturnval;
int firstparam = 2;
float secondparam = 4.5;

/* & means pass a pointer to the variable */
myfunc(firstparam,secondparam,&myreturnval);
}

I have not put this near a compiler but this should be near what you want.
You probably want doubles rather than float (I have always used doubles rather than float).
Look out for the C programming language by kernighan and ritchie, it can be downloaded off the internet.

planecrazy.eu
9th Apr 2008, 11:22
Thanks for that...

I have to admit, it was a lazy question, simply because i needed to wrap it up, so sorry...

I have looked for that book on google, seems to bring all those amazon links up =) so adding the word to***nt seems to get me the free version.

Have read a little, however, i need to focus on the main function of the software, and getting it to work.

Functions are going to be an afterthought as i just cant seem to get them to work as they are.

If i dont declare say "a" in main as a number, ie int a = 0;

I get an error in one of my functions that says "a" has no declared value.

but in the function, i have got, a = end_value, and return int a;

Learning C was going too well, loops, arrays, etc, i am fine with, oh and the logic of using as least code as possible.

Pointers and arrays, lost =)

planecrazy.eu
10th Apr 2008, 14:44
Ok, can someone just confirm this please?

Can i only return one value per function???

I can return one, and read in as many as i like, however, cant fint a way to return more than one float variable, nor find it documented?

Cheers

cdtaylor_nats
11th Apr 2008, 00:40
You can return values in parameters that are called by reference, see the example below. Call by value parameters are shown as the first two and the call by reference is the third - note the ampersand character after int.

Value parameters work by having a local copy of the value pushed onto the stack so any changes to those parameters in the function are lost on exit. A pass by reference parameter works by pushing the address of the value onto the stack and when you exit the function any changes are saved.

void addNumbers(int, int, int&);

int main ()
{
int firstNum, secondNum, sum = 0;

cout << "Enter first number: ";

cin >> firstNum;

cout << "Enter second number: ";

cin >> secondNum;

addNumbers (firstNum, secondNum, sum);

cout << firstNum << " + " << secondNum << " = " << sum;

return 0;
}

void addNumbers (int x, int y, int& z)
{
z = x + y;
}


You also need to understand the scope of variables. If you declare a variable inside a function it only exists in that function.

under_exposed
11th Apr 2008, 12:17
Just to check, are we talking C, C++ or C#?