【学习ios之路:C语言】函数及递归的简单应用

函数定义: 返回值类型 函数名(形参列表){函数体(函数的实现内容)};

函数定义的四种形式:



//函数定义第一种形式: 无参数, 无返回值
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个数的最大值2
int 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个数最小值1
int minThree(int a, int b, int c) {
    return a < b ? a < c ? a : c : b < c ? b : c;
}

//3个数最小值2
int 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);
}

//中间值1
int midThree(int sum, int min, int max) {
    return sum - min - max ;
}
//中间值2
int 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;
    
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值