当前课程知识点:C Programming > Chapter 7 Modular Programming with Functions > 7.1 Function Concept and How to Define and Call Functions > 7.1 Function Concept and How to Define and Call Functions.mp4
大家好
我是云南大学信息学院丁海燕老师
欢迎走进C语言程序设计课堂
今天我们讲解函数概念
以及怎样定义和调用函数
经过前几章的学习
已经能够编写一些简单的C程序了
但是为解决以下问题
还需要引入函数的概念
存在的问题是
如果程序的功能比较多 规模比较大
把所有代码都写在main函数中
就会使主函数变得庞杂 头绪不清
阅读和维护变得困难
有时程序中要多次实现某一功能
就需要多次重复编写
实现此功能的程序代码
这使程序冗长 不精炼
解决的方法
用模块化程序设计的思路
采用组装的办法简化程序设计的过程
事先编好一批
实现各种不同功能的函数
把它们保存在函数库中
需要时直接用
函数就是功能
每一个函数用来实现一个特定的功能
函数的名字应反映其代表的功能
在设计一个较大的程序时
往往把它分为若干个程序模块
每一个模块包括一个或多个函数
每个函数实现一个特定的功能
C程序可由一个主函数
和若干个其他函数构成
主函数调用其他函数
其他函数也可以互相调用
同一个函数可以被一个
或多个函数调用任意多次
一个程序中函数调用的示意图
如图所示
main函数调用函数a b c
a函数调用函数d e
b函数调用函数f g h
f函数又调用函数e
c函数调用函数j i
j函数又调用函数g
除了可以使用库函数
还可以使用自己编写的函数
在程序设计中要善于利用函数
可以减少重复编写程序段的工作量
同时可以方便地实现
模块化的程序设计
下面来看例7.1
输出以下的结果 用函数调用实现
一行星
How do you do
一行星号
解题思路
在输出的文字上下分别有一行*号
显然不必重复写这段代码
用一个函数print_star
来实现输出一行*号的功能
再写一个print_message函数
来输出中间一行文字信息
用主函数分别调用这两个函数
程序如下
void print_star()
printf(“****************\ n”)
void print_message()
printf(“How do you do”)
输出一行文字
void print_star 和void print_message
都是函数的申明
print_star();和print_message(); 表示调用函数
以下是两个函数的定义
print_star()和print_message()
函数输出结果如图所示
说明如下
一
一个C程序由一个或多个程序模块组成
每一个程序模块作为一个源程序文件
对较大的程序
一般不希望把所有内容全放在一个文件中
而是将它们分别放在若干个源文件中
由若干个源程序文件组成一个C程序
这样便于分别编写 分别编译
提高调试效率
一个源程序文件可以为多个C程序共用
二
一个源程序文件由一个或多个函数
以及其他有关内容
如预处理指令
数据声明与定义等组成
一个源程序文件是一个编译单位
在程序编译时
是以源程序文件为单位进行编译的
而不是以函数为单位进行编译的
三
C程序的执行是从main函数开始的
如果在main函数中调用其他函数
在调用后流程返回到main函数
在main函数中结束整个程序的运行
四
所有函数都是平行的
即在定义函数时是分别进行的
是互相独立的
一个函数并不从属于另一个函数
即函数不能嵌套定义
函数间可以互相调用
但不能调用main函数
main函数是被操作系统调用的
五
从用户使用的角度看 函数有两种
库函数 它是由系统提供的
用户不必自己定义而直接使用它们
应该说明 不同的C语言编译系统
提供的库函数的数量和功能
会有一些不同
当然 许多基本的函数是共同的
另一种是用户自己定义的函数
它是用以解决用户专门需要的函数
六
从函数的形式看 函数分两类
一 无参函数
无参函数一般用来执行指定的一组操作
无参函数可以带回或不带回函数值
但一般以不带回函数值的居多
二 有参函数
在调用函数时
主调函数在调用被调用函数时
通过参数向被调用函数传递数据
一般情况下
执行被调用函数时会得到一个函数值
供主调函数使用
那么怎样定义一个函数呢
C语言要求
在程序中用到的所有函数
必须 先定义 后使用
需要指定函数名字
函数返回值类型
函数实现的功能
以及参数的个数与类型
将这些信息通知编译系统
定义函数应包括以下几个内容
指定函数的名字 以便以后按名调用
指定函数类型
即函数返回值的类型
指定函数参数的名字和类型
以便在调用函数时向它们传递数据
无参函数不需要指定这项
指定函数的功能
这是最重要的
这是在函数体中解决的
对于库函数
程序设计者只需用#include指令
把有关的头文件
包含到本文件模块中即可
程序设计者需要在程序中
自己定义想用的
而库函数并没有提供的函数
定义无参函数的一般形式为
函数体包括声明部分和语句部分
void表示空 即函数没有参数
在定义函数时要用类型标识符
即类型名 指定函数值的类型
即指定函数带回来的值的类型
二 定义有参函数
定义有参函数的一般形式为
例如factorial函数的功能
是计算k的阶乘
三 定义空函数
定义空函数的一般形式为
先用空函数占一个位置
以便逐步扩充
好处是程序结构清楚 可读性好
以后扩充新功能方便
对程序结构影响不大
定义函数的目的是为了调用函数
下面介绍如何调用函数
函数调用的一般形式为
如果是调用无参函数
则实参表列可以没有
但括号不能省略
如果实参表列包含多个实参
则各参数间用逗号隔开
按函数调用在程序中
出现的形式和位置来分
可以有以下3种函数调用方式
一 函数调用语句
把函数调用单独作为一个语句
如print_star();
这时不要求函数带回值
只要求函数完成一定的操作
二 函数表达式
函数调用出现在另一个表达式中
如c=max(a,b);
这时要求函数带回一个确定的值
以参加表达式的运算
三 函数参数
函数调用作为另一函数调用时的实参
如m=max(a,max(b,c));
其中max(b,c)是一次函数调用
它的值作为max另一次调用的实参
好了 同学们
函数概念以及怎样定义和调用函数
我们就学习到这儿
下节课再见
-1.1 The development and characteristics of C language
--1.The development and characteristics of C language .mp4
--1.1 Self test questions
-1.2 A simple C Language program
--1.2 A simple C Language program.mp4
--【Source program】 Example 1.1 output a line of text Hello, world! "
--【Source program】 Example 1.2 program composed of multiple functions, find the larger of two integers
--1.2 Self test questions
-1.3 Program, Programming Language and C Program Running Steps
--1.3 Program, Programming Language and C Program Running Steps.mp4
--Running steps of C source program in CodeBlocks
--1.3 Self test questions
-chapter 1 Self-Test
-2.1 The Concept and Description of Algorithms
--2.1 The Concept and Description of Algorithms.mp4
--2.1 Self test questions
-2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming
--2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming.mp4
--2.2 Self test questions
-Chapter 2 Self test questions
-3.1 Simple Structure and Identifier of C Language Program
--3.1 Simple Structure and Identifier of C Language Program.mp4
--【Source program】 Example 3.1 input two integers and output the sum of two numbers.
--【Source program】 Example 3.2 input two integers and output the average value.
--【Source program】 Example 3.3 output the value of the character variable.
--3.1 Self test questions
-3.2 Constants, Variables and Assignments
--3.2 Constants, Variables and Assignments.mp4
--3.2 Self test questions
-3.3 Arithmetic, assignment, increment and decrement operators
--3.3 Arithmetic, assignment, increment and decrement operators.mp4
--3.3 Self test questions
-3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data
--3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data
--【Source program】 example of sizeof
--3.4 Self test questions
-3.5 Input and Output Examples and Character Input and Output
--3.5 Input and Output Examples and Character Input and Output.mp4
--【Source program】 Example. Find the root of quadratic equation of one variable.
--【Source program】 Example 1. Successively output three characters of BOY.
--【Source program】Example 2. Input 3 characters of BOY from the keyboard and output them to the screen
--3.5 Self test questions
-3.6 Formatted output printf function
--3.6 Formatted output printf function.mp4
--【Source program】 Example 3.4 output of integer data.
--【Source program】 Example 3.5 real data output.
--【Source program】 Example 3.6 character data output.
--【Source program】 Example 3.7 uses %s to output a string.
--3.6 Self test questions
-3.7 Formatted input scanf function
--3.7 Formatted input scanf function.mp4
--【Source program】 Example 3.8 Input and output integer data
--【Source program】Example 3.9 Input and output single precision and double precision real data.
--【Source program】Example 3.10 input and output character data.
--【Source program】Example 3.11 input and output string.
--3.7 Self test questions
-3.8 Basic Data Types of C Language
--3.8 Basic Data Types of C Language.mp4
--3.8 Self test questions
-Chapter 3 Self test questions
-Operator and expression self test questions
-4.1 Relational operators, logical operators, and if statements
--4.1 Relational operators, logical operators, and if statements.mp4
--【source example】Example 4.1 Find the root of the quadratic equation of one variable.
--【Source program】Example 4.2 Input two real numbers and output them from small to large.
--4.1 Self test questions
-4.2 Switch Statement
--【Source program】Example 4.3 uses switch statement to implement simple menu program
--【Source program】Example 4.4 converts centesimal score into the corresponding grade system score.
--4.2 Self test questions
-4.3 Examples of Selection Structural Programs
--4.3 Examples of Selection Structural Programs.mp4
--【Source program】 Example 4.5 determines whether a year is a leap year.
--【Source program】 Example 4.6 find the solution of quadratic equation of one variable.
--【Source program】Example 4.7 calculate the transportation cost for the user.
--4.3 Self test questions
-Chapter 4 Self test questions
-5.1 While and Do-While Statement
--5.1 While and Do…while statement.mp4
--【Source program】example 5.1 Use while statement to find 1 + 2 + 3 + +100
--【Source program】example 5.2 using do while statement to find 1 + 2 + 3 + +100
--5.1 Self test questions
-5.2 For Statement
--【Source program】example 5.3 Use for loop to find 1 + 2 + 3 +100
--5.2 Self test questions
-5.3 Change the State of Loop Execution and Nested Loop
--5.3 Change the state of loop execution and nested loop .mp4
--【Source program】example 5.4 collecting charity donations
--【Source program】example 5.5 output a number between 100 and 200 that cannot be divided by 3.
--【Source program】multiplication table
--【Source program】example 5.6 Output the following 4*5 matrix.
--5.3 Self test questions
-5.4 Loop Structure Program example 1
--5.4 Loop structure program example 1.mp4
--5.4 Self test questions
-5.5 Loop Structure Program example 2
--5.5 Loop structure program example 2.mp4
--【Source program】example 5. Output the following figure.
--【Source program】example6. The problem of 100 chickens and 100 coins
--【Source program】example 7. Find the approximate value of PI
--5.5 Self test questions
-Chapter 5 Self test questions
-6.1 Definition, Reference and Initialization of One-Dimensional Arrays
--6.1 Definition, Reference of One-Dimensional Arrays.mp4
--6.1 Self test questions
-6.2 One-Dimensional Array Programming
--6.2 One-Dimensional Array Programming.mp4
--【Source program】example 1. Fibonacci sequence
--【Source program】example 2. Find the maximum value and the minimum value.
--【Source program】example 3 exchanging array elements in reverse order.
--【Source program】example 4. Bubble sorting.
--6.2 Self test questions
-6.3 Definition, Reference and Initialization of Two-Dimensional Arrays
--6.3 Definition, Reference of Two-Dimensional Arrays.mp4
--6.3 Self test questions
-6.4 Two-Dimensional Array Programming
--6.4 Two-Dimensional Array Programming.mp4
--【Source program】example 1. Get the average score of each subject.
--【Source program】Example 2. The elements of row and column of a 2D array are interchanged
--6.4 Self test questions
-6.5 Definition, initialization and input and output of character arrays
--6.5 Definition, initialization and input and output of character arrays .mp4
--【Source program】Example 6.6 Output a known string.
--【Source program】Example 6.7 Output a diamond.
--【Source program】Example 6.10 Sorting of strings.
--6.5 Self test questions
-6.6 String Processing Function
--6.6 String Processing Function.mp4
--6.6 Self test questions
-6.7 Character Array Programming
--6.7 Character Array Programming.mp4
--6.7 Self test questions
-Chapter 6 Self test questions
-7.1 Function Concept and How to Define and Call Functions
--7.1 Function Concept and How to Define and Call Functions.mp4
--7.1 Self test questions
-7.2 Data Transfer in Function Call, Call Procedure and Function Return Value
--7.2 Data Transfer in Function Call, Call Procedure and Function Return Value.mp4
--7.2 Self test questions
-7.3 Declarations of called functions and nested calls to functions
--7.3 Declarations of called functions and nested calls to functions .mp4
--【Source program】Example 7.4 use a function to find the sum of t
--7.3 Self test questions
-7.4 Recursive call to function
--7.4 Recursive call to function.mp4
--【Source program】Example 7.6. How old is the fifth student?
--【Source program】Example 7.7 Use recursion method to find n!
--7.4 Self test questions
-7.5 Array as function parameter (1)
--7.5 Array as function parameter (1).mp4
--【Source program】Example 7.10
--7.5 Self test questions
-7.6 Array as function parameter (2)
--7.6 Array as function parameter (2).mp4
--【Source program】Selection sorting.
--【Source program】Example 7.13 find the maximum of a 3×4 matrix.
--7.6 Self test questions
-7.7 Local and global variables, internal and external functions
--7.7 Local and global variables, internal and external functions.mp4
--【Source program】Example 7.14
--7.7 Self test questions
-7.8 The Survival Period of Variables and the Storage Mode of Local Variables
--7.8 The Survival Period of Variables and the Storage Mode of Local Variables.mp4
--【Source program】Example 7.17
--7.8 Self test questions
-7.9 Storage Categories of Global Variables
--7.9 Storage Categories of Global Variables.mp4
--7.9 Self test questions
-Chapter 7 Self test questions
-8.1 Pointer Concept, Definition and Reference of Pointer Variables
--8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4
--8.1 Self test questions
-8.2 Pointer variables as function parameters
--8.2 Pointer variables as function parameters.mp4
--【Source program】Example 8.3. Exchange two data with function call.
--8.2 Self test questions
-8.3 Pointer of array element, operation of pointer and reference of array element by pointer
--8.3 Pointer of array element, operation of pointer and reference of array element by pointer.mp4
--【Source program】Point to array elements with pointer variables.
--8.3 Self test questions
-8.4 Using Array Name as Function Parameter
--8.4 Using Array Name as Function Parameter.mp4
--【Source program】Store array elements in reverse order.
--8.4 Self test questions
-8.5 Reference to multidimensional arrays by pointers
--8.5 Reference to multidimensional arrays by pointers.mp4
--【Source program】Pointer variable pointing to one-dimensional array.
--8.5 Self test questions
-8.6 Referencing strings through pointers
--8.6 Referencing strings through pointers.mp4
--【Source program】Reference string by pointer.
--8.6 Self test questions
-8.7 Character pointer as function parameter
--8.7 Character pointer as function parameter.mp4
--【Source program】Character pointer as function parameter.
--8.7 Self test questions
-8.8 Pointer pointing to function
--8.8 Pointer pointing to function.mp4
--【Source program】Pointer variable pointing to the function.
--8.8 Self test questions
-8.9 Functions that return pointer values
--8.9 Functions that return pointer values.mp4
--【Source program】Intercept substring.
--8.9 Self test questions
-8.10 Pointer arrays and multiple pointers
--8.10 Pointer arrays and multiple pointers.mp4
--【Source program】Sorting of strings.
--8.10 Self test questions
-8.11 Dynamic memory allocation and pointer variables pointing to it
--8.11 Dynamic memory allocation and pointer variables pointing to it.mp4
--【Source program】Dynamic memory allocation.
--8.11 Self test questions
-Chapter 8 Self test questions
-9.1 Define and use structural variables
--9.1 Define and use structural variables.mp4
--【Source program】Structure variable
--9.1 Self test questions
-9.2 Using structure arrays
--9.2 Using structure arrays.mp4
--【Source program】Structure array
--9.2 Self test questions
-9.3 Structure pointer
--【Source program】Structure pointer
--9.3 Self test questions
-Chapter 9 Self test questions
-CodeBlocks Baidu online disk download address
-Final Exam
--final exam