Nested calls and recursive calls for C + + functions learning tutorials _c Language

Source: Internet
Author: User
Tags mathematical functions cmath

Nested calls to C + + functions
C + + does not allow nested definitions of functions, that is, you cannot fully include another function in one function. The definition of each function in a program is parallel and independent.

Although C + + cannot nest definition functions, you can nest call functions, that is, when you call a function, another function is called.

When implementing a function nesting call in a program, it should be noted that before calling a function, you need to declare each function that is called (unless it is defined in the previous call).

"Example" is used to find the root of the equation f (x) =x3-5x2+16x-80=0 by the method of chord truncation.

This is a numerical solution problem, which needs to analyze the algorithm of root-finding using chord-cutting method first. According to mathematical knowledge, you can list the following problem-solving steps:
1) Take two different points x1,x2, if the F (X1) and F (x2) symbol is the opposite, then (X1,X2) The interval must have a root. If f (x1) is the same symbol as F (x2), then the x1,x2 should be changed until F (X1), F (x2) is different. Note that the value of the x1?x2 should not be too large to ensure that there is only one root in the (x1,x2) interval.

2) connection (x1, F (x1)) and (X2, F (x2)) Two points, this line (that is, the chord) to the x axis in x, see figure.


The X-point coordinates can be derived from the following:


Then the F (x) is obtained from X.

3 if f (x) and F (x1) are identical, the root must be within the (x, x2) interval, and X will be used as the new X1. If f (x) and F (x2) are the same symbol, the root is in the (x1,x) interval, and x is used as the new X2.

4 Repeat steps (2) and (3) until |f (x) |<ξ, ξ to a very small positive number, such as 10-6. The f (x) ≈0 is considered at this time.

This is the string-cutting algorithm, in the program using the following functions to achieve some of the above related functions:
1) using function f (x) to represent the function of x: x3-5x2+16x-80.

2) Use function Xpoint (X1,X2) to find (X1,f (x1)) and (X2,f (x2)) of the line and X axis intersection x coordinates.

3) Use the function root (x1,x2) to find the real roots of the (x1,x2) interval. Obviously, the Xpoint function is used during the execution of the root function, and the F function is used in the execution of the Xpoint function.

Based on the above algorithm, you can write down the following program:

#include <iostream> #include <iomanip> #include <cmath> using namespace std; Double f (double); function declaration double Xpoint (double, double); function declaration double root (double, double);
 function declaration int main () {double x1,x2,f1,f2,x;
  Do {cout<< "input x1,x2:";
  cin>>x1>>x2;
  F1=f (x1);
 F2=f (x2);
 while (f1*f2>=0);
 X=root (X1,X2);
 Cout<<setiosflags (ios::fixed) <<setprecision (7);
 Specify Output 7 decimal cout<< "A root of Equation is" <<x<<endl;
return 0;
 Double f (double x)//define F function to implement F (x) {double y;
 y=x*x*x-5*x*x+16*x-80;
return y;
 Double Xpoint (double x1, double x2)//define the Xpoint function to find the intersection of the string and the x-axis {double y; y= (X1*f (x2)-x2*f (x1))/(f (x2)-F (x1));
Call the F function in the Xpoint function return y;
 Double root (double x1, double x2)//define the root function, approximate root {double x,y,y1;
 Y1=f (x1);
   do {x=xpoint (X1,X2);//Call the Xpoint function y=f (x) in the root function;//call F function if (y*y1>0) {y1=y in the root function;
  X1=x;
 else x2=x;
 }while (Fabs (y) >=0.00001);
return x;
 }

The operating conditions are as follows:

Input x1, x2:2.5 6.7↙
A root of equation is 5.0000000

Description of the program:
1 when defining a function, the function name F,xpoint and root 3 functions are independent of each other and are not subordinate to each other. Each of these 3 functions is defined as a double.

2) The definition of 3 functions appears after the main function, so the 3 functions are declared before the main function.

