函数

作者: 羊妞麻麻 | 来源:发表于2017-02-08 16:02 被阅读13次

前序:
控制台输出结果:

the first experssion is true.

本节课导图:

函数.png

函数的引申:

int main(int argc, const char * argv[]) {
    // insert code here...

    printf("wyy has done as much as iOS as I could fit into 5 days\n");
    printf("Mike has done as much as C as I could fit into 6 days\n");
    printf("Jack has done as much as OC as I could fit into 15 days\n");
    printf("Lisa has done as much as Python as I could fit into 10 days\n");
    printf("Lucy has done as much as Cocoa as I could fit into 7 days\n");

    return 0;
}

可以看到,我们需要输入大量的文字代码,是不是很烦人?其中是否有很多重复性的语句?如果有,我们是否可以提取出来一个函数,来完成这个功能?答案是可以的。

void congratulateStudent (char *student, char *course, int numDays)
{
    printf("%s has done as much as %s as I could fit into %d days\n",student,course,numDays);
}

int main(int argc, const char * argv[]) {
    // insert code here...
    congratulateStudent("wyy", "iOS", 5);
    congratulateStudent("Lucy", "OC", 4);
    congratulateStudent("Jack", "Python", 7);

    return 0;
}

递归调用

void singSong (int num)
{
    if (num == 0) {
        printf("歌曲结束\n");
    }else{
        printf("%d个歌曲\n",num);
        int oneFewer = num - 1;
        singSong(oneFewer);//递归调用
        printf("%d歌曲正在播放\n",num);
    }
}

int main(int argc, const char * argv[]) {
    // insert code here...
    singSong(4);

    return 0;
}

控制台结果:

4个歌曲
3个歌曲
2个歌曲
1个歌曲
歌曲结束
1歌曲正在播放
2歌曲正在播放
3歌曲正在播放
4歌曲正在播放
Program ended with exit code: 0

返回函数值

float triangleArea(float a, float b,float h)
{
    float triangleArea = a * b * h;
    printf("triangleArea is %f.\n",triangleArea);
    return triangleArea;
}

int main(int argc, const char * argv[]) {
    // insert code here...

    triangleArea(2, 3, 4);
    
    return 0;
}

全局变量和静态变量

//float lastTemprature;//全局变量
static float lastTemprature;//静态变量

float fahrenheitFromCelsius(float cel)
{
    lastTemprature = cel;
    float fahr = cel *1.8+32.0;
    printf("%f Celsius is %f Fahrenheit\n",cel,fahr);

    return fahr;
}

int main(int argc, const char * argv[]) {
    // insert code here...

    float freezeInC = 0;
    float freezeInF = fahrenheitFromCelsius(freezeInC);
    printf("the lastTemprature %f \n",freezeInF);

    return EXIT_SUCCESS;
}

练习题:
已知三角形内角和一定是180度,试创建一个新项目,类型为基于C语言的command Line Tool名称为 Triangle。在main.c编写一个函数,参数是某两个内角的角度,返回值一个剩下的第三个内角的角度。

答案见下一章,写对者红包奖励哦。欢迎初学者多多参与。

相关文章

  • Excel(三)

    AND函数 OR函数 NOT函数 IF函数 频率分析函数FREQUENCY

  • if、else if、for、while、repeat函数

    ①if函数 ②else if函数 ③for函数 ④while函数 ⑤repeat函数

  • strsplit、mapply、paste、match函数

    strsplit函数 mapply函数 strsplit函数 mapply函数 paste函数 match函数 第...

  • Oracle中常用函数(SQL)

    Oracle函授有以下几个分类:数字函数、字符函数、日期函数、转换函数、集合函数、分析函数 数字函数: 字符函数:...

  • MySQL函数

    字符函数 数字运算函数 比较运算符和函数 日期时间函数 信息函数 聚合函数 加密函数 流程函数

  • BI-SQL丨AND & OR & IN

    AND函数 & OR函数 & IN函数 AND函数、OR函数和IN函数都可以理解是WHERE函数的补充,当然也可以...

  • Python之函数

    课程大纲 函数定义 函数的参数 函数的返回值 高阶函数 函数作用域 递归函数 匿名函数 内置函数 函数式编程 将函...

  • 函数基本知识

    函数 函数的定义: def 函数名() 函数的调用:函数名() #不能将函数调用放在函数定义上方 函数的文档注...

  • 积分表——不定期更新

    基本初等函数包括: 常函数: 幂函数 指数函数 对数函数 三角函数 反三角函数 I、反函数Ⅱ、复合函数:初等函数(...

  • MySQL基本使用

    函数 常用函数 数学函数 字符串函数 日期函数

网友评论

    本文标题:函数

    本文链接:https://www.haomeiwen.com/subject/xcgvittx.html