8. Function of C language

Source: Internet
Author: User

First, function analysis

1. Functions

A function is a program segment that can be reused, and it can be used by calling statements from other program segments to accomplish a given work

Description

(1) The function is called "function definition", and the function is called "function call".

(2) Calls to other functions are called "keynote functions" and the called functions are called "tuned functions".

A practical C language source program is always composed of many functions, which are based on the actual task, written by the user themselves, in these functions can call the C-provided library functions, you can also call by the user or others written by the function.

However, a C language source program regardless of how many functions, under normal circumstances always start from the main function, the main function ends

C Language source program can be placed in different files, so a function in a source program can also be divided into different files.

2. Library functions

The C language provides a wealth of library functions, including commonly used mathematical functions, character and string processing functions, input and output functions, and so on.

Include command line required when calling C standard library functions

#include <stdio.h>

3. Definition of function

Define the format:

function type function name (parameter type description table)

Description section

Statement section

(1) The function is composed of "function head" and "function body", and the function body consists of a statement and other sub-procedures.

(2) The function type specifies the data type of the function return value, if there is no return value, then the data type is void, if the function has a return value, there should be a return statement "return expression in the function body,", no return value, the return statement should be "return;" can also be omitted.

(3) The formal parameter table when a comma-separated number of formal parameters, each parameter can be a variable name, array name, pointer variable name and pointer array name, the parameter list of the data type of each argument must be given.

(4) Formal parameters can be empty, but parentheses cannot be omitted

(5) If the parameter is an array, just give the array name, [] The length of the arrays is not allowed

(6) The function body is not allowed to nest defined functions, can be nested calls

function return value

The value of the function is returned by the return statement, in the form of the return statement as follows:

return expression, or return (expression);

Description

(1) The value of the expression in the return statement is the value of the function that is being evaluated, and the type of the expression must be the same as the type described in the first part of the function. If the type is inconsistent, the type of the function value is the main, and the system automatically transforms. (You can write a function to test the following)

(2) The return statement may only be executed once, regardless of the number of return statements in the function body;

(3) The return statement may not contain an expression, and the function must be defined as void type.

(4) The function body can have no return statement, but must also define the function as void type.

  

Function call

Form: function name (actual parameter table)

(1) Function call format with no return value:

function name (actual parameter table);

(2) Function call format with return value:

x= function name (actual parameter table);

Second, the deep analysis of the function

1. Basic ideas:

Divide a large program into small modules by function

Characteristics:

    Each module is relatively independent, single function, clear structure, simple interface

Controls the complexity of the program

Improve the reliability of your originals

Shorten the development cycle

Avoid repetitive labor of program development

Easy maintenance and functional expansion

Development methods

    From top to bottom, gradually decompose, divide and conquer

function call Procedure Analysis

(1) allocating memory units for all parameters of a function

(2) Calculate the value of each argument expression, one corresponding to the corresponding parameter (if the invisible parameter, the above process does not execute)

(3) Enter the function body, execute the statement in the function, implement function

(4) When executing to the return statement, evaluates the value of the expression in the return statement (if no return value function is not done), return to the key function

(5) Release the memory of the parameter and the local variable within the function, and continue executing the subsequent statement in the main melody function

2. Transfer value between functions

In C, the data between the keynote function and the tuned function can be passed in three different ways

(1) Value transfer

Value passing is a one-way delivery process, with two one-way delivery constituting two-way delivery

The contents of the keynote function are passed to the tuned function:

Actual parameter assignment to formal parameters

(in C, data can only be passed from the actual parameter one-way to formal parameters, commonly known as "by value", the user can no longer change the value of the corresponding argument in the function)

The contents of the modulated function are passed to the keynote function

Assigning content to the keynote function through a return statement

1#include <stdio.h>2 intFunintXinty)3 {4     if(x==y)5         returnx;6     Else7         return((x+y)/2);8 }9 voidMain ()Ten { One     intA =4, b=5, c=6; Aprintf"%d\n", Fun (2*A,fun (b,c)); /Value Passing -     return 0; -}

(2) Address delivery

Method: When a function is called, the storage address of the data is passed as an argument to the formal parameter, but the parameter (pointer variable) of the address is only stored at this time. To achieve the purpose of "two-way" transmission.

The array name can be used as a function parameter. (both the argument and the parameter apply the array name)

(3) global variable delivery (this is not a good way, usually not recommended)

3. Nested calls to functions and recursive calls

(1) nested calls to functions

Function A calls the function B, and the function B calls the function C. C: Function definitions are not nested, but can be nested to call functions.

(2) recursive invocation of functions

A function called a recursive invocation of a function, either directly or indirectly.

Characteristics of recursion:

Recursion must have a recursive end condition;

The recursive process is first-level recursion (that is, the current call does not end and the next layer is called), while the recursive end condition is satisfied with the end recursion, and then a primary level of regression (that is, a first-level return to the previous level of the call continues to complete the previous level of unfinished operations).

Description

The C compiler system has no restriction on the number of recursive functions, and each time the function is called, it allocates space in the memory stack area, which is used for information such as function variables, return values, etc., so too many recursion times can cause stack overflow.

1#include <stdio.h>2 intFacintN)3 {4     intT;5     if(n = =1|| n = =0)//Recursive End Condition6         return 1;7     Else8     {9t = N*FAC (n1);//descending in a regular mannerTen         returnT//return to the previous layer call One     } A } - voidMain () - { the     intm, y; -printf"Enter m:"); -scanf"%d",&m); -     if(M <0) +printf"Input Data error!\n"); -     Else +     { Ay =FAC (m); atprintf"\n%d! =%d\n", m,y); -     } -     return 0; -}

Additional knowledge:

The abstraction of the storage space of the data in the program when the variable

(1) Local variables

Definition: Defined within a function, valid only within a function

Use: Different functions with the same name variable account for different storage units

Description: Parameter belongs to local variable

(2) Global variables

Definition: A variable defined outside a function

Scope of external variables: a program consisting of only one source program file for an Iraq, the scope of an external variable starts at the location where it is defined, until the end of its source program file

Features: The use of external variables increases the way that data is passed between functions, and any function within the scope of an external variable can refer to that external variable, and the modification of a function to an external variable can affect other functions that reference the variable, so improper use of external variables can result in unexpected errors.

8. Function of C language

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.