The difference between a macro definition and a function

Source: Internet
Author: User
1. Parameters.   Macros define no type restrictions, and function parameters must be defined even if the same functionality is implemented. Eg: #defineMAX (x, y) > (y)? (X):(Y)

2. Efficiency of execution

The function has high efficiency when it passes parameters and return values without a macro definition of a direct substitution statement.

3. Program length

When a macro is defined, a line of code is written, and the function calls a location, and the source code does not grow.

4. Side effects

The return value changes when J + + is passed

#defineMAX (x, y) > (y)? (X):(Y) int a=3;int b=4; MAX (a++,b++); The result is 5.5. Operator precedence affects the result in the macro definition (with the parentheses restriction if necessary). 6. A macro definition cannot define recursion.


This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1680261

The difference between a macro definition and a 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.