- What is a function
- 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.
- For example, if you write a MP3 player program in C, the program structure is as follows:
- Definition of a function
- Defining the purpose of a function
- Encapsulates a common feature to facilitate later invocation
- Steps to define a function
- Function name: What are the names of functions?
- Function Body: What does the function do and what code it contains
- Format
- Fixed format (This is the case for many language functions)
return value type Name of function ( formal parameter list )
{
function Body
}
Defines a function that calculates the number of two integers and
sum (int a, int b)
{
int C = a + B;
}
- Function call
- SUM (10, 11); Elicit return value
- Describe the calling procedure for a function
- Briefly describe the function of return
- Parameters of the function
- Basic concepts of formal parameters and arguments
- The number of formal parameters and the arguments are the same: sum (10, 11, 12)
- 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
- return value of the function
- The basic concept of return values, the function of return
- void
- Return
- void can omit return
- You can use return multiple times
- Cannot have other statements after return
- Weak syntax for a function
- If there is no write return value type, the default is int
- If you write a return value, you can not return
- Call a function that has not been defined
- steps to define a function
- Clear function, a meaningful function name
- Define the parameters and return values of the function
- Example:
- For two integers and
- Print a horizontal line
- Print N Horizontal lines
- function Note
- Cannot nest defined functions
- The dead loop calls themselves to call themselves
- cannot be defined repeatedly, can be declared repeatedly
- Complement of functions
- Main function
- Return value: 0, normal exit; 1, abnormal exit
- printf function
- #include
- Return value: The length of the string
- Exercises
- 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
- Declaration of a function
- 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
- Division of labour between. h files and. c Files
- The disadvantage of single file
- The content of a file is too much, not conducive to reading, debugging
- Multiple people modifying the same file problem
- The company is team work
- Extract the sum function into another. c file
- Call the SUM function directly first, compile warning, link MAIN.C error
- #include "sum.c", compile link main.c, run successfully (Paint analysis. o file)
- If you use SUM.C in avr.c, compile the link main.c, run the program (the reason why the paint analysis is wrong)
- Declaring the SUM function in another file
- int sum (int,int);
- Compile link main.c
- Compile link sum.c
- Compile link main.c sum.c, run successfully
- Avr.c Use the same method
- extract int sum (int,int) to another file
- The downside of not extracting claims: adding new functions
- Extracted into a. c file? The development tool will compile all the. C Links
- Extracted to the. h file
- Summary of extraction Steps
- Definitions for. c File Write functions
- Declaration of the. h File Write function
- To use my function, please include my. h file
- Benefit analysis
- #include
- Introduction to preprocessing Directives
- #include的作用
It's purely a file copy.
- #include <stdio.h>
- What's in stdio.h? What to do when it comes to linking
- The difference between <> and ""
- #include的路径问题
- The default is the same path, and other issues are discussed later
C-Language section eighth function