Return value

Hi,
I was wondering if a called function can have two return values. Let say I have a function that gets students' id numbers and also counts how many student IDs were entered. Can I write: return id, students; and then in the main function write id = Getid(); students = Getid(); ?
Thanks!

Comments

  • Hi,
    in C/C++ you can return only one value. It's possible to put two smaller values into one value or to return an allocated array of values, but this is rarely used and not very elegant in this situation.

    A good solution is to point the function to the variables and let the function fill them in:
    [code]
    int students, id;
    Getid(&students, &id);

    ...

    void Getid (int* pStudents, int* pId)
    {
    *pStudents = 3;
    *pId = 1;
    }
    [/code]
    or:
    [code]
    int students, id;
    id = Getid(&students);

    ...

    int Getid (int* pStudents)
    {
    *pStudents = 3;
    return 1;
    }
    [/code]
  • I'm not as knowledgeable to understand the idea and the codes. But "no" is very clear. Thank you very much!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories