Simple function explain (Going version)

        In real programming work, we need to complete the code will be very large, so the code is divided into different logical block is necessary, each block has a relatively independent functions, and the provision of external calls to other programs the parameters and return values, this program composed of a plurality of regions will make the program easier for the reader to understand programming concepts.

And can function allows functions are packaged, such a function can be called by other functions in different situations, the concept of function is thus produced.

Make a simple example, say you want to sort, to sort an array of four, four times, you might have to write a bunch of similar code.

But if there is a function, you can put that code written in a function, each time can pass an array as an argument.

FIG such as code in the function Djudeg

 

If you want to compare two numbers every time the size of direct transmission parameters on the line, without the need for extra cumbersome repeated written, there is the use of function can make your code more clear, each function represents both a function, improve code readability.

The name of the function is also very important, sensible naming people can see at a glance the role of function, do not appear the following:

1 void aaa()
2 {
3     
4 }
5 void abcd()
6 {
7     
8 }

This nomenclature, or naming a function of variables, it is recommended that you go to this blog: Transfer link

Well, to continue speaking function.

Defined Functions

First of all we are talking about the function definition, the definition of a function to be determined in three parts:

1. The return type of the function

2. The name of the function

3. function parameters

 

 

 

Written arguments of the function of some places still a little pit, such as a two-dimensional array, you can look at Baidu good school.

The above is a screenshot DisplayArray called function, the return value is null, followed by an array of parameter a, the length of the array length.

void DisplayArray(int a[],int length)
{
    cout<<"当前数组:"<<endl;
    for(int i=0; i<length; i++)
        cout<<a[i]<<" ";
    cout<<endl;
}

Function returns a null value, can not write return, other types of its function must return the same type of value, such as so

int Add(int a,int b)
{
    return a+b;
}

Call and declare functions

Function definition is over, we must learn to call them.

 If the function is defined in the call back function (below) you will need to declare defined function before the call, otherwise without prior declaration.

Statement mean to tell the compiler, return type, function name and the parameters of this function.

#include <stdio.h>
 int wrongplus ( int A, int B); // declaration of the function portion 
int main () 
{ 
    int A = 2 , B = . 3 ;
     int C; 
    C = wrongplus (A, B); / / function calling part of 
    the printf ( " % D% + D D =% \ n- " , A, B, C); 
} 
int wrongplus ( int A, int B) // function body 
{ 
    A ++ ; 
    B ++ ;
     return a + b;
}

The final output of the program 2 + 3 = 7, visible inside the function, a, b values ​​changed, but its actual value is not changed outside the function, we just put, these two values ​​gives 2,3-shaped reference fills argument will not change.

  Third, the real parameter participating

     When we call the function, received its incoming parameters and functions in vivo parameters is actually not the same variable.

When the function is called a function call parameters passed actual parameters, referred to as arguments, and a function of an incoming internal call parameters thereof for receiving an external parameter called formal parameters, referred to as parameters.

      For example, in the above example, the variables defined in the main function int a and int b, call wrongplus (a, b); when the argument is a two variables, i.e. the value of these two variables passed to the 2 and 3 internal function.

In a functional wrongplus () function in vivo, for receiving int when these two parameters a and int b is a parameter, which is the functions that two additional variables, for receiving two values ​​2 and 3 , the two variables a and b main function is not defined.

     In wrongplus () function in vivo, to make a parameter ++; and b ++; operation, in fact, affects only the interior of the body of the function of these two variables, but did not affect the main function of the value of the variables a and b .

Therefore, in wrongplus () a function of the body, and b is 3 and 4, to return the results of running a function value of 7, and the value a in the main function of two variables, and b is 2 and 3 is still, so printf ( ) shows the result of the function is 2 + 3 = 7.

So far, because of the storm, and, I do not want to write, add content to revel next time ,,

 

Guess you like

Origin www.cnblogs.com/tp25959/p/11735747.html