function Definition: return value type function name (formal parameter list) {function body (Implementation content of function)};
Four types of function definitions:
function definition First form: No parameter, no return value void Bymilk () {//If there are no arguments, the parentheses are necessary. printf ("no money \ \");} Naming conventions for function names: consists of multiple English words, except for the first letter in lowercase, and the first letter of the remaining words.//function defines the second form, with a return value, no parameter float salary () { printf ("Comrades have worked hard \"); return 0.1; return returns the value of the function to the keynote function. printf ("Comrades have worked hard \"); The code cannot be placed below the return and will not be executed.} The function defines a third form. No return value, parameter. void watch (int money) { if (Money <) { printf ("Do not buy"); } else { printf ("bought"); The fourth way that the c9/>}}//function is defined, has a return value, has parameters.//The maximum value of two numbers. int maxValue (int a, int b) { int max = 0; if (a > B) { max = A; } else { max = b; } return Max;} The above-calculated maximum optimization int maxValue1 (int a, int b) { int max = 0; max = a > B? A:B; return Max;} abbreviated int maxValue2 (int a, int b) { return a > B a:b;}
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).
Main function (main function) the entry that the application executes.
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 the function and function is also a parallel relationship.
function function: Implement the modular management of the code. The complex logic is split, while the main function can only play the role of macro-control.
Cases:
3 Number maximum 2int maxThree1 (int A, int b, int c) {//int max = 0;//Storage Maximum//Method 1//max = a > b? a:b; max = max > c? MAX:C; Call Function//max = MaxValue2 (A, b); max = MaxValue2 (max, c); Jane//max = maxValue2 (MaxValue2 (A, B), c); More Simple return maxValue2 (MaxValue2 (A, B), c);} 3 Number minimum 1int minthree (int A, int b, int c) {return a < b? A < C? A:c: B < c? b:c;} 3 Number minimum 2int minThree1 (int A, int b, int c) {//int min = 0; min = MinValue3 (A, b); min = minValue3 (min, c); Min = a < b? A:B; min = min > c? C:min; min = MinValue3 (MinValue3 (A, B), c); Return MinValue3 (MinValue3 (A, B), c); Middle value 1int midthree (int sum, int min, int max) {return sum-min-max;} Median 2int midThree1 (int A, int b, int c) {//int max = a > b? a > C? a:c: B > C? b:c; int min = a < b? b < c? A:C: b < c? B:C; int max = MaxThree1 (A, B, c); int min = MinthrEe1 (A, B, c); Return a + B + c-max-min; Return a + B + c-maxthree1 (A, B, c)-MinThree1 (A, B, c); }//the maximum value of four numbers (nested application of functions) int maxfour (int a, int b, int c, int d) { //return maxValue2 (MAXV Alue2 (A, B), MaxValue2 (c, D)); //return maxValue2 (MaxThree1 (A, B, c), D); & nbsp; //return MaxValue2 (A, B) > MaxValue2 (c, D)? MaxValue2 (A, B): MaxValue2 (c, D); return MaxThree1 (MaxValue2 (A, B), C, D);} The maximum value of the 5-digit int maxfive (int a, int b, int c, int d, int e) { return maxValue2 (Maxfour (A , B, C, D), E); Argument and Formal parameters:
Argument: The parameter given when a function call is called an argument is a uniquely determined data.
Formal parameters: In the case of a function definition, the given parameter is called the formal parameter, one variable, and the stored data is unknown before the function call.
The process of passing an argument to a row parameter is a copy of the process.
Scope of The/** variable: the range to which the variable can be accessed. Local variables: variables defined inside a function are called local variables. Accessible only within the function. Space is created when the function executes, and the function execution end space is automatically reclaimed. Global variables: Variables defined outside the function are called global variables and can be accessed globally, and space is not recycled. (Note: Global variables are dangerous, use caution) Static variables: Variables that are statically modified are called static variables. Features: 1. If you do not assign an initial value, the default is 0 2. Initialize only once. ( The variable is defined only once) 3. Space one but the opening will not be recycled. */int sumtwo (int a) { static int sum = 0; sum + = A; return sum;}
functionApplication:
Greatest common divisor int maxgy (int x, int y) for two numbers, or//For two numbers of least common multiple int mingb (int x, int y);//Assign value void Copyarray (int arr[], int count) to the array element;//ar R[] is used to accept incoming arrays. Count, which is used to accept the number of array elements passed in.//arrays are sorted in ascending order void Sortarray (int arr[], int count);//output array element void Outputarray (int arr[] , int count);//Most group elements are arranged in descending order of void Sortarraydesc (int arr[], int count);
2. Function implementation.
Two number of greatest common divisor int maxgy (int x, int y) {int rem1 = REM (x, y); while (rem1! = 0) {x = y; y = rem1; REM1 = REM (x, y); } return y;} Two number of least common multiple int mingb (int x, int y) {return (x * y)/maxgy (x, y);} The function only writes code that is related to the function function.//Assign the array element//arr[] to accept the incoming array. Count, which is used to accept the number of array elements passed in. void Copyarray (int arr[], int count) {for (int i = 0 ; I < count; i++) {Arr[i] = Arc4random ()% (40-20 + 1) + 20; printf ("%d", arr[i]); }//printf ("\ n");} //the array in ascending order void Sortarray (int arr[], int count) {for (int i = 0; i < count-1; i++) {for (int j = 0; J < count-1-I; J + +) {if (Arr[j] > arr[j + 1]) {int temp = arr[j]; ARR[J] = arr[j + 1]; Arr[j + 1] = temp; }}}}//array elements in descending order void Sortarraydesc (int arr[], int count) {for (int i = 0; i < count-1; i++) { for (int j = 0; J < count-1-I; j + +) {if (Arr[j] < Arr[j + 1]) { int temp = Arr[j]; ARR[J] = arr[j + 1]; Arr[j + 1] = temp; }}}}//output array element void Outputarray (int arr[], int count) {for (int i = 0; i < count; i++) {printf ("%d", arr[i]); } printf ("\ n");}
3. Main function Call:
int max = maxgy (6); int min = MINGB (6); printf ("%d,%d", Max, Min); int a[10] = {0}; Copyarray (A, 10);//When an array is used as a function parameter, the array name is passed. Outputarray (A, 10); Sortarray (A, 10); Outputarray (A, 10);
4. Recursion
Example:
1. Function declaration:
The function simulates eating apple void eatapple (int n);//input 54321 reverse output void reverse (int number); <span style= "font-size:18px;" ></span><pre name= "code" class= "CPP" >//positive sequence output void Noreverse (int number)//factorial to find factorial n!int str (int numbers) ;
2. Implementation of functions and encapsulation of recursive methods
void Eatapple (int n) { ///Once the number of apples is found is 0, then the current function execution //recursion must have exits by return, otherwise, or cause a dead loop if (n = = 0) { return;/ /return to NULL} //If not 0 //1. Leave an apple n--; 2. Find the next person to eat an apple eatapple (n); 3. Eat the apples in your hand. printf ("%d person eats apples \ n", 10-n);} reverse void reverse (int number) { if (number = = 0) { return; } Leave number int n =number%; Count printf ("%d", n); Reverse (NUMBER/10); Find the next person for a count.} Positive order output void Noreverse (int number) { if (number = = 0) { return; } Reverse (NUMBER/10); int n = number%; printf ("%d", n);} factorial int str (int n) { if (n = = 0 | | n = = 1) { return 1; } return n * STR (n-1);}
3. Invocation of functions
#import <Foundation/Foundation.h> //eating Apple Operation Eatapple (ten); Reverse output reverse (54321); printf ("\ n"); Noreverse (12345); int x = str (5); printf ("%d", x);
Four. Application of structure array to function
1. Function declaration
void Studentnameasc (Student str[], int count) <pre name= "code" class= "CPP" >void STUDENTASC (Student str[], int count);
void Studentagedesc (Student str[], int count);
void Alloutputstudent (Student str[], int count);
void Outputstudent (Student s);
2. Function implementation
Output individual student information void outputstudent (Student s) {printf ("%s,%d,%.1f \ n", S.name, S.age, S.score);} Output information for all students void Alloutputstudent (Student str[], int count) {for (int i = 0; i < count; i++) {//printf ("%s, %d,%.1f \ n ", Str[i].name, Str[i].age, Str[i].score); Outputstudent (Str[i]); }}//the students according to the grades in ascending order void Studentasc (Student str[], int count) { for (int i = 0; i < cou nt-1; i++) { for (int j = 0; J < count-1-I; j + +) {   ; if (Str[j].score > str[j + 1].score) { Student temp = str[j]; Str[j] = str[j + 1]; str[j + 1] = temp; } } } }//speaking students sorted by age descending void Studentagedesc (Student str[], int count) { for (int i = 0; i < count-1; i++) { for (int j = 0; J < count-1-I; j + +) {   ; if (Str[j].age < Str[j + 1].age) { Student temp = str[j]; Str[j] = str[j + 1]; str[j + 1] = temp; } } } }// Students are listed by name in ascending order void Studentnameasc (Student str[], int count) {&NBSP;&NBSp for (int i = 0, i < count-1; i++) { for (int j = 0 ; J < count-1-I; J + +) { if (strcmp (str[j].name, Str[j + 1].name) > 0) { Student temp = str[j]; Str[j] = str[j + 1]; str[j + 1] = temp; } } } }
3. Function calls
#import <foundation/foundation.h>int Main (int argc, const char * argv[]) { //define student struct array Student stu[5] = {
{"HH", Ten, +}, {"hehe", +, (), {" Heihei", 32,59.0}, { "Hiahia" , "Hihi", "The", "(+)}", "{", "+", "Max", };< c9/>//output information for all students alloutputstudent (Stu, 5); According to the student's name Ascending printf ("------by name------\ n"); STUDENTNAMEASC (Stu, 5); Alloutputstudent (Stu, 5); In descending order of age printf ("-------by age-----\ n"); Studentagedesc (Stu, 5); Alloutputstudent (Stu, 5); According to the score ascending printf ("------by fractions-----\ n"); STUDENTASC (Stu, 5); Alloutputstudent (Stu, 5); return 0; }
"Learning the path of iOS: C language" function and simple application of recursion