[Good programmer notes sharing] -- function analysis, programmer notes Function

Source: Internet
Author: User

[Good programmer notes sharing] -- function analysis, programmer notes Function

IOS training-My C Language notes. I look forward to communicating with you!

 

As for functions, the knowledge you must possess as a developer does not matter in that field, so today I will talk about functions.

I. Introduction to Functions

As for functions, I have already demonstrated a lot in the previous sections. The most commonly used is the main function. Although the function may not be familiar with it, when it comes to the main function, as long as you take a closer look at the structure of the main function, you will probably know what is going on in the heap function.

Function: encapsulate some tedious and repeatedly used code. You only need to call this function when you want to use it later.

In fact, all the work of C Programs is done by functions, so C language is also called a functional language.

For example, we want to output such information.

***

**

*

According to my previous understanding, we use this in main () to implement

 

However, after learning the function, this becomes much simpler. we can define a function to encapsulate the code for future calls!

If the code after using the function is as follows:

Here, I believe you should be familiar with the heap functions.

Ii. definition and use of functions

Int add (int x, int y)
{
Return x + y;
}

If we need to use this function, we only need to input add () and put two corresponding parameters in.

In fact, the idea is basically the same. If we encounter more complex functions in the future, we should follow this idea to declare and use a function, but this involves too many details, more in-depth research and exercises are required.

Iii. Notes for using functions

You cannot define a new function in the definition part of a function. For example:
Void ()
{
Void B ()
{}
}

The value of a parameter is correct before it enters the function, but after it enters the function, its value changes, and no operation is performed on it at this time! The problem only occurs when passing parameters!

 

Iv. Function Parameters

1: pass value

Main ()
{
Int a = 4, B = 6;
Exchg1 (a, B)
Printf ("a = % d, B = % d. \ n", a, B );
}
The operation code completed when Exchg1 (a, B) is as follows.
Int x = a;/* optional */
Int y = B;/* ignore note that the first two rows are implicit operations when the function is called */
Int tmp;
Tmp = x;
X = y;
Y = tmp;

Originally, in fact, the function implicitly assigned values of real parameters a and B to x and y respectively, then, no operations are performed on a and B in the body of the Exchg1 function you wrote.

Only the x and y variables are exchanged. It Is Not a or B. Of course, the values of a and B have not changed! The function only transfers the values of a and B to x and y by assigning values. All the operations in the function are the values of x and y, not the values of a and B. This is the so-called parameter value transfer.

2: Address Transfer

Void Exchg2 (int * px, int * py)
{
Int tmp = * px;
* Px = * py;
* Py = tmp;
Printf ("* px = % d, * py = % d. \ n", * px, * py );
}
Main ()
{
Int a = 4;
Int B = 6;
Exchg2 (& a, & B );
Printf ("a = % d, B = % d. \ n", a, B );
Return (0 );
}
The output result is:
* Px = 6, * py = 4.
A = 6, B = 4.
Look at the Interface part of the function: Exchg2 (int * px, int * py). Note that the px and py parameters are pointers. Let's look at the call: Exchg2 (& a, & B );
It substitutes the address of a (& a) into px, and the address of B (& B) into py. Like the above value transfer, two implicit operations are performed during the function call: The & a, & B values are assigned to px and py.
Px = &;
Py = & B;
Haha! We found that it is actually no different from passing a value, but here we pass the address values of a and B to px and py, instead of the contents of a and B, however, the call to the entire Exchg2 function is as follows:
Px = &;
Py = & B;
Int tmp = * px;
* Px = * py;
* Py = tmp;
Printf ("* px = % d, * py = % d. \ n", * px, * py );

3: Reference Transfer

Void Exchg3 (int & x, int & y)
{
Int tmp = x; x = y;
Y = tmp;
Printf ("x = % d, y = % d. \ n", x, y );
}
Main ()
{
Int a = 4;
Int B = 6;
Exchg3 (a, B);/* Note: The call method is the same as the value transfer method */
Printf ("a = % d, B = % d. \ n", a, B );
}
Output result:
X = 6, y = 4.
A = 6, B = 4.
No. The code format is different from the value transfer, that is, in the definition:
Exchg3 (int & x, int & y)
However, we found that the values of a and B were reversed. This shows that the variables a and B are modified in Exchg3 (a, B), not just x and y.

Let's first look at Exchg3 (int & x, int & y) in the definition of the Exchg3 function ). The x and y parameters are int variables. We can call a function (for example, Exchg1 (a, B);) Like a value (for example, Exchg3 (, B );). However, there is an ampersand "&" in front of x and y. With this, when calling Exchg3, the function will replace a and B with x and y respectively. We say: x and y reference the variables a and B respectively. In this way, the operation in the function is actually the real parameters a and B, that is, the function can be directly modified to the values of a and B.

Compare value transfer with reference transfer:
1) function definition formats are different:
The value is defined as Exchg1 (int x, int y );
The reference is passed here: Exchg3 (int & x, int & y );

2) the same format is used for calling:
Value Transfer: Exchg1 (a, B );
Reference transfer: Exchg3 (a, B );

3) functions are different:
In the value passing function, the operation is not the and B variables, but the values a and B are assigned to x and y. The function operates only the x and y variables instead of the and B variables, and the values of a and B are not modified by the Exchg1 function.
In the reference transfer Exchg3 (a, B) function, x and y are replaced by a and B respectively. The function operates on the and B variables, so the values of a and B can be modified in the function.

V. Other functions
  • Copy strcpy
  • Length strlen
  • Convert to integer atoi
  • Find strstr
  • Scanf function: IO input stream
  • Printf function: IO output stream

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.