Rules | Recent posts | topic RSS | Search | Register  | Log in

Returning values in C language

 
Post new topic  Reply to topic    EDAboard.com Forum Index -> PC Programming and Interfacing
Author Message
rizalafande



Joined: 04 Jan 2006
Posts: 11


Post08 Aug 2008 12:55   Returning values in C language

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 */
}
===========
Back to top
FvM



Joined: 22 Jan 2008
Posts: 2635
Helped: 431
Location: Bochum, Germany


Post08 Aug 2008 21:08   Returning values in C language

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



Joined: 21 Feb 2006
Posts: 220
Helped: 15


Post08 Aug 2008 23:50   Returning values in C language

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.
Back to top
mezo



Joined: 16 Jul 2007
Posts: 60
Helped: 2
Location: Egypt


Post05 Sep 2008 15:46   Re: Returning values in C language

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 .


Last edited by mezo on 08 Sep 2008 23:58; edited 1 time in total
Back to top
aospinas



Joined: 24 Nov 2003
Posts: 41
Helped: 7


Post07 Sep 2008 15:01   Re: Returning values in C language

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
Back to top
Post new topic  Reply to topic    EDAboard.com Forum Index -> PC Programming and Interfacing
Page 1 of 1 All times are GMT + 1 Hour


Abuse
Administrator
Moderators
topic RSS 
sitemap