470,571 Members | 2,420 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,571 developers. It's quick & easy.

return function

Hi,I am confused and need the answer of the question mentioned below soon.
return(x,y) is valid?
Can a return() function return more than one value within one statement.

eg:
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<iostream.h>
  3. int sum(int i,int j);
  4. main()
  5. {
  6.  int a,b,k,l;
  7.  char c;
  8.  a=3,b=4;
  9.  k= sum(a,b);
  10.  l=sum(a,b);
  11.  printf("%d %d",k,l);
  12.  c= getchar();
  13.  return 0;    
  14. }     
  15.  
  16. int sum(int i,int j)
  17. {
  18.     int kk,ll;
  19.     kk = i + j;
  20.     ll = i * j;
  21.     return(kk,ll);
  22. }
Earlier I thought compiler should give a compile time error but instead it executed the code and retun 12 12 as the answer.WHY????????
Jan 25 '08 #1
3 1464
Ganon11
3,652 Expert 2GB
, is an operator, just like +, ||, ==, etc. So returning kk, ll actually returns (I think) ll, as the comma operator returns the rightmost expression. Of course, the function isn't returning both kk and ll, it is only returning 1, so there is no reason to ever use the comma operator in a return statement.
Jan 25 '08 #2
hsn
237 100+
very basic and easy.
'return' returns only one variable. no more than that.
you can return an array, vector, int,long.........
anything but only one variable.

GOOD LUCK
Jan 25 '08 #3
Thank you every one...for a quick response...though it was a basic question but i got confussed :(.....never mind thanks a ton
Jan 25 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

17 posts views Thread by strout | last post: by
10 posts views Thread by LaEisem | last post: by
23 posts views Thread by Nascimento | last post: by
3 posts views Thread by Thomas Scheiderich | last post: by
12 posts views Thread by Michael Maes | last post: by
7 posts views Thread by nafri | last post: by
2 posts views Thread by mosesdinakaran | last post: by
7 posts views Thread by Terry Olsen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.