From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/language%2Ffunctions.html | 53 +++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 devdocs/c/language%2Ffunctions.html (limited to 'devdocs/c/language%2Ffunctions.html') 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 @@ +

Functions

A function is a C language construct that associates a compound statement (the function body) with an identifier (the function name). Every C program begins execution from the main function, which either terminates, or invokes other, user-defined or library functions.

+
// 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;
+}

A function is introduced by a function declaration or a function definition.

+

Functions may accept zero or more parameters, which are initialized from the arguments of a function call operator, and may return a value to its caller by means of the return statement.

+
int n = sum(1, 2); // parameters x and y are initialized with the arguments 1 and 2

The body of a function is provided in a function definition. Each non-inline(since C99) function that is used in an expression (unless unevaluated) must be defined only once in a program.

+

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:

+
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;
+}

References

See also

+
C++ documentation for Declaring functions
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/language/functions +

+
-- cgit v1.2.3