It is customary to place all functions used in this program in the first statement.

3 The program starts with the main function.

4 in the root function, we need to use the function Fabs of absolute value, which is the system function of the absolute value of the double precision number. It belongs to the library of mathematical functions, so the header file is included with #include <cmath> at the beginning of the file.

Recursive calls to C + + functions
In the process of calling a function, the function itself is called directly or indirectly, called a recursive (recursive) call to the function. C + + allows recursive invocation of functions. For example:

int f (int x)
{
 int y, z;
 Z=f (y); In the process of calling function F, you also call the F function return
 (2*Z);
}

The above is a direct call to this function, see the following figure.

The following figure represents an indirect call to this function. Call the F2 function during the call to the F1 function, and call the F1 function during the call to the F2 function.


As you can see from the diagram, both recursive calls are called without terminating themselves. Obviously, there should be no such recursive calls in the program, but only a finite number of recursive calls with a termination, which can be controlled using an if statement, and only if a certain condition is set up to execute a recursive call, otherwise it is no longer possible to continue.

A function that contains a recursive call is called a recursive function.

"For example" there were 5 people sitting together and asking how old was the 5th man? He said he was two years older than the 4th. Asked the 4th man's age, he said he was two years older than the 3rd. Ask the 3rd man, and say two years older than the 2nd. Ask the 2nd person, say two years old than 1th person. Finally asked the 1th man, who said he was 10 years old. How old is the 5th person, please?

Each person's age is two years older than the first 1. That

Age (5) =age (4) +2 Age (
4) =age (3) +2 age
(3) =age (2) +2 age
(2) =age (1) +2 age
(1) =10

Can be expressed as follows:

Age (N) =10 (n=1) Age
(n) =age (n-1) +2 (n>1)

As you can see, when n>1, the formula for the nth person's age is the same. Therefore, you can use a function to represent the above relationship. Figure 4.11 shows the process of asking for a 5th person's age.


You can write the following C + + program, where the age function is used to implement the recursive process described above.

#include <iostream>
using namespace std;
int age (int);/function declaration
int main ()//main function
{
cout<<age (5) <<endl;
return 0;
}
int age (int n)//The Recursive function of ages (int
C;/////C as the age-storing variable
if (n==1) c=10;//When n=1, age is
c=age (n-1) +2; When n>1, the person is the age of his previous person plus 2
return C;//Bring the age value back to the main function
}

The results of the operation are as follows:

18

"Example" uses recursion method to seek n!.
N! can be recursive method, that is, starting from 1, multiply by 2, multiply by 3 ... Always multiply to n. The n! can also be recursive method, that is, 5!=4!x5, and 4!=3!x4,...,1!=1. You can use the following recursive formula to represent:

 N! = 1 (n=0, 1)
 n * (n-1)!  (n>1)

With the foundation of example 4.10, it is easy to write the following procedure:

#include <iostream>
using namespace std;
Long FAC (int);/function declares
int main ()
{
 int n;//n As Integer long y that requires factorial,
 //y for n! variable
 cout<< "please Input an integer: "; Enter the prompt
 cin>>n;//input n
 y=fac (n);//Call the FAC function to n!
 cout<<n<< "!=" <<y<<endl; Output n! value return
 0;
}
Long FAC (int n)//recursive function
{
 long F;
 if (n<0)
 {
  cout<< "N<0,data error!" <<endl; If you enter a negative number, the error is f=-1 with 1 as the return value
  ;
 }
 else if (n==0| | N==1) f=1; When the value of 0! and 1 is 1
 else F=FAC (n-1) *n;//n>1, the recursive call
 return f;//returns the value of F as the function value
}

The operating conditions are as follows:

Please input an Integer:10↙
10!=3628800

Many problems can be handled either recursively or by a non recursive method. In the realization of recursion, in time and space overhead is relatively large, but in line with people's thinking, procedures easy to understand.

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.