• C算法--函数


     1 #include <stdio.h>
     2 
     3 int max_2(int a,int b){
     4     if(a>b) return a;
     5     else return b;
     6 }
     7 
     8 /*在max_3中调用max_2比较大小*/
     9 int max_3(int a,int b,int c){
    10     int temp=max_2(a,b);
    11     temp=max_2(temp,c);
    12     return temp;
    13 }
    14 
    15 int main(){
    16     int a,b,c;
    17     scanf("%d%d%d",&a,&b,&c);
    18     printf("%d
    ",max_3(a,b,c));
    19     return 0;
    20 }
    嵌套调用
     1 #include <stdio.h>
     2 
     3 int max_2(int a,int b){
     4     if(a>b) return a;
     5     else return b;
     6 }
     7 
     8 /*在max_3中调用max_2比较大小*/
     9 int max_3(int a,int b,int c){
    10     int temp=max_2(a,b);
    11     temp=max_2(temp,c);
    12     return temp;
    13 }
    14 
    15 int main(){
    16     int a,b,c;
    17     scanf("%d%d%d",&a,&b,&c);
    18     printf("%d
    ",max_3(a,b,c));
    19     return 0;
    20 }
    嵌套调用
     1 #include <stdio.h>
     2 
     3 void print1(){
     4     printf("HAHA,
    ");
     5     printf("goodidea,
    ");
     6 }
     7 
     8 void print2(){
     9     printf("oho,
    ");
    10     printf("badidea!");
    11 }
    12 
    13 int main(){
    14     print1();
    15     print2();
    16     return 0;
    17 }
    基本语法格式

    返回类型时void 自定义函数只是单纯实现一些语句不返回变量

     1 #include <stdio.h>
     2 
     3 
     4 /*judge函数为有参函数 返回类型int型return后面的数据类型要和一开始给出的返回类型相同*/
     5 int judge(int x){
     6     if(x>0)return 1;
     7     else if(x==0) return 0;
     8     else return -1;
     9 }
    10 int main(){
    11     int a,ans;
    12     scanf("%d",&a);
    13     ans=judge(a);
    14     printf("%d
    ",ans);
    15     return 0;
    16 }
    有参函数
     1 #include <stdio.h>
     2 
     3 
     4 int x;
     5 void change(){
     6     x=x+1;
     7 } 
     8 int main(){
     9     x=10;
    10     change();
    11     printf("%d
    ",x);
    12     return 0;
    13 }
    全局变量
     1 #include <stdio.h>
     2 
     3 void change (int x){
     4     x=x+1;
     5 }
     6 
     7 /*下面change() 函数的参数X和main函数的a其实作用于两个不同函数的不同变量。这种传递参数的方法称为值传递*/
     8 /*函数定义的小括号参数成为形式参数,实际调用的成为实参*/ 
     9 int main(){
    10     int a=10;
    11     change(a);
    12     printf("%d
    ",a);
    13     return 0;
    14     
    15 }
    局部变量实参/形参
     1 #include <stdio.h>
     2 
     3 int MAX(int a,int b, int c){
     4     int M;
     5     if(a>=b&&a>=c)M=a;
     6     else if(b>=a&&b>=c)M=b;
     7     else M=c;
     8     return M; 
     9 } 
    10 
    11 int main(){
    12     int a,b,c;
    13     scanf("%d%d%d",&a,&b,&c);
    14     printf("%d
    ",MAX(a,b,c));
    15     return 0;
    16 }
    多个参数逗号隔开

    在main函数中 return0的意义在于告知系统程序正常终止。

     1 #include <stdio.h>
     2 
     3 void change(int a[],int b[][5]){
     4     a[0]=1;
     5     a[1]=3;
     6     a[2]=5;
     7     b[0][0]=1;
     8 }
     9 
    10 int main(){
    11     int a[3]={0};
    12     int b[5][5]={0};
    13     change (a,b);
    14     int i;
    15     for(i=0;i<3;i++){
    16         printf("%d
    ",a[i]);
    17     }
    18     return 0;
    19 }
    将数组作为参数传入返回数组
     1 #include <stdio.h>
     2 
     3 int max_2(int a,int b){
     4     if(a>b) return a;
     5     else return b;
     6 }
     7 
     8 /*在max_3中调用max_2比较大小*/
     9 int max_3(int a,int b,int c){
    10     int temp=max_2(a,b);
    11     temp=max_2(temp,c);
    12     return temp;
    13 }
    14 
    15 int main(){
    16     int a,b,c;
    17     scanf("%d%d%d",&a,&b,&c);
    18     printf("%d
    ",max_3(a,b,c));
    19     return 0;
    20 }
    函数的嵌套调用
     1 #include <stdio.h>
     2 
     3 /*函数的递归调用*/
     4 int F(int n){
     5     if(n==0) return 1;
     6     else return F(n-1)*n;
     7 } 
     8 
     9 int main(){
    10     int n;
    11     scanf("%d",&n);
    12     printf("%d
    ",F(n));
    13     return 0;
    14 }
    递归调用求阶乘
  • 相关阅读:
    ADO.NET中容易混淆的概念(4)
    ADO.NET中容易混淆的概念(3)
    ADO.NET中容易混淆的概念(2)
    ADO.NET中容易混淆的概念(1)
    引用计数
    ADO.NET中SQL Server数据库连接池
    Python之禅!
    django总结
    Python第六周学习之Linux
    Python第五周前端学习之HTML5/ CSS / JS
  • 原文地址:https://www.cnblogs.com/Catherinezhilin/p/11136174.html
Copyright © 2020-2023  润新知