C language Learning-functions and recursive functions

Source: Internet
Author: User

C source program is composed of functions, and there is only one main function (main () function).

First, the function

1. The writing format of the Custom function:

返回值类型 函数名(参数类型 形式参数1,参数类型 形式参数2,…){    函数体}
For example:

1Include <stdio.h>2 voidtest ();3 4 intMain () {5 test ();6     return 0;7 }8 9 voidTest () {Ten  Oneprintf"Hello world!");  A  -}

2. Purpose of defining functions

Encapsulates a function to make it easier to call later

3. Steps to define a function

Function name: What are the names of functions?

Function Body: What does the function do and what code it contains

Return value type: What is returned to the caller after the function has finished executing

4. Definition of no parameter, no return value function

Format:

void function name () {

function body;

}

For example:

void say () {      printf (" speak up, how you are! ");}

5. Definition of a function with a return value without a parameter
Format:

return value type function name () {

function body;

}

For example:

int sum () {    return;  }

6. The definition of a parameter without a return value function

Format:

void function name (parameter type form parameter 1, parameter type parameter 2 ...) {

function body;

}

For example:

1 void printline (int  n) {2     int1;         3      while (I <= n) {4          printf ("---------"); 5          i++; 6     }   7     8 }

7. Parameters have the definition of a return value function

Format:

return value type function name (parameter type parameter 1, parameter type parameter 2 ...) {

function body;

}

For example:

1 int sum (intint  b) {2     return  a + b; 3 }

Second, recursive function

A function calls itself in its function body called a recursive call. This function is called a recursive function. The C language allows recursive invocation of functions. In recursive invocation, the keynote function is also the function of the tune. Executing a recursive function calls itself repeatedly, entering a new layer each time it is called.

void function (int  x) {    function (x);}
    • themselves call themselves
    • There is a condition that allows recursion to end
    • The scale of the problem can be reduced

For example:

Gets the number entered by the user until the user enters a positive

1 voidInputnumber ()2 {3     intNumber =-1;4printf"Please enter a positive number abc\n");5scanf"%d", &Number );6     if(Number <0) {7 //Negative number8 Inputnumber ();9}Else{Ten //Positive Oneprintf"Number =%d\n", number); A     } -}

Using recursion to find the factorial of n
1 intFactorial (intN) {2     intresult =0;//define the result of variables used to hold factorial3     if(n==1) {//if the n=1, 1! The result is still 1 .4result =1;5}Else{6result = Factorial (n1) *n;//if not 1, factorial = (n-1)!*n;7     }8     returnresult;9}

C language Learning-functions and recursive functions

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.