First, what is a function
L any C Language Program is composed of one or more program segments (small programs), each of which has its own function, which we generally call " functions ". So, you can say that the C Language Program is composed of functions.
L For example, if you write a MP3 player program in C, the program structure is as follows:
Second, the definition of function 1. Defining the purpose of a function
L encapsulate a common function to facilitate later invocation.
2. Steps to define a function
L Function Name: What is the function name
L function Body: What does the function do and what code it contains
3. Format
L fixed format (all functions in many languages are written in this way)
return value type function name ( formal parameter list )
{
function body
}
l Example
Defines a function that calculates the number of two integers and
sum (int a, int b)
{
int C = a + B;
}
Third, function call
L SUM (10, 11); Elicit return value
L Description of Function call procedure
L briefly describe the function of return
Iv. parameters of the function
- Basic concepts of formal parameters and arguments
- The number of parameters is the same as the argument:sum (ten, one, one)
- The pass of a parameter is a value pass
- Parameter name cannot have the same name as a local variable within a function
- Functions can have no parameters: Design a function to return PI
V. Return value of a function
- The basic concept of return values , the function of return
- void
- Return
l void can omit return
L can use return multiple times
L cannot have other statements after return
- Weak syntax for a function
L If there is no write return value type, the default is int
L If you write a return value, you can not return
L Call a function that has not been defined
Vi. steps to define a function
- Clear function, a meaningful function name
- Define the parameters and return values of the function
- Example:
L to ask for two integers and
L Print a horizontal line
L Print N Horizontal line
Seven, function notice
L cannot nest defined functions
L Call myself in a dead loop.
L cannot be defined repeatedly, can be declared repeatedly
Eight, the function of the Supplement 1. Mainfunction
L return Value:0, normal exit;1, abnormal exit
2. printffunction
L #include
L return Value: Length of string
Nine, exercise
- Write a function double AVR (int a, int b) to calculate the average of A and B
- Write a function int POWs (int base, int n) to return the n - th square of the base
- Write a function int pieadd (int n), calculate the value of the 1+2+3+......+n, and return. For example, the return value of Pieadd (3) is 1+2+3=6
X. Declaration of functions
- The order in which functions are defined
- function declaration function: Declaration and definition, similar to the identity card and human Relations, the compilation of buy tickets, link boarding
- Only function declaration, no definition, compile warning, link error
Xi.. hfiles and. Cthe division of Documents
- The disadvantage of single file
L The contents of a file are too many, not conducive to reading, debugging
L Multiple people modify the same file problem
L The company is a team player.
- Extract the sum function into another . C file
L CALL the sum function directly first, compile warning, link main.c error
L #include "sum.c", compile link main.c, run successfully (Paint Analysis . o file)
L If sum.cis used in avr.c , compile the link main.c, run the program (the reason for drawing analysis error)
- Declaring the sum function in another file
l int sum (int,int);
L Compile link main.c
L Compile link sum.c
L Compile link main.c sum.c, run successfully
L AVR.C Use the same method
- extract int sum (int,int) to another file
L Do not extract the disadvantages of the declaration: Add a new function
L extract to . C file? The development tool will compile all the . C links
L extract to . h file
- Summary of extraction Steps
L. C file definition of write function
L. h File write function declaration
If you want to use my function, please include my . h file
L Benefit Analysis
12, #include1. Introduction to preprocessing Directives 2. #includethe role
It's purely a file copy.
3. #include <stdio.h>
L What's in stdio.h? What to do when it comes to linking
L <> and "" The Difference
4. #includeThe path problem
L The default is the same path, other issues are discussed later
The functions in C