Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,491 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,297 people online right now. Registration is fast and FREE... Join Now!




[C Language] Functions that have 2 different return types

 
Reply to this topicStart new topic

[C Language] Functions that have 2 different return types

bestbat
24 Feb, 2007 - 03:27 PM
Post #1

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
I'm just wondering, is it possible to have a function with a return of 2 different types?

e.g.
CODE
int char myfunction (void)
{
...
}

User is offlineProfile CardPM
+Quote Post

Jayman
RE: [C Language] Functions That Have 2 Different Return Types
24 Feb, 2007 - 03:51 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
A function can only return one data type. So the simple answer is No.

You can however create a Structure that is comprised of several different data types. And pass the structure out of the function. So in essence accomplishing exactly what you want, so in this respect the answer is Yes.

This tutorial will give you a pretty good idea of how to do that.
User is offlineProfile CardPM
+Quote Post

bestbat
RE: [C Language] Functions That Have 2 Different Return Types
24 Feb, 2007 - 03:54 PM
Post #3

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
Thanks.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: [C Language] Functions That Have 2 Different Return Types
24 Feb, 2007 - 04:09 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
Yes and No and Yes again. . o O (Maybe it is No, and yes, and yes again).

In general a function returns 1 thing. I can't have int Sum(int x, int y) return a double, or two integers.

Problem #1: returning two integers: To do this you would need to combine the integers into a structure.
CODE
typedef struct {
    int x;
    int y;
} Coordinate;

Coordinate Translate(int xShift, int yShift, Coordinate p);

int main()
{
    Coordinate Point;
    Point.x=0; Point.y=0;
    Translate(5, 5, Point);
    cout << Point.x << ", " << Point.y << ".\n";
    return 0;
}

Coordinate Translate(int xShift, int yShift, Coordinate p)
{
    p.x +=xShift;
    p.y +=yShift;
    return p;
}

I could also have returned a pointer to an array (this is the way I ussualy do things in assembly language).

Another way to do this is to pass the function referances to the variables I want to change.
CODE
void func(int &x, int &y);
void main()
{
int i=0;
int j=0;
func(i);
cout << i <<'\n';
return 0
}

void func(int &x, int &y) { x++;y++; return; }


Then there is the other question: Can I get a function to return either a double or an integer? We can do this with overloading, however the functions can not differ in only the return value, the call values must also be different.

CODE
int func(int a);
double func(double a);
int func(int a) { return 2 * a; }
double func(double a) { return 2 * a; }


We can even go a step farther and make a template function:
CODE

template<class T> T Max(T a, T b) { return ( a > b) ? a : b; }

int main ()
{
int a = 5, b=4, c=0;
double d = 12,  e= 2, f=0;
c = Max(a,b);
f = Max(e, d);
return 0;
}


I am SURE that there are at least 2 more ways to do one, the other, or both of these things. If you would like to know more about a particular method, ask away.
User is offlineProfile CardPM
+Quote Post

Craige
RE: [C Language] Functions That Have 2 Different Return Types
24 Feb, 2007 - 04:27 PM
Post #5

D.I.C Head
**

Joined: 2 Nov, 2006
Posts: 63


My Contributions
No, you can't. However, I'm sure if you thought about your application, you would find you don't need to.

I'm assuming you came from a background in a language such as PHP, where such a thing is possible. However, in good, strict programming, you do not need to do such a thing. A function should return only one data type, so you know what you are getting back, and there is no doubt about it. You don't have to try to figure out whether you have a char or an int, because it can only be one of them.

Please, post your code. We would be glad to help you find another way.
User is offlineProfile CardPM
+Quote Post

bestbat
RE: [C Language] Functions That Have 2 Different Return Types
24 Feb, 2007 - 04:32 PM
Post #6

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
@ NickDMax is this partially written in C++ ? As I am unfamiliar with some things such as cout <<

Also would this still work if I wanted to use the function to pass things into an array?
e.g.

I want to read from a text file:
CODE

B
1 2 3
4 5 6
7 8 9


Would what you've suggested allow me to pass 'B' into a character variable and the numbers into a 2d array?



@ Craige Actually I have no other programming background apart from having recently started to learn C. That said I do understand basic programming concepts.

I haven't yet got a code as i'm trying to create it. I basically want my function to read a file and pass the character into a variable and the numbers into an array. Both need to be passed out of the function.

This post has been edited by bestbat: 24 Feb, 2007 - 04:32 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: [C Language] Functions That Have 2 Different Return Types
24 Feb, 2007 - 06:24 PM
Post #7

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
WOW almost needed help there myself. BTW C defines array int a[n] zero to n-1 so that there are n members. I ignored this and had a hard time tracking down why my program was screwy.

Here is an example of passing an array. Unlike single dimentional arrays you must tell the compiler the size of the array in all but one of the dimentions void func(int data[][100]); in most cases it is better to define all the dimentions.
CODE
#include <stdio.h>

void func(char ch, int Array[3][3]);

int main()
{
    char Chr = 'B';
    int A[3][3];
    int i, j;
    func(Chr, A);
    for (i=0; i<3; i++) {
        for (j=0;j<3;j++) {
            printf("%d ", A[i][j]);
        }
        printf("\n");

    }

    return 0;
}

void func(char ch, int Array[3][3])
{
    int i, j;
    ch++; // Do something with ch...
    
    //initialize the array with values
    for (i=0; i<3; i++) {
        for (j=0;j<3;j++) {
            Array[i][j]=1+i+3*j;
        }
    }
    return;
}


The above code is an example of passing a multidimentional array.

Yes the earlier post was C++ though I think that with the exception of the cout it is all C compatable.

This post has been edited by NickDMax: 24 Feb, 2007 - 06:25 PM
User is offlineProfile CardPM
+Quote Post

bestbat
RE: [C Language] Functions That Have 2 Different Return Types
24 Feb, 2007 - 06:35 PM
Post #8

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
Thanks NickDMax, that has been useful in my case the array size is determined by the user so I would need to use malloc, but this is a useful guide.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: [C Language] Functions That Have 2 Different Return Types
24 Feb, 2007 - 07:11 PM
Post #9

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
lol, I deleted the bit about passing a pointer to an array because I thought that would be a little advanced. If you are working with multidimentional dynamic arrays yes you need to write your own little access routines to calculate the correct position. row * (col width) + col works well for two dimentional arrays.

CODE
void func(int *array);

void func(int *array, int ColSize, int RowSize) {
int i;
int j;
for (i=0;i<RowSize;i++) {
    for (j=0;j<ColSize;i++) {
        array[i*ColSize+j]=i*ColSize+j +1;
    }
}
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 05:34PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month