C Primer Plus Reading notes the Nineth Chapter

Source: Internet
Author: User
Tags value of pi

The title of this chapter is the function. The design principle of C is to use the function as the constituent module of the program.

1. Overview of functions

function definition: A function is a self-contained unit of program code that is used to complete a particular task.

Reasons for using the function: 1. The use of functions can save you from writing repetitive code. 2. Make the program modular, more conducive to reading, modification and improvement. Looking at functions in this way is good for putting energy into the overall design of the program rather than implementing its details.

Functions have multiple types as with variables. Any program needs to declare the function type before it can use the function.

function arguments, function types nothing to say, other languages will talk about.

PS: The end of the function is best to use only one return, so that the reader is more useful to understand the function of the execution process.

2. Recursion

Recursion can generally be used instead of a circular statement. In some cases, it is better to use a looping statement, and sometimes it is more efficient to use recursion. Although the recursive method makes the program structure graceful, its execution efficiency is not high in the loop statement. In general, the choice of loops is a little better. First of all, because each recursive call has its own set of variables, so need to compare more memory; Second, each function call takes a certain amount of time, so execution is slow. In some cases, however, we cannot replace recursion with a simple looping statement, so it is necessary to learn recursion.

#include"stdio.h"voidUp_and_down (int);intMain (void) {Up_and_down (1); return 0;}voidUp_and_down (intN) {printf ("Level %d:n Location%p\n", N, &N); if(N <4) Up_and_down (n+1); printf ("Level %d:n Location%p\n", N, &n);}

This program is typical of recursion, the result of the operation is as follows, the key is to understand the principle of the stack.

3. Compilation of multi-source code file programs

You can generally put multiple functions in the same file, you can also use a header file, the function prototype is placed in a header file.

4. Address operators

The unary operator & can get the storage address of the variable. %p is the specifier for the output address.

The concept of pointers is introduced below. A pointer is a variable whose value is an address. The indirect operator * is able to get the numeric value of the variable pointed to by the pointer. When you declare a pointer, you need the type of the variable that the pointer points to, such as

int * PI;

The value of pi is an address, in most systems, the value of pi is an address, in most systems it is represented by an unsigned integer, but this does not mean that the pointer can be treated as an integer type because the pointer is a new data type.

How to understand variable names, addresses, and values: A variable typically has two properties: variable name and value. After the program is compiled and loaded, the two properties of the same variable in the computer are address and numeric values. The address of a variable can be treated as a variable name in the computer. In many programming languages, variable addresses are processed only by the computer and are completely invisible to programmers, but in the C language, you can use the operator & to manipulate variable addresses.

Programming exercises are simpler and less difficult than the code examples in the book.

To be Continued ...

  

C Primer Plus Reading notes the Nineth Chapter

Related Article

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.