C ++ - function (a)

As I have written and functions related to the blog ( C ++ - math functions ).

Function, also known as a method, it is the role definitions reusable code, and to organize and simplify the code.

The so-called reuse, reuse is the meaning. When writing the code, we will reuse a lot of code to. In one example, a calculator program, there are addition, subtraction and other arithmetic operations common. If before, the sum of the number change, we must change the source code, and if done repeatedly addition, we need to write a similar program many times. However, the program logic to the function package after the addition, the addition of a function to call.

#include <iostream>

using namespace std;

int main ()
{
    // Initialization SUM 
    int SUM = 0 ;

    // compute the sum of 1 to 10 and 
    for ( int I = 1 ; I <= 10 ; I ++ ) {
        sum += i;
    }
    cout << "Sum from 1 to 10 is " << sum << endl;

    // compute the sum of 11 to 20 and 
    SUM = 0 ;
     for ( int I = 11 ; I <= 20 ; I ++ ) {
        sum += i;
    }
    cout << "Sum from 11 to 20 is " << sum << endl;

    // calculate an integer of 21 to 30 and the sum 
    SUM = 0 ;
     for ( int I = 21 ; I <= 30 ; I ++ ) {
        sum += i;
    }
    cout << "Sum from 21 to 30 is " << sum << endl;

    return 0;
}

It can be seen similar code we wrote three times. Look at the following code

#include <iostream>

using namespace std;

// The function of this code with the similar encapsulate 
void SUM ( int StartNumber, int endNumber) {
     int SUM = 0 ;

    // Calculation of startNumber ~ endNumber integers and 
    for ( int I = StartNumber; I <= endNumber; I ++ ) {
        sum += i;
    }

    cout << "Sum from " << startNumber << " to " << endNumber << " is " << sum << endl;
}

int main ()
{
    // all integers from 1 to 10 and 
    SUM ( 1 , 10 );

    // all integers of 11 to 20 and 
    SUM ( 11 , 20 );

    // all integers and 21 to 30 
    SUM ( 21 , 30 );

    return 0;
}

Two pieces of code to achieve exactly the same function. However, from the viewpoint of ease of modification, the code for the second segment, if we want to change the range of numbers, then only two parameters to change, if you want to change the functions implemented (e.g., into a number of intervals addition) , then we can only modify the internal implementation of a function. The first piece of code will have to change a lot of things, if you forget to change something, it will cause an error. Benefits function is evident.

Function syntax

 The syntax for defining function is as follows:

returnValueType functionName(list of parameters) {
    //Function body
}
  • returnValueType return value type, it is mainly divided into two categories: type empty (void), there is the return value type. void can refer to the above code, it is no return statement, only performs a series of operations return type; while the type of the return value of the function body must have a return statement, return back returnValueType with the same type of data of this type can includes basic data types (int, double, bool ......) and abstract data types (struct, union ......).
  • functionName is a function name, used to identify different functions
  • list of parameters is a parameter list for this part of the arguments passed to the parameter (argument is actually present, to the parameters passed to the function, and the like as previously 1,10,11,20 code, parameter parameter is defined by the function definition brackets for receiving the arguments, as previously StartNumber code and endNumber)
  • Is a function of the body part between the braces {}, is embodied and functions in the body. Note that this section generally have indented (empty grid 4)
  • Function name and parameter list is called the function signature, i.e. functionName (list of parameters) section
  • Function signature and return type referred to function header , i.e. returnValueType functionName (list of parameters) section

This time is not difficult to find, we often see int main () {...} is a function. The main function is the return value of type int, so the last return 0; 0 indicates successful execution. main function is a system function, is the entry procedure, the system will perform the main function of the code. If there is no main function in your program, only a few other simple functions, then your program will not be implemented.

Function call

Call functions very simple way, using the name of the function () can be. To participate in the above code.

The only caveat is that C ++ function must be declared before use. In other words, the function either written before the main function, or to declare, then write in the back of the main function.

The first

#include <iostream>

using namespace std;

// implemented before the main function 
void function () {
     // function body 
}

int main ()
{
// perform three function function 
    function ();
    function ();
    function ();

    return 0;
}

The second

#include <iostream>

using namespace std;

// before the main function declaration, this part is called the function prototype 
void function ();

int main ()
{
// perform three function function 
    function ();
    function ();
    function ();

    return 0;
}

// achieved after main 
void function () {
     // function body 
}

 

Guess you like

Origin www.cnblogs.com/bwjblogs/p/12634032.html