Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to return multiple value from fuction? C


  • Please log in to reply
5 replies to this topic

#1 carbon

carbon

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 59 posts

Posted 01 February 2012 - 06:47 AM

How to do that? I watched on Google and I found a lot of this but I dont understand. If someone can post example and explanation.

Thanks!
  • 0

#2 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 01 February 2012 - 08:31 AM

There is no way to return multiple values via a return statement, BUT there are ways to get around this limitation. In C, you can use reference parameters that could be changed in the called function. In C++, you can also use reference, or even make a class for a result that gets returned from the function.
  • 0

#3 carbon

carbon

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 59 posts

Posted 01 February 2012 - 08:32 AM

There is no way to return multiple values via a return statement, BUT there are ways to get around this limitation. In C, you can use reference parameters that could be changed in the called function. In C++, you can also use reference, or even make a class for a result that gets returned from the function.


Can you give me an example?
  • 0

#4 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 01 February 2012 - 08:46 AM

void foo(int &i) // <= notice the reference to i
{
i++;
cout << i << endl;
}

int main()
{
int i = 0;

cout << i << endl;
foo(i);
cout << i << endl;

return 0;
}
  • 0

#5 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 01 February 2012 - 09:42 AM

If you want to avoid functions having side effects on reference variables, you can also declare a struct to contain multiple values of interest. Then, your function would create an instance of this struct and assign its values, and return the single object, which itself contains multiple values.

Example:
typedef struct Multival_struct {
    int value_a;
    int value_b;
} Multival;

Multival foo() {
    Multival val;
    val.value_a = 5;
    val.value_b = 10;
    return val;
}

int main() {
    Multival answer;
    answer = foo();
    printf("%d ", answer.value_a);
    printf("%d", answer.value_b);
}
// The above would print out:
// 5 10

There you go. Two values returned from a function, wrapped in a struct.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#6 notes

notes

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 88 posts
  • Location:Poland
  • Programming Language:C++
  • Learning:Java, C#

Posted 02 February 2012 - 04:10 AM

Beside reference you can also use pointer.
It would look like :
#include <iostream>

using namespace std;

void f( int *ptr) {
    cout << *ptr << endl;
    *ptr = 120;
    cout << *ptr<< endl;

}

int main(int argc, char *argv[])
{
    int i=5;
    f(&i);
    cout << i << endl;
}

  • 0
Remebre about KISS & DRY




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download