黑马程序员——C语言基础---函数

来源:互联网 发布:心灵捕手影评 知乎 编辑:程序博客网 时间:2024/05/23 23:06

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

什么是函数

任何一个C语言程序都是由一个或者多个程序段(小程序)构成的,每个程序段都有自己的功能,我们一般称这些程序段为函数。所以,你可以说C语言程序是由函数构成的。


函数的定义

定义函数的目的

将一个常用的功能封装起来,方便以后调用


定义函数的步骤

函数名:函数叫什么名字

函数体:函数是干啥的,里面包含了什么代码


格式

固定格式(很多语言的函数都是这么写的)

返回值类型 函数名(形式参数列表

{

 函数体

}

举例

定义一个函数,计算两个整数的和

sum(int a, int b) 

{

int c = a + b;

}


函数调用

sum(10, 11); 引出返回值

说明函数的调用过程

简述return的作用


函数的参数

形参和实参的基本概念

形参个数和实参一致:sum(10, 11, 12)

参数的传递是值传递

参数名不能跟函数内的局部变量同名

函数可以没有参数:设计一个函数返回PI


函数的返回值

返回值的基本概念、return的作用

void

return

void可以省略return

可以多次使用return

return后面不能有其他语句

函数的弱语法

如果没有写返回值类型,默认是int

如果写了返回值,可以不返回

调用一个没有定义过的函数


定义函数的步骤

明确函数作用,起一个有意义的函数名称

明确函数的参数和返回值


示例:

求两个整数的差

打印一条横线

打印N条横线

/* 求两个整数的差 打印一条横线 打印N条横线  定义函数的步骤 1> 根据函数的作用,起一个有意义的名称 2> 确定函数的形参个数 3> 确定函数的返回值 */#include <stdio.h>void printLines(int n){    for (int i = 0; i<n; i++)    {        printf("-------------------\n");    }}void printLine(){    printf("-------------------\n");}int minus(int a, int b){    return a - b;}int main(){    printLines(10);    //printLine();    //printf("%d\n", minus(100, 29));        return 0;}

运行结果:


函数注意点

 1.默认情况下,不允许有函数的名称一样

 2.函数不能嵌套定义

 3.函数不能重复定义,但是可以重复声明

 4.如果有函数的声明,没有函数的定义

 1> 编译可以通过,因为编译器只会检测语法合不合理,并不会检测函数有没有定义

 2> 链接报错,因为链接的时候会检测函数是否定义



函数的补充

main函数

返回值:0,正常退出;1,异常退出

printf函数

#include

返回值:字符串的长度


示例:

编写一个函数int aver(int a, int b),计算ab的平均值

#include <stdio.h>int average(int num1, int num2){    return(num1 + num2)/2;}int main(){            int a = 10;        int b = 9;        int c = average(a,b);        printf("d is %d\n",c);       return 0;}

运行结果:



函数的声明

函数的定义顺序

函数的声明作用:声明和定义,类似于身份证和人的关系,编译买机票、链接登机

只有函数声明、没有定义,编译警告、链接报错


示例:

/* include 1> 作用:拷贝右边文件的所有内容到#include所在的位置 2> 自定义的文件用"",系统自带的文件用<> 3> #include <stdio.h>的目的:拷贝printf函数的声明 */#include <stdio.h>int main(){    printf("哈哈哈\n");    //#include "haha/abc.txt"#include "/Users/apple/Desktop/iOS课堂共享/0722课堂共享/0727/代码/include/haha/abc.txt"    return 0;}


.h文件和.c文件的分工

单文件的坏处

一个文件的内容太多,不利于阅读、调试

多人修改同一个文件出问题

公司里面是团队合作


sum函数抽取到另一个.c文件中

先直接调用sum函数,编译警告、链接main.c错误

#include “sum.c” 编译链接main.c,运行成功

如果avr.c中使用sum.c,编译链接main.c,运行程序


在其他文件中声明sum函数

int sum(int,int);  

编译链接main.c

编译链接sum.c

编译链接main.c  sum.c,运行成功

avr.c利用一样的方法


int sum(int,int)抽取到另外一个文件中

不抽取声明的坏处:增加新的函数了

抽取到.c文件中?开发工具会将所有的.c进行编译链接

抽取到.h文件中


抽取步骤总结

.c文件写函数的定义

.h文件写函数的声明

要想用我的函数,请包含我的.h文件


示例:

多文件开发

/*李四 编写基本算数运算的函数*/int average(int a, int b){    return (a + b)/2;}int sum(int a, int b){    return a + b;}

/* 李四 编写函数的声明 */int average(int a, int b);int sum(int a, int b);
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);">/*</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: 'Heiti SC Light'; color: rgb(0, 132, 0);">张三</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);"> <span style="font-family: 'Heiti SC Light';">编写</span>main<span style="font-family: 'Heiti SC Light';">函数</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0); min-height: 13px;"> </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: 'Heiti SC Light'; color: rgb(0, 132, 0);"><span style="font-family: Menlo;"> </span>链接:把项目中所有相关联的<span style="font-family: Menlo;">.o</span>目标文件、<span style="font-family: Menlo;">C</span>语言函数库合并在一起,生成可执行文件</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0); min-height: 13px;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);">*/</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(120, 73, 42);">#include <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b"><stdio.h></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(120, 73, 42);">#include <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"</span><span style="font-family: 'Heiti SC Light'; color: rgb(209, 47, 27);">李四</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">.h"</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">int</span> main()</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">int</span> score1 = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">100</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">int</span> score2 = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">70</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">int</span> c = average(score1, score2);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">int</span> d = sum(score1, score2);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">    printf(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"</span><span style="font-family: 'Heiti SC Light'; color: rgb(209, 47, 27);">平均分是</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">%d\n"</span>, c);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">    printf(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"</span><span style="font-family: 'Heiti SC Light'; color: rgb(209, 47, 27);">总分是</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">%d\n"</span>, d);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">0</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);">/*</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: 'Heiti SC Light'; color: rgb(0, 132, 0);"><span style="font-family: Menlo;">1.</span>函数的定义放<span style="font-family: Menlo;">.c</span>文件,函数的声明放<span style="font-family: Menlo;">.h</span>文件</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: 'Heiti SC Light'; color: rgb(0, 132, 0);"><span style="font-family: Menlo;">2.</span>如果要使用某个<span style="font-family: Menlo;">.c</span>文件中定义的函数,只需要<span style="font-family: Menlo;">#include</span>这个<span style="font-family: Menlo;">.c</span>文件对应的<span style="font-family: Menlo;">.h</span>文件</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: 'Heiti SC Light'; color: rgb(0, 132, 0);"><span style="font-family: Menlo;">3..h</span>文件的作用:被别人拷贝。编译链接的时候不需要管<span style="font-family: Menlo;">.h</span>文件</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);">4.cc xx.o xxx.o <span style="font-family: 'Heiti SC Light';">将多个目标文件链接在一起</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);">  cc xx.c xxx.c <span style="font-family: 'Heiti SC Light';">将多个源文件编译、链接</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);">*/</p>
运行结果:






0 0
原创粉丝点击