Jump to content

How to return multiple value from fuction? C

- - - - -

  • Please log in to reply
5 replies to this topic

#1
carbon

carbon

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts
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!

#2
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
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.

#3
carbon

carbon

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts

lespauled said:

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?

#4
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
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;
}

#5
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
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.
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

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
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;

}

Remebre about KISS & DRY




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users