C Language Programming Learning: 3 things to know about using functions

Source: Internet
Author: User
Tags object model

C language is process-oriented, and C + + is Object-oriented

The difference between C and C + +:

C is a structured language that focuses on algorithms and data structures. The design of the C program is primarily concerned with the output (or implementation process (transaction) control) of the input (or environmental conditions) that is processed through a process.

C + +, the first consideration is how to construct an object model, so that the model can fit with the corresponding problem domain, so that you can get the state information of the object to get output or implementation process (transaction) control. So the biggest difference between C and C + + is that their way of thinking about solving problems is different. C + + is more advanced than C because "the concept of design has been incorporated into C + +".

The biggest difference between C and C + + is that their way of thinking is different for solving problems. The reason that C + + is more advanced than C is because "the concept of design has been incorporated into C + +", and in terms of language itself, in C more is the concept of algorithms. Then C is not important, wrong! Algorithm is the basis of programming, good design if there is no good algorithm, just as not. Moreover, "C plus good design" can also write very good things.

This time to share with you is my little text on the use of functions in C, I hope to help students with doubts better use this powerful language features.

In high school, we all asked for the expression of mathematical function, in fact, the function of programming language is the same as the mathematical expression.

We can all interpret it as a "functional black box" with input and output, to see:

Just like giving the function an X-value, you can find the same result, give the function some input parameters, also can get the same output result.

Small series recommended a learning C language/c++ study Skirt "730, 130, 221", whether you are Daniel or small white, is to change careers or want to join the study together to learn about progress together! The skirt has the development tool, many dry goods and the technical information to share!

1. Modular Programming

Why use a function? In fact, in order to better maintain the functionality of the software and the implementation of modular programming.

You can think about how scary it would be if you wrote all the functions in the main function, and no one would ever read your code.

Through the use of functions can make our software more logical, separate functions written in a separate function, but also convenient for later reuse and so on, many of the benefits are not exhaustive.

One of the more important role to say: Modular programming and code reuse, it is recommended that you find the actual project to see the code composition, naturally understand.

2. Copy mechanism of functions

Many beginners do not understand the function's copy mechanism, such as the following Exchange function:

This function is wrong because when the function is passed, the value of the passed parameter m,n is copied to the function's form parameter, a, B.

Therefore, the value of the formal parameter, a, B, is exchanged inside the function, not the value of the M,n variable when the interchange is passed.

The copy mechanism of a function is from the memory point of view: When the function is passed parameter, the memory address of the argument and the formal parameter is different.

This is the output of this example, and each person's machine may be different:

You can see that the variables M and a, and the memory addresses of N and B are different, so the exchange of values does not work.

But the use of pointers is completely different, see the following example:

Small series recommended a learning C language/c++ study Skirt "730, 130, 221", whether you are Daniel or small white, is to change careers or want to join the study together to learn about progress together! The skirt has the development tool, many dry goods and the technical information to share!

This function is the correct Exchange function, why?

Because we pass the M,n memory address here, that is, the pointer is passed, so the pointer dereference within the function can directly access the contents of the address, which is the characteristics of the pointer provides.

Does it mean that there is no copy mechanism for passing pointers? This is also wrong, the pointer is also a variable, essentially no difference from int, but the characteristics are different.

The above example, when passing pointers, is also a copy of the address of the M,n to PA, and Pb, is also the existence of a replica mechanism.

The only copy here is the address instead of the value, which can be exchanged by directly accessing the memory address of the m,n via the pointer's dereference operation.

Because pointers are very important, it is recommended that you run the program yourself, you can see the output of the PA points to the memory address of M, and &PA is the pointer variable PA memory address, these two don't confuse.

Always remember the following conclusions:

1. An int variable has its own memory address, and also has its own stored integer value

2. A pointer variable has its own memory address, and also has its own stored pointing to the address value

3. Variables have their own addresses and their stored contents in memory

3. Parameter pass Order of function

The VC and GCC compilers calculate the order of function parameters from right to left, and this feature is a bit to remember, the interview may ask.

You can use the following procedure to verify that the program is relatively simple, left for you to think (not thinking is tantamount to wasting time to see the CD this article):

Small series recommended a learning C language/c++ study Skirt "730, 130, 221", whether you are Daniel or small white, is to change careers or want to join the study together to learn about progress together! The skirt has the development tool, many dry goods and the technical information to share!

All right, here we go today.

Written in the last

Programming the most important practice, repeat writing.

These are the things that C + + can do

Server Development Engineer, AI, cloud computing engineer, information security (hacker anti-hacker), big data, data platform, embedded engineer, streaming media server, Data control solution, image processing, audio and video development engineer, game server, distributed system, game assistant, etc.

C Language Programming Learning: 3 things to know about using functions

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.