C Primer + + Learning Note 8th--c++ function

Source: Internet
Author: User

C + + functions
inline functions
The function of inline functions: The program does not need to jump to another location to execute code, and then jumps back
Inline functions run slightly faster than regular functions, but at the expense of more memory
Measures for declaring inline functions: add keyword inline before function declaration and definition
Inline double Square (double x);//declaring inline function
Inline double Square (double x) {
return x*x;
}//defining an inline function


Macro-like definition
#define SQUARE (X) x*x; But X is not a parameter pass


Variable reference
int rats = 10;
int &rods= rats; & is not an address character, but is part of a type identifier
Rats and rods have the same value and address, and when rods++, the value of rats is automatically added 1
They are a variable of two names
int *p =&rats;//Pointer declaration

void swap (int &a,int &b) {
int temp;
Temp=a;
A=b;
B=temp;
}//reference passing can change the value of a B while transmitting it back.
void swap (int *p,int *q) {
int temp;
Temp=*p;
*p=*q;
*q=temp;
}//pointers are changing their point of address
All two of these functions can be real value exchange passes, but
void swap (int a,int b) {
int temp;
Temp=a;
A=b;
B=temp;
}//cannot be implemented
When to use reference parameters
1. Programmers can modify data objects in the calling function
2. You can increase the speed of your program by passing a reference instead of the entire data object
For functions that use passed values without modification
1. If the data object is small, into a built-in data type or small structure, it is passed by value
2. If the data object is an array, use the pointer and declare the pointer as a pointer to the const
3. If the data object is a larger structure, use a const pointer or a const reference to increase the efficiency of the program.
4. If the data object is a class object, use the const reference
Functions that modify the data in the calling function
1. If the data object is a built-in data type, use the pointer
2. If the data object is an array, you can only use pointers
3. If the data object is a struct, use the reference or pointer

4. If the data object is a class object, use the reference


function overloading

C Primer + + Learning Note 8th--c++ function

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.