High quality programming guide-C/C ++ chapter 6th C/C ++ Function Design Basics

Source: Internet
Author: User

Chapter 3 C/C ++ Function Design Basics

6.1 recognize Functions
1. functions are not necessary for building a program, but they are very important. They greatly enhance code vigilance and make the program easier to develop and maintain.
2. actively using existing libraries in programs not only saves time, but also improves program quality.
3. Static and Dynamic Link Libraries:
After writing the code for so long, the concepts of "static Link Library" and "Dynamic Link Library" have been vague. Baidu just gave an excerpt as follows:
"Static Link LibraryYes *. files in lib format are generally added to the project setting interface. When the program is "compiled", the code of the Lib file is added to your program, which increases the code size, when your program starts running, the Lib code is forcibly loaded into the runtime space of your program. You cannot manually remove the Lib code.

Dynamic Link LibraryIt is a module dynamically loaded into the memory when the program is running. The format is *. dll. It can be loaded and removed at will when the program is running, saving memory space. In large software projects, many functions are generally implemented. If you write all the independent functions one by one *. if the Lib file is used, the program runs slowly because it occupies a large amount of memory space. However, if the function is written *. DLL file, you can call the DLL file corresponding to the function when using this function, and remove the DLL file from the memory when not using this function, which can save memory space ".

After reading this passage, I seem to understand a little bit more.

If a function you write is not called anywhere in the program, the compiler will not generate executable code for the function.---- I have never understood the truth. I saw it today. Haha! In this way, when developing some common classes, we should design and implement their complete functions, without worrying that its users only use a small part of the functions, but the entire class definition will cause the code size to increase. This is the principle of C ++ template instantiation and the design and implementation of class libraries.

6.2 function prototype and definition

1. The function prototype format is as follows:
[Scope] [function link specification] Return Value Type [function call specification] function name (parameter list ...);
This format is a bit dizzy! In the past, I was impressed with the function prototype by the function name of the return value type (list of parameters ...);
Let's take a look at this difference later.

2. Understand the differences between a parameter and an actual parameter: the essence of parameter passing in a function call is"Initialize the form parameter with the real Parameter"Instead of replacing the form parameter, it is important to remember this!

6.3 function call Method
6.4 function Stack
6.5 calling convention)
Function call specifications? Another new thing! Never used.
Common calling rules in windows are as follows:
1) _ cdecl
2) _ stdcall
3) _ thiscall
4) _ fastcall

6.6 function Connection Specification (linkage Specification)
When using different programming languages for software joint development, you need to unify the connection specifications of functions, variables, data types, constants, and so on, especially the interfaces shared between different modules.

6.7 function parameter passing rules
Two elements of a function interface are parameters and return values.
In C, function parameters and return values are transmitted in two ways: pass by value and pass by pointer.
In C ++, in addition to pass by value and pass by pointer, a pass by reference is added.
There are two recommended codes:
1) if the parameter is a pointer and only used for input, you should add const before the type to prevent the memory unit (value or content pointed by the pointer) pointed to by the pointer) it is accidentally modified in the function body.
2) avoid having too many parameters in the function (less than five are recommended ). If there are too many parameters, consider refactoring: encapsulated as an object.

6.8 rules for returning function values
The return value of a function can be either a return statement or an output parameter.
1. Do not mix the normal value with the error mark to return the result. For example, the normal value can be returned using the output parameter, and the error mark is returned using the return statement. Otherwise, you can proceed with the specific situation.
2. Do not use the return value of the function as the left value !!

6.9 internal implementation rules of functions
A large part of the effort in programming is to implement the function (that is, to define the function implementation ). Therefore, the careful and rigorous definition of function implementation is particularly important to improve the quality of software.
We can strictly control the function at the "ENTRANCE" and "exit.
1) Check the parameter validity at the entrance of the function body. Use assert in large quantities ).
2) Check the correctness and efficiency of the Return Statement at the exit of the function.
3) Of course, we should not only check the validity of the input parameters, but also check the validity of the variables that enter the function through other channels (member variables or global variables.
4)An effective use of static local variables: global variables can be encapsulated. For example:
Inline const char * getwindowspath (void)
{
Static const char * P = "C: \ WINDOWS \\";
Return P;
}

6.10 storage type and scope rules
After reading this section, my mind is a bit messy. Do not sort it out first. You can check it again when using it or encountering problems.

6.11 recursive functions
Recursive functions are inherently inefficient ". But recursion can make some problems intuitive and concise.

6.12 use assert)
The purpose of assertion is to capture "invalid conditions" that should not occur during runtime ". Here we need to distinguish between "illegal" and "error.
For example:
Assert (I = 1 );
That is, you must make sure that I must be 1. If it is another value, the program will be ended directly.

1) if an error occurs, the program should exit directly instead of simply returning the result.
2) if an error occurs, the program should handle the error:
1) Modify input or correction operations.
2) return directly, prompting the user for incorrect input or operation.

6.12 use const to improve the robustness of functions
"Use const whenever you need ."
When using const to modify the return value of a function, you need to think more.

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.