C programming functions, c programming functions
Structured Program Design advocates functional analysis of needs, the main principle from top to bottom, gradually refinement, modular and so on.
It is suggested that the software system be subdivided by function. Each function is responsible for processing the data. Each function receives some data and outputs some data after processing, this processing method is also called a data stream-oriented processing method.
The smallest unit of a function is a function. Each function is responsible for a function. The entire software system is composed of functions. The function serving as the program entry is called the main function.
The main function calls other common functions in turn, and calls between common functions in order to complete the functions of the entire software system.
A c program consists of a main function and several other functions. The main function calls other functions, and other functions can also call each other.
The general form of Function Definition: (No Parameter Function/empty function)
No Parameter Function Form:
Type identifier function name ()
{
Declaration part;
Statement section;
}
Parameter Function Format:
Type identifier function name (Format parameter list)
{
Declaration part;
Statement section;
}
Empty Function Format: in the initial stage of program development, you can write an empty function where you are preparing to expand the function in the future. The actual function name is used, but these functions are not compiled and take the first place, we will replace it with a compiled function later.
Type specifier function name ()
{
}
Function parameters and function values
Participate in real parameters
When defining a function, the parameters in the function name brackets are form parameters;
When a function is called, real parameters are included in the brackets of the called function;
Description of form participation in real parameters:
1) parameters specified in the definition function do not occupy the memory units when function calls are not performed. Memory units are allocated only when a function call occurs. After the call, the memory occupied by the parameters is also released.
2) real parameters can be constants, variables, or expressions.
3) in the defined function, the type of the parameter must be specified.
4) The type of the input parameter must be the same or compatible.
5) The real parameter transmits data to the shape parameter, "value transfer", one-way
6) The return value is obtained by the return Statement. If the type of the function value is different from the expression in the return statement, the function type prevails. That is, the type of the returned value of the function type.
7) for functions without return values, use void to define the function as non-typed (empty type );
Function call: It is divided by the position where the function appears in the program. It can be divided into three call methods.
Nested function call
The C language cannot be nested to define functions. functions are equal, but can be nested to define functions.
// Obtain the equation # include <stdio. h> # include <math. h> // define function ffloat f (float x) {float y; y = (X-5) * x + 16) * x-80; return y ;} // define function of cross point with x: xpointfloat xpoint (float x1, float x2) {return (x1 * f (x2)-x2 * f (x1 )) /(f (x2)-f (x1);} // define root functionfloat root (float x1, float x2) {float x, y, y1; y1 = f (x1); do {x = xpoint (x1, x2); y = f (x); if (y * y1> 0) {y1 = y; x1 = x;} else x2 = x;} while (fabs (y)> = 0.00001); return x;} int main () {float x1, x2, f1, f2, x; do {printf ("input x1, x2: \ n"); scanf ("% f", & x1, & x2); f1 = f (x1 ); f2 = f (x2);} while (f1 * f2> = 0); x = root (x1, x2 ); printf ("A root of equation is % f \ n", x );}
Recursive call of functions
When defining a function, the function itself is called directly or indirectly, which is called a recursive call of the function.
int f(int x){ int y,z; z=f(y); return z; }
// Calculate the class # include <stdio. h> float frac (int n) {float f; if (n <0) {printf ("error ");} else if (n = 0 | n = 1) f = 1; else f = frac (n-1) * n; return f;} int main () {float y; int n; printf ("input an integer:"); scanf ("% d", & n); y = frac (n); printf ("% d! = % 10f \ n ", n, y );}
Array as function parameter
The array name can be used as a form parameter and an actual parameter to pass the first address of the array;
#include<stdio.h>float average(float a[10]){ int i; float aver,sum=0; for(i=0;i<10;i++) { sum=a[i]+sum; } aver=sum/10; return aver;}int main(){ float score[10],aver; int i; printf("input 10 scores:\n"); for(i=0;i<10;i++) scanf("%f",&score[i]); printf("\n"); aver=average(score); printf("%f\n",aver);}
Note: 1) use an array name as a function parameter. The primary function and the called function should define arrays, for example, score, a, and array.
2) The real parameters are of the same type as the form parameters.
3) In the called function, it is declared that the array size of the parameter is 10, but in fact, it does not have any effect to specify its size, because the C language compilation does not check the array size of the parameter, it only transmits the address of the first element of the real parameter array to the form parameter array. Therefore, the name of the form parameter array obtains the first element address of the real parameter array. They share the same address and the same storage unit. Score [n] And a [n] have the same value.
4) The parameter array can be of no size specified. When defining an array, an empty bracket is followed by the array name. You can also set another parameter.
5) when using array names as function arguments, instead of passing the values of array elements to the form parameters, the addresses of elements in the real arguments array are passed to the form parameters array. In this way, the two arrays occupy a total of memory units.
Local variables and global variables: the scope (Space) of variables.
Local variable: The variable defined in a function is an internal variable, which is only valid within the function range. This function can only use them.
1) the variables (m, n) defined in the main function are only valid in the main function, and the main function cannot use the variables defined in other functions.
2) variables with the same name can be used in different functions. They represent different objects and do not interfere with each other.
3) The parameter is also a local variable.
4) within a function, you can define variables in composite statements. These variables are only valid in this composite statement.
Global Variables
It corresponds to local variables. The variables defined outside the function are called external variables and global variables. Other functions can be called.
Global variables increase the channels for data connection between functions. Generally, the first letter of the global variable name is capitalized.
1) global variables occupy memory units in all program execution processes, rather than opening up units only when needed.
2) It reduces the versatility of the function because the function depends on its external variables during execution.
3) using too many global variables will reduce program clarity, and it is often difficult for people to clearly determine the values of various external variables at every moment. Therefore, you must restrict external variables.
4) if the external variable in the same source file has the same name as the local variable, the external variable is "blocked" within the scope of the local variable, that is, it does not work.
Storage Class of Variables
Dynamic Storage and static storage (from the time when the variable value exists (lifetime ))
Static storage: During the program runningWith a fixed storage space.
Dynamic Storage: dynamically allocates storage space as needed during the running of the program.
Global variables are all stored in the static storage area. Space is allocated at the beginning of the program, occupying a fixed memory unit. The execution ends and the memory is released.
The dynamic storage area stores the following data:
1) function parameters
2) auto)
3) field protection and return values during function calls
The above data is allocated with dynamic memory space at the beginning of the function call, and the function is released. If a program calls the same function twice, it gives the function parameters different buckets.
Variables have two attributes:Storage type (data is stored in memoryOfMethod: dynamic and static)AndData Type
There are four types:
Auto variable
If static storage classes are not specifically declared, all buckets are dynamically allocated.
Declare local variables with static
Sometimes, if you want the value of a local variable in a function to not disappear after the function is called, the original value is retained, that is, the occupied memory unit is not released. When you call the function next time, the variable has a value, that is, the value at the end of the callback function call.
1) Static sentence variables belong to the static storage class and are allocated in the static storage area. The memory is not released during the entire running process.
2) Assign the initial value only once.
3) If no value is assigned when defining a local variable, the initial value 0 or null is automatically assigned to the static local variable during compilation;
For automatic variables, an uncertain value is assigned if no initial value is assigned.
4)Although static local variables still exist after the function call, other functions areNoReferenceIt.
Register variable
In the C language, the values of local variables are placed in the registers in the CPU. When necessary, the values are taken directly from the registers for calculation without being accessed in the memory. This improves the efficiency. Declare with the keyword register.
1) only local automatic variables and parameters can be used as register variables. Other (global) variables cannot.
2) due to the limited number of registers in the computer, no number of register variables can be defined;
3) Local static variables cannot be defined as register variables;
Use extern to declare external variables
When an external variable is a global variable defined outside the function, its scope starts from the definition of the variable to the end of the program file. It is allocated to the static storage area during compilation. Sometimes you need to use extern to declare external variables to extend the scope of external variables.
Declare external variables with static
In programming, it is expected that some external variables will be referenced only by this file, but not by other files;
In the process of program design, several people often complete different modules respectively. Each person can use the same external variable name in the design file independently, but they are irrelevant. You only need to add the static declaration at the beginning of each file.
Internal and external functions
A function is essentially global, because a function is called by another function, but you can also specify that the function cannot be called by other files. Functions can be divided into internal and external functions based on whether the function can be called by other source files;
Internal functions (static functions)
If a function can only be called by other functions in this file, it is called an internal function. The function scope is limited to the file where it is located. Different files have internal functions with the same name, so they do not interfere with each other. Add static before the function name and function type;
Static int fun (int a, int B );
External Functions
1) when defining a function, if the keyword extern is added to the leftmost end of the function header, the external function is used for calling other functions. The default value is an external function.
2) in the file that needs to call this function, use extern to declare the function, indicating that the function is an external function defined in other files.