標籤:des style blog io ar color os 使用 sp
函數定義: 返回值類型 函數名(形參列表){函數體(函數的實現內容)};
函數定義的四種形式:
//函數定義第一種形式: 無參數, 無傳回值void byMilk() { //如果沒有參數,小括弧必不可少. printf("沒錢\n");}//函數名的命名規範:由多個英文單片語成,除了第一個單詞的首字母小寫,其餘單字首大寫.//函數定義第二種形式,有傳回值,無參數float salary() { printf("同志們辛苦了\n"); return 0.1; //return 將該函數的值返回主調函數. //printf("同志們辛苦了\n"); 不能把代碼放到return下邊,會執行不到.}//函數定義第三種形式.無傳回值,有參數.void watch(int money){ if (money < 10) { printf("不買"); } else { printf("買了"); }}//函數定義的第四種方式,有傳回值,有參數.//求兩個數的最大值.int maxValue(int a ,int b) { int max = 0; if (a > b) { max = a; } else { max = b; } return max;} //上述求最大值最佳化int maxValue1(int a ,int b) { int max = 0; max = a > b ? a : b; return max;} //更簡寫int maxValue2(int a ,int b) { return a > b ? a : b;} 函數與函數之間可以嵌套調用(也就是在一個函數內部可以調用另外一個函數).但不能嵌套定義(不能在一個函數內部定義另外一個函數).
main 函數(主函數) 應用程式執行的入口.
函數相當於公司的部門,每一個部分完成特定的功能,部門與部門之間的並列關係決定了函數與函數之間也是並列關係.
函數功能:實現代碼的模組化管理.把複雜的邏輯進行拆分,而主函數只起到宏觀調控的作用即可.
例:
//3個數的最大值2int maxThree1(int a, int b, int c) { // int max = 0;//儲存最大值 //方法1 //max = a > b ? a : b; //max = max > c ? max : c; //調用函數 //max = maxValue2(a, b); //max = maxValue2(max, c); //簡 //max = maxValue2(maxValue2(a, b), c); //更簡 return maxValue2(maxValue2(a, b), c);}//3個數最小值1int minThree(int a, int b, int c) { return a < b ? a < c ? a : c : b < c ? b : c;}//3個數最小值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);}//中間值1int midThree(int sum, int min, int max) { return sum - min - max ;}//中間值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); }//求四個數的最大值(函數的嵌套應用)int maxFour(int a, int b, int c, int d) { //return maxValue2(maxValue2(a, b), maxValue2(c, d)); //return maxValue2(maxThree1(a, b, c), d); //return maxValue2(a, b) > maxValue2(c, d) ? maxValue2(a, b) : maxValue2(c, d); return maxThree1(maxValue2(a, b), c, d);}//求5個數的最大值int maxFive(int a, int b, int c, int d, int e) { return maxValue2(maxFour(a, b, c, d), e);} 實參和形參:
實參:函數調用時給定的參數叫做實參,是一個唯一確定的資料.
形參:形式上的參數,在函數定義時,給定的參數叫做形參,一個一個的變數,儲存的資料在函數調用之前未知.
實參向行參傳遞的過程是一個拷貝的過程.
/** 變數的範圍:變數可以訪問的範圍. 局部變數:在函數內部定義的變數叫做局部變數.只在函數內部可訪問.函數執行時開闢空間,函數執行結束空間自動回收. 全域變數:在函數外部定義的變數叫做全域變數,在全域都可以訪問,空間不會回收.(注:全域變數非常危險,使用需謹慎) 靜態變數:凡是被static修飾的變數都叫做靜態變數. 特點:1.如果不賦初值,預設為0 2.只初始化一次.(該變數只會定義一次) 3.空間一但開闢不會回收. */int sumTwo(int a) { static int sum = 0; sum += a; return sum;} 函數應用:
//求兩個數的最大公約數int maxGY(int x, int y);//求兩個數的最小公倍數int minGB(int x, int y);//給數組元素賦值void copyArray(int arr[], int count);//arr[]用來接受傳入的數組.count,用來接受傳入的數組元素的個數.//對數組進行升序排序void sortArray(int arr[], int count);//輸出數組元素void OutputArray(int arr[], int count);//多數組元素降序排列void sortArraydesc(int arr[], int count);
2.函數實現.
//求兩個數的最大公約數int maxGY(int x, int y){ int rem1 = rem(x, y); while (rem1 != 0) { x = y; y = rem1; rem1 = rem(x, y); } return y;}//求兩個數的最小公倍數int minGB(int x, int y){ return (x * y) / maxGY(x, y);}//函數內唯寫與本函數功能相關的代碼.//給數組元素賦值//arr[]用來接受傳入的數組.count,用來接受傳入的數組元素的個數.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");} //對數組進行升序排序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; } } } }//對數組元素進行降序排列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; } } } }//輸出數組元素void OutputArray(int arr[], int count) { for (int i = 0; i < count; i++) { printf("%d ",arr[i]); } printf("\n");}
3.主函數調用:
int max = maxGY(30, 6); int min = minGB(30, 6); printf("%d,%d", max, min); int a[10] = {0}; copyArray(a, 10);//當數組作為函數參數時,傳遞數組名即可. OutputArray(a, 10); sortArray(a, 10); OutputArray(a, 10);
4.遞迴
例子:
1.函式宣告:
//函數類比吃蘋果void eatApple(int n);//輸入54321倒序輸出void reverse(int number);<span style="font-size:18px;"></span><pre name="code" class="cpp">//正序輸出void noReverse(int number)//階乘求一個數的階乘n!int str(int number);
2.函數的實現,及遞迴方法的封裝
void eatApple(int n) { //一旦發現蘋果個數為0,則通過return結束當前函數執行 //遞迴一定要有出口, 否則或造成死迴圈 if(n == 0) { return;//返回空 } //如果不為0 //1.留一個蘋果 n--; //2.找下一個人來吃蘋果 eatApple(n); //3.吃自己手裡的蘋果. printf("第 %d 個人吃蘋果\n", 10 - n);}//倒序void reverse(int number) { if (number == 0) { return; } //留下個數 int n =number % 10; //報數 printf("%d ", n); reverse(number / 10); //找下一個人報數.}//正序輸出void noReverse(int number) { if (number == 0) { return; } reverse(number / 10); int n = number % 10; printf("%d ",n);}//階乘int str(int n) { if (n == 0 || n == 1) { return 1; } return n * str(n - 1);} 3.函數的調用
#import <Foundation/Foundation.h> //吃蘋果操作 eatApple(10); //倒序輸出 reverse(54321); printf("\n"); noReverse(12345); int x = str(5); printf("%d", x);
四.結構體數組與函數之間的應用
1.函式宣告
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.函數實現
//輸出單個學生資訊void OutputStudent(Student s) { printf("%s,%d,%.1f \n", s.name, s.age, s.score);}//輸出所有學生的資訊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]); }}//將學生按照成績升序排列void studentAsc(Student str[], int count) { for (int i = 0; i < count - 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; } } } }//講學生按照年齡降序排列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; } } } }//講學生按照姓名升序排列void studentNameAsc(Student str[], int count) { 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.函數調用
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) { //定義學生結構體數組 Student stu[5] = { {"hh", 10, 30}, {"hehe", 22 ,89}, {"heihei", 32,59.0}, {"hiahia", 34, 86}, {"hihi", 23, 77} }; //輸出所有學生的資訊 allOutputStudent(stu, 5); //按照學生姓名升序 printf("------按照姓名升------\n"); studentNameAsc(stu, 5); allOutputStudent(stu, 5); //按照年齡降序 printf("-------按照年齡降-----\n"); studentAgeDesc(stu, 5); allOutputStudent(stu, 5); //按照分數升序 printf("------按照分數升-----\n"); studentAsc(stu, 5); allOutputStudent(stu, 5); return 0; }
【學習ios之路:C語言】函數及遞迴的簡單應用