Simple learning C -- the seventh day, the seventh day

Source: Internet
Author: User

Simple learning C -- the seventh day, the seventh day

Function

A function is an important part of C language. Any C language program you write now or later (if C is not changed) is composed of one function. Of course, now you may only write a small program in the main function. After reading this article, I feel that it is not a problem to write multiple functions to run in one program, it is also helpful for understanding the modular programming of C language.

I have learned a lot about data types, such as pointers, arrays, struct, and so on. before using them, what operations do we need first? That's right. The first is definition. Before using a function, define a function. The following is how to define:

Type specifier function name (passed parameter type row parameter name ...)

{

// The content here is the same as that in the main () braces of the main function.

}

Think about whether the above definition method is very familiar?

Remember the first program you came into contact? Its core part is

Int main ()

{

// Content

Return 0;

}


In fact, this is a main function. It is required for every C language program to run. Its name is fixed and cannot be changed, that isMain,We will find that there is nothing in the parentheses behind the main function, which means that this main function does not contain parameters, which is also frequently used. Of course, the main function can also contain parameters, suchInt main (int argc, char * argv []), see: http://blog.csdn.net/cnctloveyu/article/details/3905720

In the preceding Definition FormatType specifierSpecifies the type of return Value of the function (generally, the main function has return 0 at the end. This indicates that the function runs normally and can be omitted. If it is omitted, it is automatically added during compilation, so the return value of the main function is int). If there is no return value, you need to add void to indicate that the return value is null. ShapeVoid print (){}Now, a function with a NULL return value type named print and no formal parameters is defined. Next I will use the print function defined by myself.

