Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Returning values in C language

Status
Not open for further replies.

rizalafande

Junior Member level 2
Joined
Jan 4, 2006
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,435
Based on C codings below, how can i return 3 output values to be passed to other functions..
==========
float test1 (float x)
{
float a,b,c;

a = x + x;
b = x * x;
c = (x / x) - x;

return a,b,c; /* when using this style, it will only return the result of c but not together with a and b */
}
===========
 

As additional parameters, passed by reference
Code:
float test1 (float x, *a, *b)
{
*a=x+x;
*b=x+x;
}
 

You can only pass one value back in C. Either put a, b and c into a structure and pass the structure address or pass values by reference as FvM showed. If you pass references you might as well pass all three references and use the return for sucess/error codes.
 

well , rizalafande

you can do this using pointers . this code is to return 3 values from a function :

void test(int *, int *, int *); /* Prototype of function called Test */

void main()
{
int a, b, c ;
test(&a ,&b ,&c) ; /* Calling test function. */
}

void test(int *pa, int *pb, int *pc)
{
*pa = x ; /* where x, y, z is the values the function will return */
*pb = y ;
*pc = z ;
}

have a note : when you call the function test you give it an arguments
this arguments is the addresses the main function gave to to test function to put the values it want to return in .

hope this be useful .
 

Hi... is bad


Code:
#include <stdio.h>
void test(int *, int *,int *); /* Prototype of function called Test */

int main()
{
int a, b, c ;
test(&a ,&b ,&c) ; /* Calling test function. */
}

void test(int *pa, int *pb, int *pc)
{
int x=1,y=2,z=3;

*pa = x ; /* where x, y, z is the values the function will return */
*pb = y ;
*pc = z ;
}



enjoy
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top