And call overloaded function

Call functions

1. Format: function (parameter 1, parameter 2, ...)
2. function does not call the call will not be implemented
in three formats 3. The function call

  • If you call the function does not return a value, you can directly call
  • If the calling function's return value
  • If the result is to be used in another location, use a variable to store the return value of the method call
  • If you want to return the value of the output function, call the method directly written in the output statement parentheses memory function execution

Call: jointly determine a function by function name and parameter list;

Benefits: lets developers need to remember the name of the function is greatly reduced, improving development efficiency.

Recursive call:

People iterative, recursive God

What is recursive

Itself is the method calls itself recursively

Recursive: first delivery, after the return. We can recursively divided into three parts, namely:

Forward segment: refers to the question from Dahua small talk

The end of the paragraph: The problem can no longer continue to minor deal with the current problems

Return segments: small problem after the deal is completed, back up (some of the problem is no return)

Recursive loop and must be written in three parts:

  • The initial value
  • Termination condition
  • Long before progress

Recursive main problem to solve:

  • factorial
/** 
 * 计算5的阶乘(result = 5*4*3*2*1) 
 * @author Champion.Wong 
 *  
 * 
 */  
public class Jiecheng {  
    public static void main(String[] args) {  
        System.out.println(f(5));  
    }  
      
    public static int f(int n) {  
        if (1 == n)   
            return 1;  
        else   
            return n*f(n-1);  
    }  
}  
  • Progression
/** 
 * 求数列:1,1,2,3,5,8......第40位的数 
 * @author Champion.Wong 
 *  
 */  
public class Shulie {  
    public static void main(String[] args) {  
        System.out.println(f(6));  
    }  
      
    public static int f(int n ) {  
        if (1== n || 2 == n)   
            return 1;  
        else  
            return f(n-1) + f(n-2);  
    }  
}  
  • Fibonacci number

 Fibonacci number first and second bit values ​​are 1, after the first two, and each and every one;

Iteration:

Iteration: the use of a variable principle calculate the new value of the variable. If we say that calls itself recursively, the iteration is then continuously A call B

Recursive and iterative difference:

  • In general, anything can be iterated (loop) to solve the problem, recursion can; and problem solving recursive, iterative might not
  • Recursive function is push, push times more, is bound to occupy memory, can not be avoided

On some issues, recursive code written less than one iteration writes code

On some issues, iterations can not be written, it can only use recursion

  • Recursive divide and conquer is actually an implementation of (an implementation of ideas)

Divide and conquer algorithm is an idea, divide and conquer the main problem is the big issue, split, split into several small problems to solve, the solution will eventually be merged every little problem.

In fact, the divide and conquer is a kind of brute force method (exhaustive), the answer is also an optimal search algorithm

Next, we were used to thinking iteration and recursion to find Fibonacci front row and 10.

Ideological analysis:

Before 10: 2. 3. 1. 1. 5. 8 ...... 13 is 21 is 34 is 55
        F (n) refers to the n-th Fibonacci term
        f (n) = f (n -1) + f (n -2) F (. 1). 1 = F (2) =. 1
                    F (. 5)
                F (. 4) F (. 3)
            F (. 3) F (2) F (2) F (. 1)
        F (2) F (. 1 )
 

public class Feibo {
    public static void main(String[] args) {
        System.out.println(fab_recursion(10));
        System.out.println(fab_iteration(10));
    }
    //迭代实现斐波那契
    private static long fab_iteration(int index) {
        if (index ==1 || index == 2) {
            return 1;
        }
        else {
            long f1 = 1l;
            long f2 = 1l;
            long f3 = 0;
            for ( int i = 0; i < index-2; i++) {
                f3 = f1 + f2;//利用变量的原值推算出变量的一个新值
                f1 = f2;
                f2 = f3;
            }
            return f3;
        }
    }
    //递归实现斐波那契
    private static long fab_recursion(int index) {
        if (index ==1 || index == 2) {
            return 1;
        }
        else {
            return fab_recursion(index-1) + fab_recursion(index - 2);//递归求值
        }
    }
}
    

Recursion is actually difficult for programmers to facilitate the machine, recursive mathematical formulas can be easily converted to a program. The advantage is easy to understand, easy to program. But recursion stack mechanism is implemented, goes one level deeper, we must take up a stack data region, some deep nesting algorithm, recursive run out of steam, the memory space will collapse in the end, but also with recursion to a large number of function calls, it also has a lot of extra time overhead. So in great depth, it would look very bad time and space. (Take up a lot of memory space.

The iterative Although high efficiency, increased uptime because the number of cycles increases, no overhead, and no increase in space, but the disadvantage is not easy to understand, difficult when writing complex problems.

Overload:

Concept: In the same class can define a plurality of identical names, the list of functions of different parameters, such cases do more overloaded function;

public static int sum(int a,int b)
{    
    return a + b;
}
public static float sum(float a,float b)
{    
    return a + b;
}

Features:

  • Regardless of the return type, just look at the method name and parameter list
  • When called, the virtual machine to distinguish between different parameters by the same method on the list of

Benefits: that we can extend the functionality of the function (function same name, but not the same type of parameters, execute the content may not be the same)

The process of looking for an appropriate function

1. see if there is an exact parameters defined int + int see if there is (int, int)

2. see if there is a compatible parameter defines int + int see if there is (double, double)

3. If the parameter is defined with a plurality of compatible int + int, (double, int) or (int, double) is not clear at this time being given reference

 


 

发布了65 篇原创文章 · 获赞 3 · 访问量 1776

Guess you like

Origin blog.csdn.net/weixin_44077638/article/details/104265674