summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Ffunctions.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/language%2Ffunctions.html
new repository
Diffstat (limited to 'devdocs/c/language%2Ffunctions.html')
-rw-r--r--devdocs/c/language%2Ffunctions.html53
1 files changed, 53 insertions, 0 deletions
diff --git a/devdocs/c/language%2Ffunctions.html b/devdocs/c/language%2Ffunctions.html
new file mode 100644
index 00000000..23bdd021
--- /dev/null
+++ b/devdocs/c/language%2Ffunctions.html
@@ -0,0 +1,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">
+ &copy; 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>