C language-function and recursion

Source: Internet
Author: User
Tags greatest common divisor

1. A function is called a method, which means a block of code that implements a function or completes a task.

The body of the function begins with the curly brace, and the closing of the curly brace//function consists of the//main function, which is the function//function that is called to the system: the return value, the function name, the passed parameter//such as: Implement two integers to add, return their and void show (void) {    printf (" Hello world!\n ");} int add (int x, int y) {    return x+y;} int main (int argc, const char * argv[]) {    int val;    Show ();    val = Add (3, 5);    printf ("%d\n", Val);    return 0;}

Function composition: function name function interface function body return value//function call//function//function return value is the result of function run//formal parameter is also called parameter variable, actually is the variable//actual parameter is the actual existence of the value//parameters equivalent to the variables defined in the function, The procedure that invokes a function to pass a parameter is equivalent to a variable that defines a formal parameter initialized with the value of the argument (the variable defined inside the function is called the local variable void print_time1 (int hour, int minute) {    printf ("%02d:%02d\n ", hour, minute);    printf ("%p%p\n", &hour, &minute);} void print_time2 (int hour, int minute) {    print_time1 (hour, minute);    printf ("%p%p\n", &hour, &minute);} int main (int argc, const char *argv[]) {    int hour, minute;    hour = n;    minute =;    printf ("%p%p\n", &hour, &minute);    Print_time2 (Hour,minute);        return 0;}

A local variable is a variable declared inside the function body, it is only valid inside the function body, that is, it can only be used inside the function, it cannot be used outside the function, only in the use of the function is called, the variable will allocate space in memory, call end, memory release;

Testing local variables every time the memory location is allocated is indeterminate

A global variable is a variable defined outside the body of the function (including the main function), which does not belong to the source program, so the global variable can be called by all functions in the program, its effective range from the definition to the end of the source program;

The life of a global variable is long, it consumes memory throughout the execution of the program, and when the program ends, the "life" of the variable ends, and the system frees up the memory it occupies.

Disadvantages of global variables: values are easily modified

Scope, the scope of a local variable is from the beginning of the definition of the variable to the end of the closing curly brace, the scope of the global variable is from the definition of the variable to the end of the source program

Declaration and definition of functions

The declaration of a function is to tell the compiler what kind of function exists, which can be found later, and if there is no declaration, there may be an error that could not find the function.

Use of library functions: When using library functions, the header file containing the library functions is required

2. Recursive invocation of functions

A function calls itself in its function body, called a recursive function.

In the C language, recursive calls are allowed, and in recursive calls, the keynote function is also the function of being tuned.

To prevent recursion from terminating, there must be a condition in the function body to terminate the recursive call

The common way is to add conditional judgment statement, meet a certain condition is not recursive call, layer by level return

Mathematically there is such a definition of n factorial equals n multiplied by n-1 factorial, N-1 's factorial equals (n-1) * (n-2)!

This will never end, so you need to define a basic condition

Base condition: Factorial of 0 is 1

N! = N (n-1)!

0! = 1

So 3! = 3*2! = 3*2*1!= 3*2*1*0!= 3*2*1 = 6;

Because of the basic conditions, there is no endless progress.

So what do we do with the program?

A method in which a procedure or function calls itself directly or indirectly in its definition or description, which usually transforms a large complex problem layer into a smaller problem similar to the original problem, and the recursive strategy can describe the repeated computations needed in the process of solving the problems by a small number of programs. Greatly reduces the code volume of the program. The ability to recursion is to define an infinite set of objects with limited statements. The program written with recursive thought is often very simple and easy to understand.

In general, recursion requires boundary conditions, recursive forward segments, and recursive return segments. Recursion advances when boundary conditions are not met, and returns recursively when the boundary conditions are met.

Attention:

(1) Recursion is the invocation of itself in a process or function;

(2) When using the incremental return strategy, there must be a definite recursive end condition called recursive egress, otherwise it will go indefinitely (deadlock).

Recursive algorithms are generally used to solve three types of problems:

(1) The definition of the data is defined by recursion. (Fibonacci function)

(2) The problem solution is implemented by recursive algorithm. Back

(3) The structural form of the data is defined by recursion. (Traversal of the tree, search of the graph)

Disadvantages of recursion:

Recursive algorithm is less efficient in solving problems. In the process of recursive invocation, the system opens up a stack for each layer's return point, local quantity and so on. Too many recursion times can cause stack overflow and so on.

int factorial (int n) {    if (n==0)    {        return 1;    }    else    {        int recurse = factorial (n-1);        int result = Recurse * N;        return result;}    } Fibonacci Sequence//1 1 2 3 5 8 144 .... int Fibonacci (int n) {    if (n==0| | N==1)     {        return 1;    }    else {        int val = Fibonacci (n-1) + Fibonacci (n-2);        Return  val;    }}

eg. write a recursive function to find the greatest common divisor of two positive integers A and B (Gcd,greatest Common Divisor), using the Euclid algorithm!

If a divided by B can divide, then greatest common divisor is B. Otherwise, greatest common divisor equals the greatest common divisor of B and a%b. The Euclid algorithm is easy to prove, please prove to the reader himself why this is calculated to calculate the greatest common divisor. Finally, modify your program so that it applies to all integers, not just positive integers. int Euclid (int A, int b) {    if (a%b==0)    {        return b;    }    else    {        return Euclid (b, A%b);}    }

eg. interesting question--age. There are 5 people sitting together and asking how old is the five? He said he was 2 years older than the 4th one. Asked the age of 4th, he said he was 2 years older than the 3rd man. Asked the third person, and said that the 2nd People's Congress two years old. Ask the 2nd person, who says two years older than the first one. Finally asked the first person, he said is 10 years old. How old is the fifth one? Using recursive algorithm to implement

int age (int n) {    if (n==1) {        return;    }    else{        return Age (N-1) +2;}    }

C language-function and recursion

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.