• 七、C语言函数定义详解


    1、函数的声明和定义

    • 函数声明:
      • 作用:告诉编译器函数的名称,返回类型和参数
      • 格式:reture_type function_name(parameter list);
      • 在函数声明中,函数的名称并不重要,参数的类型是必须的,例如: int maxint(int, int);
      • 当一个源文件调用另一个源文件的函数时,应在文件顶部声明函数
    • 函数定义:定义函数的实际主体

    2、函数的调用

    • 格式:
      • 先声明,后定义
      • 声明,定义同时进行
    • 代码实现:
      #include<stdio.h>
      
      int max(int num1, int num2);//函数声明,num1,num2为形参,进入函数时被创建,退出函数时被销毁
      
      //声明,定义同时进行
      int max1(int num1, int num2){
          int result;
          if(num1 > num2)
              result = num1;
          else
              result = num2;
          return result;
      }
      
      int main(){
          int a = 100;
          int b = 200;
          int ret;
          int ret1;
      
          ret = max(a,b);//函数调用
          ret1 = max1(a,b);
          printf("ret=%d\n",ret);
          printf("ret1=%d\n",ret1);
          return 0;
      }
      
      int max(int num1, int num2){
          int result;
          if(num1 > num2)
              result = num1;
          else
              result = num2;
          return result;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
    • 运行结果:
      ret=200
      ret1=200
      
      • 1
      • 2

    3、函数参数的两种传递方式

    • 形参在进入函数时被创建,退出函数时销毁

    1.传值调用(C语言默认传递参数方式)

    • 原理:把参数的实际值复制给函数的形式参数,因而,修改函数的形式参数不会影响实际参数
    • 代码实现:
      #include<stdio.h>
      
      void swap(int x, int y){
          int temp;
          temp = x;
          x = y;
          y = temp;
      }
      
      void main(){
          int a = 100;
          int b = 200;
          printf("交换前a=%d,b=%d\n",a,b);
          swap(a,b);
          printf("交换后a=%d,b=%d",a,b);
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
    • 运行结果:
      交换前a=100,b=200
      交换后a=100,b=200
      
      • 1
      • 2

    2.引用调用

    • 原理:形参为指向实参地址的指针,当对形参的指向操作是,就相当于对实参本身的操作
    • 代码实现:
      #include<stdio.h>
      
      void swap(int *x, int *y){//*x表示指向x的地址
          int temp;
          temp = *x; //将x的值赋值给temp
          *x = *y; //将y的值赋值给x
          *y = temp;
      }
      
      int main(){
          int a = 100;
          int b = 200;
          printf("交换前a=%d,b=%d\n",a,b);
          swap(&a,&b);//&a表示指向a的指针,即变量a的地址
          printf("交换后a=%d,b=%d",a,b);
          return 0;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
    • 运行结果:
      交换前a=100,b=200
      交换后a=200,b=100
      
      • 1
      • 2

    4、函数的分类

    1.内部函数(静态函数)

    • 特点:只能被本文件内的其他函数调用,即使在不同的文件中有相同的函数名,也互不影响
    • 格式:static int max(int a, int b)

    2.外部函数

    • 当一个文件引用另一个文件的函数时,需要先对该函数使用extern关键字进行声明,表明该函数为外部函数
    • 代码实现:
    1. test1.c
      #include<stdio.h>
      static void delete_string(char str[],char ch);//内部函数声明
      
      int main(){
          extern void enter(char str[]);//对外部函数的声明
          extern void print(char str[]);
      
          char c,str[100];//字符和字符串声明
          enter(str);
          printf("请输入要删除的字符:\n");
          scanf("%c",&c);
          delete_string(str,c);
          print(str);
          return 0;
      }
      
      static void delete_string(char str[],char ch){//内部函数定义
          int i,j;
          for(i=j=0;str[i] != '\0';i++)//遍历str,删除目标字符
              if(str[i] != ch)
                  str[j++] = str[i];
          str[j] = '\0';//在C语言中,字符串以\0结束
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
    2. test2.c
      #include<stdio.h>
      
      void enter(char str[100]){
          printf("请输入字符串:\n");
          fgets(str,100,stdin);
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    3. test3.c
      #include<stdio.h>
      
      void print(char str[]){
          printf("删除后的字符串:\n%s",str);
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
    • 运行结果:
      d102@d102-W65KJ1-KK1:/media/d102/EPAN/Desktop/c++_ubuntu/c++_01/workspace$ gcc test1.c test2.c test3.c -o test
      d102@d102-W65KJ1-KK1:/media/d102/EPAN/Desktop/c++_ubuntu/c++_01/workspace$ ./test
      请输入字符串:
      asdasdasd
      请输入要删除的字符:
      a
      删除后的字符串:
      sdsdsd
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
  • 相关阅读:
    java计算机毕业设计海城同泽中学图书仓库管理系统MyBatis+系统+LW文档+源码+调试部署
    企业快速构建可落地的IT服务管理体系的五大关键点
    【编译部署】使用Visual Studio编译Linux平台程序/动态库(远程连接)
    mysql如何复制一张表的数据到新的表
    【ACM】前言(1)
    wps/word中字体安装教程
    【三人一机】
    隐私计算 FATE - 多分类神经网络算法测试
    jenkins +miniprogram-ci 一键上传微信小程序
    Vue2:官方路由 Vue-Router 3.x
  • 原文地址:https://blog.csdn.net/qq_43280851/article/details/125545979