blob: 33a0ef72c4ae791b968d135734e99949d420e5c2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
<h1 id="firstHeading" class="firstHeading">return statement</h1> <p>Terminates current function and returns specified value to the caller function.</p>
<h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span> <code>return</code> <span class="t-spar">expression</span> <code>;</code> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td>
</tr> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span> <code>return</code> <code>;</code> </td> <td> (2) </td> <td class="t-sdsc-nopad"> </td>
</tr>
</table> <table class="t-par-begin"> <tr class="t-par"> <td> <span class="t-spar">expression</span> </td> <td> - </td> <td> expression used for initializing the return value of the function </td>
</tr> <tr class="t-par"> <td> <span class="t-spar">attr-spec-seq</span> </td> <td> - </td> <td> <span class="t-mark-rev t-since-c23">(C23)</span>optional list of <a href="attributes" title="c/language/attributes">attributes</a>, applied to the <code>return</code> statement </td>
</tr>
</table> <h3 id="Explanation"> Explanation</h3> <div class="t-li1">
<span class="t-li">1)</span> Evaluates the <span class="t-spar">expression</span>, terminates the current function and returns the result of the <span class="t-spar">expression</span> to the caller (the value returned becomes the value of the function call expression). Only valid if the function return type is not <code>void</code>.</div> <div class="t-li1">
<span class="t-li">2)</span> Terminates the current function. Only valid if the function return type is <code>void</code>.</div> <p>If the type of the <span class="t-spar">expression</span> is different from the return type of the function, its value is <a href="conversion" title="c/language/conversion">converted</a> as if by assignment to an object whose type is the return type of the function, except that overlap between object representations is permitted:</p>
<div class="c source-c"><pre data-language="c">struct s { double i; } f(void); // function returning struct s
union { struct { int f1; struct s f2; } u1;
struct { struct s f3; int f4; } u2; } g;
struct s f(void)
{
return g.u1.f2;
}
int main(void)
{
// g.u2.f3 = g.u1.f2; // undefined behavior (overlap in assignment)
g.u2.f3 = f(); // well-defined
}</pre></div> <p>If the return type is a real floating type, the result may be represented in <a href="../types/limits/flt_eval_method" title="c/types/limits/FLT EVAL METHOD">greater range and precision</a> than implied by the new type.</p>
<p>Reaching the end of a function returning <code>void</code> is equivalent to <code>return;</code>. Reaching the end of any other value-returning function is undefined behavior if the result of the function is used in an expression (it is allowed to discard such return value). For <code>main</code>, see <a href="main_function" title="c/language/main function"><code>main</code> function</a>.</p>
<table class="t-rev-begin"> <tr class="t-rev t-since-c11">
<td> <p>Executing the <code>return</code> statement in a <a href="_noreturn" title="c/language/ Noreturn">no-return function</a> is undefined behavior.</p>
</td> <td><span class="t-mark-rev t-since-c11">(since C11)</span></td>
</tr> </table> <h3 id="Keywords"> Keywords</h3> <p><a href="../keyword/return" title="c/keyword/return"><code>return</code></a></p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h>
void fa(int i)
{
if (i == 2)
return;
printf("fa(): %d\n", i);
} // implied return;
int fb(int i)
{
if (i > 4)
return 4;
printf("fb(): %d\n", i);
return 2;
}
int main(void)
{
fa(2);
fa(1);
int i = fb(5); // the return value 4 used to initializes i
i = fb(i); // the return value 2 used as rhs of assignment
printf("main(): %d\n", i);
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">fa(): 1
fb(): 4
main(): 2</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 6.8.6.4 The return statement (p: 111-112) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 6.8.6.4 The return statement (p: 154) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 6.8.6.4 The return statement (p: 139) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 3.6.6.4 The return statement </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/return" title="cpp/language/return">C++ documentation</a></span> for <span class=""><span><code>return</code> statement</span></span> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/language/return" class="_attribution-link">https://en.cppreference.com/w/c/language/return</a>
</p>
</div>
|