# Include <stdio. h> void print () // print function definition {printf ("you are someone \ n"); // statement
Return;
} Int main () {print (); // call the print function in the main function}

Running result:

If the print function is not called in the main function, the program is correct, but print is useless. Understanding the execution process of a program is a key step,As the running sequence of a C Language Program, the main function is taken as the starting point, from top to bottom is fundamental, and then the execution sequence is followed by the written statements.

Next, write a slightly complex program: Compare the sizes of two numbers a and B. If a is large, output a, and output B if B is large.

# Include <stdio. h> // 1int Max (int c, int d) // definition of the Max function // 5 {// at this time, the value of c is equal to the value of, d = B // if c is greater than d, the return value of Max is c. Otherwise, the return value is d if (c> d) // 6 return c; // 7 else // 6 return d; // 7} int main () {int a, B, c; // 2 scanf ("% d ", & a, & B); // 3 // pass the value of a to the form parameter c, and pass the value of B to d // return the value of Max, that is,, the maximum value of B is assigned to c = Max (a, B); // 4 // 8 printf ("% d \ n", c ); // Output c // 9 return 0; // 10}

 

(Additional: Where should external functions be written?

Generally, for convenience,Place the external function above the function that calls it, so that you do not need to declare it in the main function. To place it where you want to place it, you must declare it in the main function.For example, to declare the Max function, you must add the statement in the main function:Int Max (int c, int d); the two identifiers c and d can be removed.

# Include <stdio. h> // 1int main () {int a, B, c; int Max (int c, int d); // 2 // Max function declaration, in this way, the Max function can be placed behind the main function. // generally, the declaration and data definition are put at the same position. // or int Max (int, int ); scanf ("% d", & a, & B); // 3 // pass the value of a to the form parameter c, pass the parameters of B to d // assign the return value of Max, that is, the maximum value of a and B to c = Max (a, B ); // 4 // 8 printf ("% d \ n", c); // Output c // 9 return 0; // 10} int Max (int c, int d) // Max Function Definition // 5 {// at this time, the value of c is equal to the value of a, d = B // If c is greater than d, the return value of Max is c. Otherwise, the return value is d if (c> d) // 6 return c; // 7 else // 6 return d; // 7}

Compare the differences between the above two programs)

The above program achieves the maximum value of two numbers. The running sequence of the program is 1 ~ 10. Repetition indicates that one of them may be repeated. Now let's look at these two programs. Is there a lot more than the top program? The second and third programs define a Max function, which has a returned value, soReturnThe following is a number or expression that is consistent with the returned value. The first program defines the type of the returned value as void, which is null, so there is no return value and no return value can be written.Return;Or leave this statement empty. In addition, we can see that I defined variable c in the main function, and I also defined a variable c in the Max function as a form parameter. Isn't it that the variable names in the program cannot be the same? In fact,This is related to the scope of the variable.. There are two types of variables: global variables and local variables. global variables are defined outside the function, and local variables are defined inside the function, global variables only need to be defined once outside the function. This variable can be used in all the functions below it, while local variables are defined inside the function, it can only be used in this function. If other functions need to use it, the parameter must be passed. passed by value, 2. the address is passed to the function that needs to be used.

Let's talk about these two types of transfer.

1. Value Transfer

Only the values of some variables can be passed to the custom function, but the variable address cannot be accessed, if you cannot access the address of a variable, you cannot operate on the variable in the User-Defined Function body. The values used to compare the two numbers are passed, in this case, we only need to know the values a and B to determine whether a is big or B, and do not need to change the values of a and B.

However, in many cases, we do not just want to know the value of a variable,For example, you can exchange the positions of two numbers and then output them.When you see this, you will definitely think it's not a very simple thing !! Then you write the following programs:

Way1

#include<stdio.h>int main(){    int a,b;    scanf("%d%d",&a,&b);    printf("%d %d\n",b,a);}

Way2

#include<stdio.h>int main(){    int a,b,c;    scanf("%d%d",&a,&b);    c=a;    a=b;    b=c;    printf("%d %d\n",a,b);}

Believe that you can write the above two programs. Of course, they are also very correct. But now I am talking about the address transfer of function parameters. So while implementing this program, I also want you to use the function and its parameter is the address. Think about it, what should I do now?

In retrospect,PointerCan the pointer be used to store the address? You can use the pointer to receive the address of the variable in the main function, and then transfer it to the custom function, in a custom function, you can access the addresses in the main function. You can access the addresses to perform operations on the variables corresponding to the addresses.

 

Code:

# Include <stdio. h> void swap (int * p, int * q) // the pointer p and q are the form parameters of the swap function and receive, the address of B {// At this time * p is the same as a in the main function, but the name is different. * q is the same as int c; c = * p; * p = * q; * q = c; return; // because there is no void function returned, you can also directly remove this line} int main () {int a, B; scanf ("% d", & a, & B); // enter two numbers of swap (& a, & B); // & is the address operator, in this way, the addresses a and B can be passed to the swap function printf ("% d \ n", a, B) as the real parameters of the function; // output a and B}

The code above is exactly the same as the two code above, but the code above helps to understand the content, so it is complicated.

If you understand this code, you may wonder if other methods can be used to implement it? Do pointers need to be used?

Many people may easily make the mistake of not passing through the address, but passing through the value to achieve this, so you can write a try,

Code:

# Include <stdio. h> void swap (int p, int q) {// exchange the values of p and q int c; c = p; p = q; q = c; // at this time, the values of p and q are exchanged, but the values of a and B are not exchanged return; // swap function end} int main () {int a, B; scanf ("% d", & a, & B); swap (a, B); // pass the values of a and B to the swap function, in the swap function, a and B printf ("% d \ n", a, B) cannot be accessed again; // Output a and B, and the result is the same as the input, return 0;} is not exchanged ;}

Of coursePrintf ("% d \ n", a, B );Delete. Then, return in the swap function; the preceding sentence is added.Printf ("% d \ n", p, q );.This can also implement functions, but there is no essential difference with the fifth program above.

There is another way to remember not to mention the global variables mentioned above. Its scope is all the functions under its definition.

Code

# Include <stdio. h> int a, B; // defines a and B as global variables, acting on all the following functions void swap () {// exchange a and B int c; c =; a = B; B = c; return; // swap function end} int main () {scanf ("% d", & a, & B ); swap (); // No parameter is required, because both a and B exist in the main function and the swap function printf ("% d \ n", a, B ); return 0 ;}

Because there are too many repeated codes, the running results will not be placed. You can paste and run it yourself.

In this way, the same function is also implemented. Because a and B are global variables, the addresses a and B can be accessed in swap without passing through the address.

So much about functions. Although not mentioned, it is enough for beginners to understand.

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.