<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Get return value from function returning void]]></title><description><![CDATA[<p>Hello.</p>
<p>I have the following function:</p>
<pre><code class="language-cpp">void f(int a)
{
  return;
}
</code></pre>
<p>Then I trying to get its return value:</p>
<pre><code class="language-cpp">int x = f(10);
</code></pre>
<p>Compiler says that it cannot convert <code>void</code> to <code>int</code> .</p>
<p>I tryed to do this way:</p>
<pre><code class="language-cpp">int x = (int)f(10);
</code></pre>
<p>Still can't convert.</p>
<p>So, I decided to make variable <code>x</code> the same type as returning type of <code>f</code> :</p>
<pre><code class="language-cpp">void x = f(10);
</code></pre>
<p>Still error. What I am doing wrong? I have learned that call of function is an <em>expression</em>, i.e. it has <em>value</em> which can be assigned somewhere.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/250786/get-return-value-from-function-returning-void</link><generator>RSS for Node</generator><lastBuildDate>Tue, 15 Sep 2026 21:43:03 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/250786.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 24 Sep 2009 08:05:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Get return value from function returning void on Thu, 24 Sep 2009 09:02:59 GMT]]></title><description><![CDATA[<p>Hello.</p>
<p>I have the following function:</p>
<pre><code class="language-cpp">void f(int a)
{
  return;
}
</code></pre>
<p>Then I trying to get its return value:</p>
<pre><code class="language-cpp">int x = f(10);
</code></pre>
<p>Compiler says that it cannot convert <code>void</code> to <code>int</code> .</p>
<p>I tryed to do this way:</p>
<pre><code class="language-cpp">int x = (int)f(10);
</code></pre>
<p>Still can't convert.</p>
<p>So, I decided to make variable <code>x</code> the same type as returning type of <code>f</code> :</p>
<pre><code class="language-cpp">void x = f(10);
</code></pre>
<p>Still error. What I am doing wrong? I have learned that call of function is an <em>expression</em>, i.e. it has <em>value</em> which can be assigned somewhere.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1783416</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1783416</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 24 Sep 2009 09:02:59 GMT</pubDate></item><item><title><![CDATA[Reply to Get return value from function returning void on Thu, 24 Sep 2009 08:06:54 GMT]]></title><description><![CDATA[<p>Troll oder schlechter Witz?! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1783417</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1783417</guid><dc:creator><![CDATA[Trollhunter]]></dc:creator><pubDate>Thu, 24 Sep 2009 08:06:54 GMT</pubDate></item><item><title><![CDATA[Reply to Get return value from function returning void on Thu, 24 Sep 2009 08:09:49 GMT]]></title><description><![CDATA[<p>Nix Troll, fauler Typi, der sich nicht die Mühe macht, einen Post den er in ein englisches Forum gestellt hat ins Deutsche zu übersetzen.</p>
<p>Das Problem hat eine einfache Lösung:<br />
void als return-Wert heißt halt mal &quot;ich gebe nix zurück&quot;, da kannst du lange versuchen etwas zuzuweisen...<br />
Wenn du was zurückgeben willst, musst du deine Funktion anpassen.<br />
(Mannmannmann, seit 2007 ein C++Forum-Member....)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1783420</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1783420</guid><dc:creator><![CDATA[rotonder]]></dc:creator><pubDate>Thu, 24 Sep 2009 08:09:49 GMT</pubDate></item><item><title><![CDATA[Reply to Get return value from function returning void on Thu, 24 Sep 2009 08:11:56 GMT]]></title><description><![CDATA[<p><strong>Trollhunter</strong>, really, not!</p>
<p>I am an old member here. I just trying to get <em>deep</em> knowledge of C++ and all its internals.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1783421</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1783421</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 24 Sep 2009 08:11:56 GMT</pubDate></item><item><title><![CDATA[Reply to Get return value from function returning void on Thu, 24 Sep 2009 08:16:38 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p>Still error. What I am doing wrong? I have learned that call of function is an <em>expression</em>, i.e. it has <em>value</em> which can be assigned somewhere.</p>
</blockquote>
<p>Yes, it is an expression. No, it doesn't have a value (at least none which can be assigned somewhere). You can't declare variables of type void. The only thing you can do with a void expression is return it from a void function:</p>
<pre><code class="language-cpp">void g();
void f() {
  return g();
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1783423</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1783423</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Thu, 24 Sep 2009 08:16:38 GMT</pubDate></item><item><title><![CDATA[Reply to Get return value from function returning void on Thu, 24 Sep 2009 08:18:01 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p><strong>Trollhunter</strong>, really, not!</p>
<p>I am an old member here. I just trying to get <em>deep</em> knowledge of C++ and all its internals.</p>
</blockquote>
<ol>
<li>this is a german C++-Forum. Aren't there any good english ones?</li>
<li>void means nothing. returning void means returning nothing. And how one should apply nothing to an integer? and how sould &quot;nothing&quot; have a representation in memory?<br />
What you often see in C-Apps is returning void-POINTERS; those can of course be casted, as a pointer has an adress.</li>
</ol>
]]></description><link>https://www.c-plusplus.net/forum/post/1783424</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1783424</guid><dc:creator><![CDATA[pseudomappe]]></dc:creator><pubDate>Thu, 24 Sep 2009 08:18:01 GMT</pubDate></item><item><title><![CDATA[Reply to Get return value from function returning void on Thu, 24 Sep 2009 08:21:19 GMT]]></title><description><![CDATA[<p>Today I will give a lection to my students, and I need to clarify for them, what is the difference between <em>operator</em>, <em>expression</em> (also <em>l-value</em> and <em>r-value</em>). The problem is that function returning <code>void</code> seems to break this classification.</p>
<p>P.S. Long time ago this forum was the top one in Google, so I decided to register. Sorry, I do not speak German.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1783425</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1783425</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 24 Sep 2009 08:21:19 GMT</pubDate></item><item><title><![CDATA[Reply to Get return value from function returning void on Thu, 24 Sep 2009 08:21:45 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/17152">@SAn</a>: Just ignore the unregistered users. We still know you <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1783429</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1783429</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Thu, 24 Sep 2009 08:21:45 GMT</pubDate></item><item><title><![CDATA[Reply to Get return value from function returning void on Thu, 24 Sep 2009 08:25:05 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p>The problem is that function returning <code>void</code> seems to break this classification.</p>
</blockquote>
<p>I found it helpful to realize that void is an incomplete type, which, unlike a class type without a full definition, cannot be completed.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1783432</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1783432</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Thu, 24 Sep 2009 08:25:05 GMT</pubDate></item><item><title><![CDATA[Reply to Get return value from function returning void on Thu, 24 Sep 2009 08:44:42 GMT]]></title><description><![CDATA[<p>void is an incomplete type that cannot be completed. Therefore, no object of that type can exist. Consequently, no l-value expression of this type exists either.<br />
A value of type void, however, does exist essentially meaning &quot;nothing&quot;. There are (at least - hope I didnt forget one) 3 kinds of expressions of that type:<br />
- a call to a function with the return type void<br />
- a cast to type void, including the form void()<br />
- throw expressions though these never actually do yield a value of void<br />
All these expressions are r-value expressions which fits nicely into the system. An expression of type void may be the argument of a return statement in a void function (this is illegal in C btw), because there is no reason not to allow it, that helps to write generic code as well.</p>
<p>A pointer of type <em>cv</em> void* is something different - this is a reuse of the keyword void roughly meaning &quot;pointer to any type&quot;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1783441</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1783441</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 24 Sep 2009 08:44:42 GMT</pubDate></item><item><title><![CDATA[Reply to Get return value from function returning void on Thu, 24 Sep 2009 09:02:09 GMT]]></title><description><![CDATA[<p>Bashar schrieb:</p>
<blockquote>
<p>You can't declare variables of type void. The only thing you can do with a void expression is return it from a void function:</p>
<pre><code class="language-cpp">void g();
void f() {
  return g();
}
</code></pre>
</blockquote>
<p>Excellent! I can return it! So, call to a void function is indeed an expression.</p>
<p>Bashar schrieb:</p>
<blockquote>
<p>I found it helpful to realize that void is an incomplete type, which, unlike a class type without a full definition, cannot be completed.</p>
</blockquote>
<p>So, I cannot instantiate it (cannot make a variable of this type). Good explanation.</p>
<p>Also I have found the following: <a href="http://wiki.answers.com/Q/Why_can_you_not_declare_void_variables_in_c" rel="nofollow">http://wiki.answers.com/Q/Why_can_you_not_declare_void_variables_in_c</a><br />
It seems a little wrong to me, because I have checked: <code>sizeof(void)==0</code> . So, its looks like <code>void</code> type <em>has</em> size (zero size).</p>
<p>Also I have figured out the following</p>
<pre><code class="language-cpp">struct S
{
};

S f(int x)
{
	S s;
	return s;
}

...

void; void; void; //Operator? Expression?

s S = f(10);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1783446</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1783446</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 24 Sep 2009 09:02:09 GMT</pubDate></item><item><title><![CDATA[Reply to Get return value from function returning void on Thu, 24 Sep 2009 09:00:47 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>void is an incomplete type that cannot be completed. Therefore, no object of that type can exist. Consequently, no l-value expression of this type exists either.<br />
A value of type void, however, does exist essentially meaning &quot;nothing&quot;. There are (at least - hope I didnt forget one) 3 kinds of expressions of that type:<br />
- a call to a function with the return type void<br />
- a cast to type void, including the form void()<br />
- throw expressions though these never actually do yield a value of void<br />
All these expressions are r-value expressions which fits nicely into the system. An expression of type void may be the argument of a return statement in a void function (this is illegal in C btw), because there is no reason not to allow it, that helps to write generic code as well.</p>
<p>A pointer of type <em>cv</em> void* is something different - this is a reuse of the keyword void roughly meaning &quot;pointer to any type&quot;</p>
</blockquote>
<p>So, as always, <strong>Camper</strong> came with complete explanation <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /><br />
Thank you!</p>
<p>Added later:</p>
<pre><code class="language-cpp">void g(void)
{
	return void();
	return void(10);
	return (void)10;
	return static_cast&lt;void&gt;(10);
}
</code></pre>
<p>That is impressing me!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1783450</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1783450</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 24 Sep 2009 09:00:47 GMT</pubDate></item><item><title><![CDATA[Reply to Get return value from function returning void on Thu, 24 Sep 2009 09:17:11 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p>It seems a little wrong to me, because I have checked: sizeof(void)==0.</p>
</blockquote>
<p>That seems wrong</p>
<p>C++03 Standard schrieb:</p>
<blockquote>
<p>5.3.3/1 ...<br />
The sizeof operator shall not be applied to an expression that has function or incomplete type, or to an enumeration type before all its enumerators have been declared, or to the parenthesized name of such types, or to an lvalue that designates a bit-field.</p>
</blockquote>
<p>void is an incomplete type, so sizeof(void) is ill-formed.<br />
This applies to C as well (6.5.3.4/1)</p>
<p>Seems your compiler isn't conforming.</p>
<p>And</p>
<pre><code class="language-cpp">void;
</code></pre>
<p>is ill-formed as well. Just like</p>
<pre><code class="language-cpp">int;
</code></pre>
<p>would be.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1783465</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1783465</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 24 Sep 2009 09:17:11 GMT</pubDate></item><item><title><![CDATA[Reply to Get return value from function returning void on Thu, 24 Sep 2009 09:25:53 GMT]]></title><description><![CDATA[<p>Ok. Now I see that all these <em>void</em> things are rather consistent.</p>
<p>P.S. Using Visual Studio 2008 compiler.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1783470</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1783470</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 24 Sep 2009 09:25:53 GMT</pubDate></item><item><title><![CDATA[Reply to Get return value from function returning void on Thu, 24 Sep 2009 09:31:42 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p>P.S. Using Visual Studio 2008 compiler.</p>
</blockquote>
<pre><code class="language-cpp">void foo() { sizeof(void); }
</code></pre>
<p>Visual C++ 2008 schrieb:</p>
<blockquote>
<p>error C2070: 'void': illegal sizeof operand</p>
</blockquote>
<p>what kind of code did you use?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1783475</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1783475</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 24 Sep 2009 09:31:42 GMT</pubDate></item><item><title><![CDATA[Reply to Get return value from function returning void on Thu, 24 Sep 2009 09:50:45 GMT]]></title><description><![CDATA[<p>I have checked <code>sizeof(void)</code> during the debug session in the Watch window:<br />
<a href="http://s49.radikal.ru/i124/0909/39/f6fb2218a7ae.png" rel="nofollow">http://s49.radikal.ru/i124/0909/39/f6fb2218a7ae.png</a><br />
I think Microsoft people forgot to fix this inconsistency <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1783484</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1783484</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 24 Sep 2009 09:50:45 GMT</pubDate></item></channel></rss>