Function
1. Function composition
int main (int argc, const char * argv[]) {
Insert code here ...
return 0;
}
return value type function name (function parameter)
{
function body Statement;
return value;
}
2. Function classification
1) Standard function
Functions provided by an official or third-party library can be called directly without implementation;
2) Custom Functions
A function implemented by the programmer himself;
3. Steps to write a function
1) Declaring functions
Write in header file
2) Implementation function
Written in the original file
3) Call function
C-phase, typically called in the main function
"Precautions"
The "1" C language file consists of a header file (. h) and a original file (. c)
The entry function for all functions in the "2" C language is the main function, and the main function can only have one
No parameter no return value function:
void function name ();
Write a function that implements printing Hello world;
Write a function for printing a poem of Li Bai;
No parameter has a return value function
Returns the value type function name ();
Write a function that implements 1+2+. +100 and returns the result to the main function;
There is a return value function with the parameter
return value type function name (function parameter);
Write a function that requires passing two numbers from the main function to the child function through the parameter pass, the greatest common divisor of two numbers, and returns the result with a return value.
4. Scope and life cycle of variables
Scope: The use of variables in programs
Life cycle: The effective range of variables in time
5. Local variables and global variables
1) Local variables (variables defined inside the function):
Scope: Start from definition to end of function
Life cycle: From the definition to the end of the function call
2) Global variables (variables defined outside of all functions)
Scope: Start from definition to end of file
Declaration period: From the beginning of the definition to the end of the program
3) Static variables
Scope: As with local variables
Life cycle: As with global variables
Memory Fragmentation:
Data segments: Global variables and static variables stored in data segments
Read-only data segments: strings stored in read-only data segments
Heap Area: Program Ape manually applied memory
Stack area: Local variables
5. Recursion
Using recursive implementation to seek 1+2+3+ ... +100 's and.
A function called itself is called a recursive function.
The behavior of invoking itself is called recursion.
Steps for recursion
1) Find the law of recursion
2) Find the conditions that will end the recursion
3) Perform recursion
n = 5;
int sum (int n);
SUM (5) = SUM (4) + 5;
SUM (4) = SUM (3) + 4;
SUM (3) = SUM (2) + 3;
SUM (2) = SUM (1) + 2;
SUM (1) = 1;
Simple Recursive Demo
#include <stdio.h>
int sum (int n);
void Hello (int n);
int main (int argc, const char * argv[]) {
Insert code here ...
printf ("%d\n", SUM (5));
Hello (10);
return 0;
}
int sum (int n)
{
int ret = 0;
for (int i = 1; I <= n; i++) {
RET + = i;
// }
//
return ret;
if (n = = 1)
{
return 1;
}
return sum (n-1) + N;
}
void Hello (int n)
{
if (n = = 0) {
Return
}
printf ("Hello world\n");
Hello (n-1);
}
C Language _ Functions