C Language Chapter 2

Source: Internet
Author: User

Directory of this document

    • First, the basic concept
    • Second, the definition of function
    • Iii. formal parameters and actual parameters
    • Iv. type of return value
    • V. Return
    • Six, the function definition of attention
    • Vii. Common functions

We have already finished the basic sentences and basic operations in C, and this is how to introduce the plays---functions in C language. In fact, the concept of function, in most high-level language is very important, I have also in the "First C language Program," A brief introduction to the function.

Back to top one, basic concept 1. What is a function

Any C language program is composed of one or more program segments (small programs), each of which has its own function, which we generally call "functions". So, you can say that the C language program is composed of functions.

For example, if you write a MP3 player program in C, the program structure is as follows:

    • As you can see, a function is a program segment used to implement a function, and each function has its own function. Therefore, you should write the code needed to implement a function in the function. For example, one function is to play MP3, so the code that plays MP3 should be written in this function.
    • When a function is called (executed), the computer executes all of the code in the function sequentially, demonstrating the functionality implemented by the function.

2. Name of function

In general, we will give different functions to different functions to implement. For example, write the code that pauses the MP3 to a function, and write the code that stops the playback MP3 to another function. Therefore, there may be a lot of functions in a C program. To facilitate the invocation and differentiation of these functions, we need a unique name for each function, and the naming of the function must be named according to the identifier. The function name can be used to invoke the corresponding function.

Back to top second, definition of function 1. Any function must be defined before it can be used

The purpose of defining a function is to write down what you are doing with the function and what code it contains. The function is defined in the following format:

1 return value type  function name (formal parameter list) 2 {3     function body 4}

2. For example

For example, define a function to calculate the and of two integers. Then it can be written as follows:

1 #include <stdio.h> 2  3 int sum (int a, int b) 4 {5     int c = a + B; 6     return C; 7} 8  9 int main () 10 {one     int d = SUM (ten, one);     printf ("D is%d", d);     return 0;15}
    • The SUM function, the main function, is defined in line 3rd and line 9th, where the SUM function evaluates to two integers.
    • Once the function is well defined, the code inside the function is not executed immediately, and the internal code is executed if someone calls it. It is as if your phone has the function of calling, but only if the dialing key is pressed, the function of calling will be performed.
    • When the program runs, it calls the main function and executes the 11th to 14th line of code sequentially, because the main function is the entry for the program.
    • The sum of line 11th (10, 11) is called the SUM function, and the SUM function is called, allocating the storage to all parameters in the formal parameter list, which is the variable A and variable B in line 3rd, which are "formal arguments"
    • The 11th line of 10, 11 is called the "actual parameters", they will be assigned to the variable A, variable B, which is equivalent to
1 int a = 10;2 int b = 11;
    • Now that the SUM function is called, the code in the SUM function is executed sequentially, which is the 5th to 6th line of code.
    • The 5th line of the A+b value 21 is assigned to the variable C, row 6th uses the return keyword to return the variable C to the function caller, and the value of the 11th line sum (10, 11) is 21. Then the value of the variable D is 21.
    • The output of line 13th is:
D is 21
    • The variables a, B, and C in line 3rd to 7th allocate storage space only when the sum function is called, and when the SUM function finishes executing, the variables a, B, and C are freed of storage space. Therefore, their scope is limited to the SUM function inside.
    • The int on the left of the 3rd line sum is called the return value type, and the value followed by the 6th line return keyword is called the return value, so the variable C of row 6th is the return value, and the data type of the return value should be consistent with the return value type.

Back to top three, formal parameters and actual parameters 1. Basic Concepts

1> formal parameters: When defining a function, the variable defined in the parentheses () following the function name is called the formal parameter, or the formal argument

2> actual parameter: The value passed in when the function is called is called the actual parameter, or the argument

2. The number of arguments passed when calling a function must be consistent with the number of parameters of the function
1 int sum (int a, int b, int c) 2 {3     return a + B + C; 4} 5  6 int main () 7 {8     sum (Ten, 9, 7); 9     return 0; 10}

There are 3 parameters in the SUM function in line 1th, so when you call the SUM function on line 8th, you need to pass in 3 arguments

3. When you use the base data type (char, int, float, and so on) as the argument, only the value is passed between the argument and the parameter, and the value of the modified parameter does not affect the argument
1 #include <stdio.h> 2  3 int test (char a) 4 {5     a = ten; 6      7     return 0; 8} 9 int main () one {     Char B = 5;13     Test (b);     printf ("Value of B is%d", b); +     return 0;19}
    • The test function is defined in line 3rd with only one parameter a
    • In line 14th, the test function is called and then the memory is allocated to variable a. Here the variable B is treated as an argument, then the value of variable B is passed to the variable A. This time, the memory is roughly as shown:

