The function of C programming

Source: Internet
Author: User
Tags define function

Structured programming advocates the analysis of requirements by function, the main principles from top to bottom, gradually refinement, modularity and so on.

advocated by the function of the software system gradually subdivided, each function is responsible for the data processing, each function to receive some data, after processing the output of some data, this processing method is also known as the data flow processing method.

Its smallest unit is a function, each function is responsible for a function, the entire software system consists of a function, wherein the function as a program entry is called the main function.

The main function calls the other normal functions in turn, and calls the normal functions in turn to complete the function of the whole software system.

A C program can consist of one main function and several other functions, and the main function calls other functions, and other functions can call each other.

General form of function definition: (no parameter function/parameter function/null function)

Non-parametric function form:

Type identifier function name ()

{

Statement part;

Statement part;

}

The form of an argument function:

Type identifier function name ( formal parameter list )

{

Statement part;

Statement part;

}

Empty function form: In the initial stage of program development, you can write an empty function in the future to prepare the expansion function, take the actual function name, but these functions are not well-written, first occupies a position, and later with a well-compiled function instead of it.

Type specifier function name ()

{

}

Values for function parameters and functions

form participates in the actual parameter

When defining a function, the arguments in the function name brackets are formal parameters;

When the function is called, the argument is called in parentheses of the function;

Description of the formal participation argument:

1) parameters specified in the definition function do not account for memory cells when function calls are not present. The inner deposit element is allocated only when a function call occurs. When the call ends, the memory used by the parameter is also freed.

2) An argument can be a constant, a variable, or an expression.

3) in the defined function, the type of the formal parameter must be specified.

4) The type of the actual participation parameter should be the same or compatible.

5) The argument passes data to the parameter, "value passing", one-way

6) The return value is obtained by the return statement. If the type of the function value is inconsistent with the expression in the return statement, the function type will prevail. That is, the type of the function type feels the return value.

7) for a function without a return value, define the function with void as an untyped (empty type);

Function call: According to the function in the program where the points appear, can be divided into three kinds of call mode.

Nested calls to functions

C language cannot be nested to define functions, functions are equal, but can be nested to define functions.

//using intercept method to find the equation#include <stdio.h>#include<math.h>//define function ffloatFfloatx) {    floaty; Y= ((x5) *x+ -) *x- the; returny;}//define function of cross point with X:xpointfloatXpointfloatX1,floatx2) {    return((X1*f (x2)-x2*f (x1))/(f (x2)-f (x1)));}//Define root functionfloatRootfloatX1,floatx2) {    floatx,y,y1; Y1=f (x1);  Do{x=Xpoint (X1,X2); Y=f (x); if(y*y1>0) {y1=y; X1=x; }        ElseX2=x; } while(Fabs (y) >=0.00001); returnx;}intMain () {floatx1,x2,f1,f2,x;  Do{printf ("input x1,x2:\n"); scanf ("%f%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 invocation of functions

In the process of defining a function there appears a recursive invocation of the function itself, called directly or indirectly.

int f (int  x) {  int  y,z;  Z=f (y);    return z;    }
//seeking stratum#include <stdio.h>floatFracintN) {    floatF; if(n<0) {printf ("Error"); }    Else        if(n==0|| n==1) F=1; ElseF=frac (n1)*N; returnF; }   intMain () {floaty; intN; printf ("input an integer:"); scanf ("%d",&N); Y=frac (n); printf ("%d!=%10f\n", N,y); }

Array as function parameter

Array names can do formal parameters and arguments, passing the first address of the array;

#include <stdio.h>floatAveragefloata[Ten]){    inti; floataver,sum=0;  for(i=0;i<Ten; i++) {sum=a[i]+sum; } aver=sum/Ten; returnaver;}intMain () {floatscore[Ten],aver; inti; printf ("Input Ten scores:\n");  for(i=0;i<Ten; i++) scanf ("%f",&Score[i]); printf ("\ n"); Aver=average (score); printf ("%f\n", aver);}

Description: 1) using the array name as the function parameter, you should define the array separately in the key function and the modulated function, for example Score,a, array.

2) The real parameters are consistent with the formal parameter types.

3) in the called function declares the shape parameter group size is 10, but in fact, specifies its size is not any effect, because the C language compiles the shape parameter group size does not check, but the real parameter group of the first element of the address to the parameter group. Therefore, the shape parameter group name obtains the first element address of the real parameter group. They account for the unified address, the same storage unit. Score[n] and A[n] have the same value.

