blob: 23bdd021796e24ea33fd8c0f7c364170299a464c (
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
|
<h1 id="firstHeading" class="firstHeading">Functions</h1> <p>A function is a C language construct that associates a <a href="statements#Compound_statements" title="c/language/statements">compound statement</a> (the function body) with an <a href="identifier" title="c/language/identifier">identifier</a> (the function name). Every C program begins execution from the <a href="main_function" title="c/language/main function">main function</a>, which either terminates, or invokes other, user-defined or library functions.</p>
<div class="c source-c"><pre data-language="c">// function definition.
// defines a function with the name "sum" and with the body "{ return x+y; }"
int sum(int x, int y)
{
return x + y;
}</pre></div> <p>A function is introduced by a <a href="function_declaration" title="c/language/function declaration">function declaration</a> or a <a href="function_definition" title="c/language/function definition">function definition</a>.</p>
<p>Functions may accept zero or more <i>parameters</i>, which are initialized from the <i>arguments</i> of a <a href="operator_other#Function_call" title="c/language/operator other">function call operator</a>, and may return a value to its caller by means of the <a href="return" title="c/language/return">return statement</a>.</p>
<div class="c source-c"><pre data-language="c">int n = sum(1, 2); // parameters x and y are initialized with the arguments 1 and 2</pre></div> <p>The body of a function is provided in a <a href="function_definition" title="c/language/function definition">function definition</a>. Each <span class="t-rev-inl t-since-c99"><span>non-<a href="inline" title="c/language/inline">inline</a></span><span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span> function that is used in an expression (unless <a href="expressions#Unevaluated_expressions" title="c/language/expressions">unevaluated</a>) must be <a href="extern#One_definition_rule" title="c/language/extern">defined only once</a> in a program.</p>
<p>There are no nested functions (except where allowed through non-standard compiler extensions): each function definition must appear at file scope, and functions have no access to the local variables from the caller:</p>
<div class="c source-c"><pre data-language="c">int main(void) // the main function definition
{
int sum(int, int); // function declaration (may appear at any scope)
int x = 1; // local variable in main
sum(1, 2); // function call
// int sum(int a, int b) // error: no nested functions
// {
// return a + b;
// }
}
int sum(int a, int b) // function definition
{
// return x + a + b; // error: main's x is not accessible within sum
return a + b;
}</pre></div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 6.7.6.3 Function declarators (including prototypes) (p: 96-98) </li>
<li> 6.9.1 Function definitions (p: 113-115) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 6.7.6.3 Function declarators (including prototypes) (p: 133-136) </li>
<li> 6.9.1 Function definitions (p: 156-158) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 6.7.5.3 Function declarators (including prototypes) (p: 118-121) </li>
<li> 6.9.1 Function definitions (p: 141-143) </li>
</ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul>
<li> 3.5.4.3 Function declarators (including prototypes) </li>
<li> 3.7.1 Function definitions </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/function" title="cpp/language/function">C++ documentation</a></span> for <span class=""><span>Declaring functions</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/functions" class="_attribution-link">https://en.cppreference.com/w/c/language/functions</a>
</p>
</div>
|