(actually stored in memory is binary data, here I write 10 binary is for the sake of intuition)

    • After the 5th line of code is executed, the value of variable a becomes 10, but the value of variable B remains 5.

    • The output of the 16th line of code is:
The value of B is 5.

Therefore, the values of the parameters are modified inside the function and do not affect the outside arguments.

4. Function can have no formal parameter

You can have no formal parameters when defining a function, such as the following function

1 #include <stdio.h> 2  3 int Test () 4 {5     printf ("Call test Function"); 6     return 0; 7} 8  9 int main () 10 {11< C5/>test ();     return 0;13}
    • The test function is defined in line 3rd, and it can be found that its formal parameter list is empty, that is, no physical parameter
    • Then when you call the test function on line 11th, you don't have to pass any arguments
    • In fact, the main function defined in line 9th is also not a formal parameter

Back to top four, return value type 1. The return value is the result returned to the function caller when the function call is finished, returned with the return keyword. When defining a function, indicate the return value type of the function
1 Double pi () 2 {3     return 3.4; 4} 5  6 int main () 7 {8     Double A = Pi (); 9     return 0;10}
    • The 1th line defines a PI function, and the return value type is double, so the value returned with return should be of type double, which returns 3.14
    • After the 8th line of the PI function is called, the function caller gets a value of 3.14, so the value of variable A is 3.14.

2. A function can have no return value, and if there is no return value, the return value type should be represented by void
1 #include <stdio.h> 2  3 void Test () 4 {5     printf ("Call the Test function"); 6     return; 7} 8  9 int main () {11
   test ();     return 0;13}
    • In line 3rd, a test function is defined, it has no return value, so the return of line 6th does not follow any value
    • The test function is called on line 11th

3. If a function does not return a value, the return statement of the last face can be omitted

Therefore, the test function above can be simplified into:

1 void Test () 2 {3     printf ("The test function called"); 4}

4. If a function does not explicitly write the return value type, then the return value type representing the function is int
1 sum (int a, int b) 2 {3     return a + b;4}

As you can see, the SUM function defined by line 1th does not explicitly write out the return value type, then the return value type of the SUM function is int. Therefore, the 3rd line return followed by the INT type data

Back to the top five, Return1.return statements can return a value to the function caller inside the function
1 int sum (int a, int b) 2 {3     return a + B; 4} 5  6 int main () 7 {8     int c = SUM (5, 8); 9     return 0;10}

Line 3rd uses the return statement to return the value of A+b to the function caller, so the value of the 8th row variable C is 13

2. The return statement can be used more than once within a function, and after the return statement is used, the function stops executing immediately, and the code following the return statement is no longer executed
1 int max (int a, int b) 2 {3     if (a>b) 4     {5         return a;6     }7     8     return b;9}
    • This Max function can be used to find the maximum value in two integers
    • If a is greater than B, the 5th line of code is executed, and a is returned directly, and the function stops executing. In other words, the 8th line of code will not be executed.
    • If a is not more than B, execute the 8th line of code and return B to

Back to top six, function definition Note 1. Function names cannot be duplicated

By default, the name of the function must be unique. The following syntax is incorrect:

1 void Test (int a) 2 {3 4}5 6 int test () 7 {8     return 10;9}

The name of the function defined in line 1th and 6 is test, and the compiler will directly error

2. Each function should be defined independently and cannot be nested

The following syntax is incorrect:

1 int Main () 2 {3     void Test () 4     {5     6     }7     8     return 0;9}

In line 3rd, the test function is defined inside the main function, which is the wrong wording.

Back to top seven, common function 1.main function

Starting with the first C language program, you know the main function. The main function is the entrance to the entire C program, with the main function, the C program can run successfully, and the entire C program can only have one main function.

A simple definition of the main function is as follows:

1 int Main () 2 {3     4     return 0;5}

It can have no formal parameters and the return value is of type int. Its return value is used to describe the exit status of the program: if 0 is returned, the program exits normally, otherwise the program exits unexpectedly

2.printf function

You may have noticed that printf, which is always in use, is actually a function, which is the system's own function

1 #include <stdio.h>2 3 int main () 4 {5     6     printf ("I want to learn iOS development"); 7     8     return 0;9}
    • In line 6th, the printf function is called, and the passed parameter is a string constant: "I want to learn iOS development"
    • The function of the printf function is to output content on the screen
    • Note the 1th line of code, if you use the printf function, add this sentence, as for the reason, will be detailed in the following chapters

C Language Chapter 2

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.