Click here to Skip to main content
16,015,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How is it possible to get value from void function?
Please give example.
Thank You.
Posted
Updated 29-Mar-11 15:30pm
v2
Comments
Monjurul Habib 29-Mar-11 13:30pm    
please clarify your question.
HimanshuJoshi 29-Mar-11 21:30pm    
Edited to remove bold tag.

Pass value back by reference...
void func1(int &value){value = some_value;}

to use:
int x;
func1(&x);
 
Share this answer
 
Comments
UL UL ALBAB 29-Mar-11 15:17pm    
Thank you...
UL UL ALBAB 29-Mar-11 15:22pm    
Than I have another question. How is it possible to return two values from any type function such as int fun() or char fun() ?
Is it possible to return two values ?
Albert Holguin 29-Mar-11 15:58pm    
yes, just follow the same sort of guide (with any additional parameters)...
int func(int &value1, int &value2)
if you want 3 values
int func(int &value1, int &value2, int &value3)
and so on...
Sergey Alexandrovich Kryukov 29-Mar-11 16:24pm    
Albert, this is technically correct, but there is no situation when it should be done.
Yes, you answered OP question, but why?
Please see my Answer.
--SA
if you are not satisfied with the answer of Alber Holguin, just see this example.
The following example show you how to retrieve value from void type function. int *p, here pass a variable from main() function and that variable will be changed inside fun() function.
It calls pass value by reference.

C
#include<stdio.h>
#include<conio.h>
void fun(int *p);/* function prototype */
int main()
{
 clrscr();
 int x=0;
 fun(&x); /* function call*/
 printf("%d",x);
 getch();
return 0;
}
void fun(int *p) /*function body/definition*/
{
 *p=13;/*any int value*/
}
 
Share this answer
 
Comments
Albert Holguin 29-Mar-11 14:52pm    
he better be satisfied with my answer! jk! :p
UL UL ALBAB 29-Mar-11 15:17pm    
Many many thanks both of you.
Аslam Iqbal 29-Mar-11 15:21pm    
you are welcome.:)
Sergey Alexandrovich Kryukov 29-Mar-11 16:26pm    
Aslam, this is a good explanation and technically correct answer (but by reference would be better), but why?
There is no situation when such thing is practically needed, so it should never be done.
Please see my Answer.
--SA
In addition to the other answers, which are technically correct: if your really need to return something, using a void function makes no sense at all. After all, what is the return parameter for?!

I would somehow understand if you already have one return parameter and then need to add something else to return. The answer would be 1) return second value using a parameter by reference; 2) using pointer passed by value and changing the pointed object in the code of your function (as it is done on C); 3) using return parameter, but with a different type such as class or struct, so all your return data would be passed in the members of the class/struct.

But no, you're asking about returning something from a void function. This is really an oxymoron. In practice, there is no situation when it can be needed.

[EDIT]

Example:
//makes no sense:
void func1(int &value){ value = some_value; }

//how it should be:
int func1(){ return some_value; }


Same thing with any other parameter type. With reference or pointer type a caution should be used: the object should exist even when an instance of the class is destroyed, but this is exactly same thing as with passing the our parameter by reference. Read also on the concept of l-value in any C++ manual.

[END EDIT]

—SA
 
Share this answer
 
v3
Comments
Albert Holguin 29-Mar-11 16:38pm    
it is a bit of an oxymoron... but I answered nonetheless... :)
Sergey Alexandrovich Kryukov 29-Mar-11 17:30pm    
Yes, you did, so did I. The only essential difference is I point it out.
--SA
UL UL ALBAB 30-Mar-11 5:08am    
@SAKryukov :: Can you show me a example as using return parameter, but with a different type such as struct and class ??
Thank you
Sergey Alexandrovich Kryukov 30-Mar-11 16:28pm    
OK, see my update. I put example with int (not struct or class as you asked), but there is no difference. Please also see my notes below the codelet.
--SA
In addition to above valuable answers, cheating a little bit. :-)
void voidFunc(const int nVal)
{
    int nRet = nVal + 8;
#ifdef __GNUC__	
    asm("movl %0, %%eax"
        : 
        : "r"(nRet)
        : "%eax"
       );
#else
    _asm mov eax, nRet
#endif
}

int main(void)
{
    int nRet = 0;
    voidFunc(10);

#ifdef __GNUC__	
    asm("movl %%eax, %0"
        :"=r"(nRet)
        :
        :"%eax"
       );
#else
    _asm mov nRet, eax
#endif

    printf("Result is %d\n", nRet);

    return 0;
}

Disclaimer:
This is not a competitor solution, and is also "as is".
 
Share this answer
 
Comments
Albert Holguin 29-Mar-11 20:15pm    
assembly? now you're just showing off! :p
Ozer Karaagac 29-Mar-11 21:00pm    
Not really. I don't have any more useful job to do for now.
And also it would be really a diverse way of returning value from void (though not recommended). I thought, maybe, someone might want to know.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900