Functions of C + +

Source: Internet
Author: User

Function

When it comes to C + + functions, the first thing to learn is to get the functions out of the existing program, let's start with an example.

<span style= "FONT-SIZE:18PX;" > #include <iostream.h>void main () {double x,y;cout<< "Input double x and y:";cin>>x>>y;< Span style= "color: #000099;" >double z=x+y;</span>cout<< "x+y=" <<Z<<ENDL;} </span>

This is a program that asks for the sum of two floating-point numbers, using an identifier sum_double to abstract the function of two floating-point numbers. This abstraction is implemented with several statements, and the abstraction that is obtained is a function, and the identifier sum_double is the name of the functor.

<span style= "FONT-SIZE:18PX;" >double sum_double (double x,double y) {return x+y;} </span>

function Sum_double function is used to obtain the two parameters x and Y.

The Complete program:

<span style= "FONT-SIZE:18PX;" > #include <iostream.h>double sum_double (double x,double y);//description of function sum_double void Main () {double x,y;cout << "Input double x and y:"; cin>>x>>y;double z=sum_double (x, y);//call to function sum_double cout<< "x+y=" <<z<<endl;} Double sum_double (double x,double y)//definition of function sum_double {return x+y;} </span>

the definition format of the function :

< type descriptor > < function name > (< parameter table >)

{

< function body several statements >

}

The type descriptor indicates the type of the function, which is the function return value type. If a function does not have a return value and is just a procedure call, the function type is described as void. A parameter table is made up of one or more parameters, separated by commas, or without parameters. When defining a function, the parameter given in the parameter table is a formal parameter, and you need to indicate the type of the parameter.

Defining functions should be noted:

Many functions can be defined in C + +, and these functions are parallel. , a function is not allowed to be defined in a function, and the relationship of several functions in C + + is a call relationship.
description of the function:

The definition and description of a function are two different things. A function is defined by a statement that describes the function's function, and the description of the function is the type of the function and the type of all parameters before the function is called. A prototype description is required when explaining a function, including the function name, function type, and function parameter type.

Description format for the function:

< type specifier > < function name > < (parameter table) >

Note that the function should be noted:

in the program, the function is defined first, then the description can be omitted after the call, and then the function that is called before the call must be stated before being called.

Evaluation order of function parameters:

When a function takes more than one parameter, the C + + language does not specify the order of evaluation of the arguments in the function call, and allows the different compilation systems to specify the order of evaluation of the arguments according to the needs of the code optimization. The different order of evaluation has no effect on the general expression, and for those operators with side effects, it generates two semantics under the compilation system. The order in which the vc++6.0 is evaluated is from right to left.

The following functions are provided with ambiguity:

<span style= "Font-size:18px;color: #000000;" > #include <iostream.h>int sum_int (int i,int j)//defined with two parameters I and J{return i+j;} void Main () {int x=1,y=2;int s=sum_int (++x,x+y);//called with two arguments x and Ycout<<s<<endl;} </span>

The result under the vc++6.0 compilation system is: 5 (from right to left). If the result is: 6 under other left-to-right compilation systems.

The default value of the function parameter;

You should be aware of the default values for setting function parameters:

1, when specifying a default value, to start at the right end of the parameter table, a parameter that does not specify a default value is not allowed to the right of the parameter with the default value specified.

2, when a function is called, the given argument value supersedes the default value of the parameter, and the default value of the parameter will be used without the given argument value.

3, if a function needs to be explained, the default parameter value should be set in the description of the function, not in the definition.

4, when setting a default value for a parameter, it can be a numeric value, or it can be an expression, the default is generally a global variable, you can make a function, but it cannot be a local variable.

<span style= "Font-size:18px;color: #000000;" > #include <iostream.h>int q=5,p=7;//two global variables Q and pint sum_int (int a,int b=p+q,int c=q*p);// The expression that sets the function parameter when the function is described defaults to void Main () {int x=5,y=10;int s1=sum_int (x);//function call with an argument value int s2=sum_int (x, y),//function call with two argument values cout << "s1=" <<s1<< "s2=" <<S2<<ENDL;} int sum_int (int a,int b,int c)//definition of function {return a+b+c;} </span>

return value of function: s1=52 (5+12+35) s2=50 (5+10+35)

In C + + programs, some functions have return values, and some functions do not return values. In a function with a return value, you need to return the value of an expression using the return statement. The format is: return < expression >.

Implementation of the return statement with the returned value:

1, the value of the expression after the statement is evaluated first when a return statement with a return value is executed

2, the type of the expression is determined according to the type of the function, and if the type of the expression is inconsistent with the type of the function, the expression type is forcibly converted to the function type.

3, the expression value as the return value of the function is passed to the calling function as the value of the calling function, and then assigned to the corresponding variable or output display.

4, turn the execution order of the program to the calling function statement, then execute the statement below the calling function.





Functions of C + +

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.