Problems needing attention in writing C language program

Source: Internet
Author: User
Tags execution

There are several important points to keep in mind when writing functions for C, because they affect the efficiency and usability of functions.

4.5.1 parameters and general functions

Common functions are functions that can be used in various situations, or that can be used by many different programmers. We should not build common functions on global variables (global variables should not be used in common functions). All data required by a function should be passed with parameters (static variables can be used in cases where it is difficult to do so individually). Using parameter passing,

In addition to helping the function to be used in a variety of situations, it can also improve the readability of the function code. Without global variables, you can make a function less likely to cause errors due to side effects.

4.5.2 efficiency

function is the basic component of C language. Functions are essential for all programs that are written in addition to simple programs.

However, in some specific applications, the function should be eliminated, and the embedded code should be used. Inline code refers to the statement of a function that does not contain a function call statement. Use inline code instead of a function only if the execution speed is critical.

There are two reasons why the inline code executes faster than the function. First, the call takes time, and secondly, if there are parameters that need to be passed, it takes time to put them on the stack. In almost all applications, the small overhead of execution time is negligible. However, when time overhead is critical, use inline code to eliminate function calls,

You can save the overhead of each function call. The following two programs print the squares of the numbers from 1 to 1 0. Because the function call takes time, the inline code version runs faster than the other.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn 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.