7 Replies - 1669 Views - Last Post: 20 December 2011 - 09:40 PM Rate Topic: -----

#1 AlbuquerqueApache   User is offline

  • D.I.C Addict

Reputation: 50
  • View blog
  • Posts: 523
  • Joined: 21-February 10

I'm such a lazy programmer.....

Posted 20 December 2011 - 07:43 AM

I want to be able to return multiple datatypes from a C function such as


int char CheckAngle (AngleA, AngleB) 
{ 
   int FirstTwoAnglesTotal = (AngleA + AngleB);

   if (!(FirstTwoAnglesTotal > 180))
   {  int RemainingAngle =(180 - FirstTwoAnglesTotal);
      return RemainingAngle; }

    else

    { char ErrorMessage=printf("This is not a Triangle you idiot!"); 
      return ErrorMessage;  

     }  

} 




my compiler just tells me I'm lazy!

This post has been edited by AlbuquerqueApache: 20 December 2011 - 07:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: I'm such a lazy programmer.....

#2 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: I'm such a lazy programmer.....

Posted 20 December 2011 - 07:57 AM

well you can't do that in C, C isn't dynamically typed. If you are just returning either a char or int then just use int as every value that can be stored in a char can be stored in a int. similarly if arguments are either going to be charor int, just make them int.

edit:
although it looks like your trying to return an error so that wouldn't really work for you. instead, return the angle by reference and return the error by value.

int CheckAngle (int* out, int AngleA, int AngleB)  { 
   int FirstTwoAnglesTotal = (AngleA + AngleB);

   if (!(FirstTwoAnglesTotal > 180)) {  
      int RemainingAngle =(180 - FirstTwoAnglesTotal);
      *out = RemainingAngle; //return value by refrence
      return 0; //everything went ok
   } else {  
      return -1; //error!! 
   }  
} 

This post has been edited by ishkabible: 20 December 2011 - 08:01 AM

Was This Post Helpful? 1
  • +
  • -

#3 AlbuquerqueApache   User is offline

  • D.I.C Addict

Reputation: 50
  • View blog
  • Posts: 523
  • Joined: 21-February 10

Re: I'm such a lazy programmer.....

Posted 20 December 2011 - 08:06 AM

Nice...

yeah, I'm new to C so I'm I havent gotten into returning info by value or reference yet.

and thanks for the INT info, would of kept me from having to write an extra function last night.

Thats the bad part of learning from the book, the author slow feeds you...


:D
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: I'm such a lazy programmer.....

Posted 20 December 2011 - 08:17 AM

as a simpler way to do the same thing, don't check the sum of the angles but instead just return 180 minus the sum of the angles. if the result is <= to 0 then you know the arguments passed didn't form a triangle.
Was This Post Helpful? 0
  • +
  • -

#5 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 455
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: I'm such a lazy programmer.....

Posted 20 December 2011 - 08:33 AM

What is often overlooked is you can also return a more complex type.

typedef struct {
    int error_code;
    int angle;
} Return_type;

Return_type foo() {
    Return_type result = {1,360};
    return result;
}



Although returning a result by indirection and an error code as return value is very common in C and very recognizable.

@ishkabible

C not supporting multiple return values has nothing to do with dynamic typing. You could perfectly design a language that has support for multiple strongly typed return values.

This post has been edited by Karel-Lodewijk: 20 December 2011 - 08:35 AM

Was This Post Helpful? 1
  • +
  • -

#6 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: I'm such a lazy programmer.....

Posted 20 December 2011 - 01:45 PM

Quote

C not supporting multiple return values has nothing to do with dynamic typing. You could perfectly design a language that has support for multiple strongly typed return values.


I never said it did; It didn't seem to me that he wanted to return 2 value but rather 1 of a different type given different scenarios. Also, His arguments have no type so It seemed like he wanted dynamic typing rather than multiple return. Because the issue can be solved by returning more than one value that is the solution I recommended(although I revised it to just return 1 value).

Quote

What is often overlooked is you can also return a more complex type.


Ya, it's probably faster to return a complex type because indirection is a bit costly. It seems like convention(as you said it's common) to return by reference and return the error by value for some reason, I'm not sure why though.

This post has been edited by ishkabible: 20 December 2011 - 01:52 PM

Was This Post Helpful? 1
  • +
  • -

#7 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: I'm such a lazy programmer.....

Posted 20 December 2011 - 02:13 PM

I dont see the relevance of multiple data types. Base your error output on the return value of the function.

#define NON_TRIANGLE_ERROR -1

int getAngle (int a, int B)/>   { 
	int c = 180 - (a + B)/>;
	return c < 180 ? NON_TRIANGLE_ERROR : c;
} 

int main() {
	int a = 90;
	int b = 100;
	int c;
	if((c = getAngle(a, B)/>) == NON_TRIANGLE_ERROR) {
		printf("Could not form triangle");
	}
	
	return 0;
}


This post has been edited by jjl: 20 December 2011 - 02:15 PM

Was This Post Helpful? 0
  • +
  • -

#8 obviousninja   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 68
  • Joined: 17-February 10

Re: I'm such a lazy programmer.....

Posted 20 December 2011 - 09:40 PM

View PostAlbuquerqueApache, on 20 December 2011 - 07:43 AM, said:

I want to be able to return multiple datatypes from a C function such as


int char CheckAngle (AngleA, AngleB) 
{ 
   int FirstTwoAnglesTotal = (AngleA + AngleB);

   if (!(FirstTwoAnglesTotal > 180))
   {  int RemainingAngle =(180 - FirstTwoAnglesTotal);
      return RemainingAngle; }

    else

    { char ErrorMessage=printf("This is not a Triangle you idiot!"); 
      return ErrorMessage;  

     }  

} 




my compiler just tells me I'm lazy!



you can't do that in C. however, if you want to return multiple data type in C there is a way. use void pointer, struct, and union. ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1