4) Parameter groups can specify no size, followed by an empty parenthesis after the array name when the array is defined. You can also set another parameter.

5) Instead of passing the value of an array element to a formal parameter when using an array-famous function argument, the address of the element of the real parameter group is passed to the parameter group. This allows two arrays to occupy a single memory cell.

Local variables and global variables: from the scope (spatial) angle of the variables.

Local variables: variables defined inside a function are internal variables that are valid only within the scope of the function. This function can use them.

1) The variables defined in the main function (m,n) are also valid only in the main function, and the main function cannot use variables defined in other functions.

2) variables of the same name can be used in different functions, which represent different objects and do not interfere with each other.

3) Formal parameters are also local variables.

4) within a function, variables can be defined in compound statements that are valid only in this compound statement.

Global variables

A variable that corresponds to a local variable, defined outside the function, is called an external variable, a global variable. Other functions can be called.

Global variables increase the channel of data connections between functions. The first letter of a global variable name is generally capitalized.

1) Global variables take a memory unit in all of the program's execution, rather than just the unit when needed.

2) It reduces the versatility of the function, because the function depends on the external variable in which it is executed.

3) Using too many global variables can reduce the clarity of the program, and it is often difficult to clearly determine the value of each external variable in each moment. Therefore, you want to limit the external variables.

4) If the external variable has the same name as the local variable in the same source file, the external variable is "masked" in the scope of the local variable, i.e. it does not work.

Storage categories for variables

Dynamic storage vs. static storage (time from variable value (Lifetime))

Static storage: The way a fixed storage space is provisioned by the system during a program run.

Dynamic storage: A way to dynamically allocate storage space as needed during a program's run.

The global variables are all placed in the static store, allocating space at the beginning of the program, occupying a fixed memory unit. Program execution ends freeing memory.

The dynamic storage area holds the following data:

1) Function parameters

2) automatic variable (auto)

3) field protection and return values for function calls, etc.

The above data allocates the dynamic memory space at the beginning of the function call, and the function ends the release. If a program calls the same function two times, it assigns different storage spaces to the function parameters.

Variables have two properties: storage type (how data is stored in memory : Dynamic and Static) and data type

There are four specific types:

Auto variable

If you do not specifically declare static to store a class, you are allocating storage space dynamically.

Declaring a local variable with static

Sometimes you want the value of a local variable in a function not to disappear after a function call and leave the original value, that is, the memory unit is not freed, and the next time the function is called, the variable already has a value, which is the value at the end of the last function call.

1) Static sentence variables belong to the static storage class and allocate space in static storage area. The entire run process does not release memory.

2) Only the initial value is assigned once.

3) If the local variable is not assigned to the value, then for the static local variable, the compiler automatically assigns the initial value of 0 or null character;

For an automatic variable, an indeterminate value is assigned if the initial values are not assigned.

4) Although a static local variable still exists after the function call ends, other functions cannot reference it.

Register variable of Register

C language word order the value of the local variable in the register in the CPU, need to take a direct from the register to participate in the operation, no longer into the memory to access, so as to improve efficiency. Declare with the keyword register.

1) only local automatic variables and formal parameters can be used as register variables, other (global) not.

2) Because of the limited number of registers in the computer, can not define any multiple register variables;

3) Local static variables cannot be defined as register variables;

Declaring an external variable with extern

An external variable is defined at the outside of the function as a global variable, and its scope starts at the definition of the variable and ends at the end of the program file. Compile-time allocation in static storage. It is sometimes necessary to declare an external variable with extern to extend the scope of the external variable.

Declaring an external variable with static

In the program design, it is hoped that some external variables are only referenced by this file and cannot be referenced by other files.

In the process of programming, often by several people to complete the different modules, each individual can be independently in their design of the file with the same external variable name and irrelevant to each other. You only need to add a static declaration at the beginning of each file.

intrinsic and external functions

Functions are global in nature because a function is called by another function, but it is also possible to specify that the function cannot be called by another file. According to the function can be called by other source files, the function is divided into internal functions and external functions;

intrinsic function (static function)

If a function can only be called by other functions in this file, it is called an intrinsic function. The scope of a function is limited to the file in which it is located, with internal functions of the same name in different files. Add static before the function name and function type;

static int fun (int a, int b);

External functions

1) When defining a function, if you add the keyword extern at the leftmost end of the function, the outer function is represented by the other function when it is called. The default is an external function.

2) in the file that calls this function, use extern to declare the function as an external function that is defined in another file.

The function of C programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.