C-language functions, function calls and function recursion

Source: Internet
Author: User

1. Function definition: Return value type function name (formal parameter list) {function body (Implementation content of function)}, note: Parentheses are also necessary if there are no arguments.

A call can be nested between functions and functions (that is, you can call another function inside a function), but you cannot nest definitions (you cannot define another function within one function)

Formal parameters: In the case of a function definition, the given parameter is called the formal parameter, and is a variable that stores the data before the call is complete.

Argument: The parameter given when a function call is called an argument is a uniquely determined data. The process of passing an argument to a parameter is a copy of the process

function is equivalent to the company's department, each part of the completion of a specific function, department and Department of the side-by-side relationship between function and function is also a side relationship

Function: Implement the modular management of the code. The complex logic is split, and the main function is only the function of macro-control, the main functions are the entrance of application execution

Errors that may occur during function definition:

A. function definition is not a allowed here: Reason: A new function is defined inside the function, nested definition

Solution: The function is a side-by-side relationship that defines the internally defined function externally

B. Control reaches end of non-void function: Reason: The return value cannot be null, the function does not return data through return

Solution: Returns the corresponding data by return

2. function definition form:

A. function definition first form : no parameter , no return value

void Buymike () {

printf(" no money Yet");

    }

naming conventions for function names : There are more than one English barbed , except for the first letter in lowercase , the remaining words are capitalized

B. Second form of function definition : Return value , no parameter

Float salary () {

printf(" comrades have done a good job this year, good work next year, do not job-hopping \ \");

return 1.0;//ruturn Returns the value of the function to the keynote function

//Do not put the code below the return , will not be executed

}

c. Third form of function definition : No return value , parameter

void BuyMike2 (float money) {

if (Money < ) {

printf (" not enough money \ \");

} Else {

printf("OK, buy milk \ n");

}

}

  D. The fourth form of the function definition : There is a return value , there are parameters

Maximum value for two numbers

int maxvalueoftwo (int A, int b) {

return a > B? a:b;

}

    To find the minimum value of two numbers

int minvalueoftwo (int A, int b) {

return a < b? a:b;

}

    Maximum value for three numbers

int Maxofthreenumber (int A, int b, int c) {

1. Max for a and b first

int max = 0;// Store maximum value

max = Maxvalueoftwo (A, b);

2. Max vs . c Max

return maxvalueoftwo(Maxvalueoftwo(A, B), c);

}

3. Function call (let function work)

A. function with no return value: function name (pass the argument table); such as BuyMike2 (3.2);

B. function with return value: Defines a variable corresponding to the return value type used to receive the value returned by the function call. such as: int max = MaxValue (2, 3);

4. Array and function of the relationship, array as the parameters of the function:

A. When an array is a function parameter, only the function name is passed as int a[10] = {0}; Outputarray (A, ten);//outputarray (int arr[], int n) is a function of its own definition

5. The use of functions is divided into three parts :(in Xcode)

A. function declaration , similar to the instruction manual , written in the . h file

B. function definition (Implementation of function ), implementation written in . m file

c. function invocation ( The import header file is essentially a copy of the contents of the . h file into the file )

6. Scope of variables : range of variables accessible

  Local variables : variables defined inside the function are called local variables , are accessible only inside the function, space is created when the function executes , and the function execution end space is automatically recycled

  Global variables : variables defined outside the function are called global variables , accessible globally , and space is not recycled (warning: Global variables are dangerous , use caution )

Static variable : any variable that is modified by static is called a static variable .

features: 1. If the initial value is not assigned, the default is 0

2. Initialize only once ( the variable is defined only once)

3. Once the space is opened, it will not be recycled

7. Function recursion: The recursive function is to invoke a function that is exactly the same as the function (note: Recursion must have an exit , otherwise it will cause a dead loop ), such as:

  

Function Simulation eating apples

void Eatapple (int n) {

//Once the number of apples is found to be 0, end the current function execution by return

Recursion must have an exit , otherwise it will cause a dead loop.

if (n = = 0) {

return; // return null

}

if it is not zero

1. Keep an apple

n--;

2. find the next person to eat an apple

eatapple (n);

3. eat the apples in your own hands

printf (" %d person eats apples : \ n", 10-n);

}

function calls are made directly in the main function: Eatapple (ten);

Reverse 54321

  void reverse (int n) {

if (n = = 0) {

return;

}

1. number of left

int m = n% ;

2. Count off

printf ("%d", m);

3. find the next one.

reverse (n/ ten);

}

Positive Order

void positive (int n) {

if (n = = 0) {

return;

}

1. number of left

int m = n% ;

2. find the next one.

positive (n/ ten);

3. Count off

printf ("%d", m);

}

factorial

int FAC (int n) {

if (n = = 0 | | n = = 1) {//0 and 1 have a factorial of 1

return 1;

}

return n * FAC (n- 1);

}

C-language functions, function calls and function recursion

Related Article

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.