函数(一)
什么是函数呢?
函数:函数就是程序中独立的功能
📚需求:用输出语句描述,如何打两局游戏?
-
思路:
- 第一种方法:
- 第二种方法:使用循环的方式进行定义 这样就导致一个问题,所展现的游戏信息将会成为连续的。
- 第三种方法:使用函数
使用情况:反复书写的代码,又不确定什么时候会用到的代码打包起来。
C 语言中有许多定义好的函数。
8.1 函数的定义和调用
返回值类型 函数名(形参1,形参2…)
{
函数体;
return 返回值;
}
案例1:
- 需求:用输出的语句描述,如何打一局游戏
#include <stdio.h>
void playGame()
{
printf("选择人物\n");
printf("准备开局\n");
printf("开始对线\n");
printf("碾压崩盘\n");
printf("问候队友\n");
printf("疯送人头\n");
printf("下把继续\n");
}
int main()
{
playGame();
printf("--------------------\n");
playGame();
return 0;
}
练习
1:
- 定义一个函数,用来描述你吃饭的过程
#include <stdio.h>
// 定义描述吃饭过程的函数
void eatDinner()
{
printf("1. 拿起筷子\n");
printf("2. 从碗里夹取适量食物\n");
printf("3. 将食物送至嘴边\n");
printf("4. 细嚼慢咽,享受美食\n");
printf("5. 咽下食物\n");
printf("6. 不断重复步骤2-5,直到吃饱为止\n");
printf("7. 放下筷子,擦擦嘴巴,表示用餐完毕\n");
}
int main()
{
printf("开始吃饭...\n");
eatDinner();
printf("饭后休息一下...\n");
return 0;
}
练习
2:
- 定义一个函数,求10-20的和
#include <stdio.h>
// 定义求和函数
int sumInRange(int start, int end)
{
int total = 0;
for (int i = start; i <= end; i++)
{
total += i;
}
return total;
}
int main()
{
// 调用函数并输出结果
int result = sumInRange(10, 20);
printf("10到20的和是:%d\n", result);
return 0;
}
8.2 带有参数的函数
需求
2:
- 定义一个函数,求10-20的和
❓ 如果要求不同数值之间的和,
我们如何解决这类问题呢
可能出现语义上的冲突,因此C语言当中对这种不同的参数有不同的定义方法。
- 在C语言中,您可以定义一个带参数的函数来计算两个数的和。下面是一个简单的函数定义示例:
#include <stdio.h>
// 定义求和函数,接收两个整数作为参数,返回它们的和
int addTwoNumbers(int num1, int num2)
{
// 计算两个数的和
int sum = num1 + num2;
// 返回求得的和
return sum;
}
int main()
{
// 声明两个整数变量并初始化
int a = 10;
int b = 20;
// 调用函数计算两数之和
int result = addTwoNumbers(a, b);
// 输出结果
printf("The sum of %d and %d is: %d\n", a, b, result);
return 0;
}
在此代码中,addTwoNumbers函数接收两个整数参数num1和num2,计算它们的和,并通过return语句将结果返回给调用者。在main函数中,我们实例化了两个整数变量a和b,并将它们作为参数传递给addTwoNumbers函数,获取并输出两数之和。
练习
3:
- 需求:小慧慧考试成绩:基础题得分93,附加题得分10。
- 小丹丹考试成绩:基础题得分87,附加题得分9。
- 请问,谁的总分更高?
#include <stdio.h>
// 定义计算总分的函数
int calculateTotalScore(int basicScore, int extraScore)
{
return basicScore + extraScore;
}
int main()
{
// 小慧慧的成绩
int xiaohuihui_basic = 93;
int xiaohuihui_extra = 10;
int xiaohuihui_total = calculateTotalScore(xiaohuihui_basic, xiaohuihui_extra);
// 小丹丹的成绩
int xiaodandan_basic = 87;
int xiaodandan_extra = 9;
int xiaodandan_total = calculateTotalScore(xiaodandan_basic, xiaodandan_extra);
// 比较总分
if (xiaohuihui_total > xiaodandan_total) {
printf("小慧慧的总分更高,她的总分为:%d\n", xiaohuihui_total);
} else if (xiaohuihui_total < xiaodandan_total) {
printf("小丹丹的总分更高,她的总分为:%d\n", xiaodandan_total);
} else {
printf("小慧慧和小丹丹的总分相同,都是:%d\n", xiaohuihui_total);
}
return 0;
}
8.3 定义函数的终极绝招
三个问题❓
-
1.我定义函数是为了做什么事情? 函数体
-
2.我干这件事,需要什么才能完成? 形参
-
3.我干完了,调用处是否需要继续使用?返回值类型
- 需要继续使用 必须写
- 不需要返回
void
案例
2
- 需求:给定两个长方形,判断谁的面积更大?如何定义函数?
- 1.我定义函数,求长方形的面积
- 2.需要什么才能完成?长/宽
- 3.是否要继续使用?要需要判断
#include <stdio.h>
// 定义计算长方形面积的函数
double calculateRectangleArea(double length, double width)
{
return length * width;
}
// 定义比较两个长方形面积的函数
int compareRectangleAreas(double area1, double area2)
{
if (area1 > area2)
return 1; // 第一个长方形面积更大
else if (area1 < area2)
return 2; // 第二个长方形面积更大
else
return 0; // 两个长方形面积相等
}
int main()
{
// 假设有两个长方形
double rectangle1_length = 5.0;
double rectangle1_width = 4.0;
double rectangle2_length = 3.0;
double rectangle2_width = 6.0;
// 计算面积
double area1 = calculateRectangleArea(rectangle1_length, rectangle1_width);
double area2 = calculateRectangleArea(rectangle2_length, rectangle2_width);
// 比较面积
int result = compareRectangleAreas(area1, area2);
switch (result)
{
case 1:
printf("第一个长方形的面积更大。\n");
break;
case 2:
printf("第二个长方形的面积更大。\n");
break;
case 0:
printf("两个长方形的面积相等。\n");
break;
}
return 0;
}
首先定义了一个计算长方形面积的函数calculateRectangleArea,接着定义了一个用于比较面积的函数compareRectangleAreas。在main函数中,我们创建了两个长方形实例并计算它们的面积,最后通过调用compareRectangleAreas函数来判断哪个长方形的面积更大。
练习
4:
#include <stdio.h>
#include <math.h> // 引入数学库,以便使用π和平方根函数sqrt()
// 定义计算圆面积的函数
double calculateCircleArea(double radius)
{
const double PI = 3.14159265358979323846;
return PI * pow(radius, 2); // 使用半径的平方计算面积
}
// 定义比较两个圆面积的函数
int compareCircleAreas(double area1, double area2)
{
if (area1 > area2)
return 1; // 第一个圆的面积更大
else if (area1 < area2)
return 2; // 第二个圆的面积更大
else
return 0; // 两个圆的面积相等
}
int main()
{
// 假设有两个圆,半径分别为 r1 和 r2
double radius1 = 5.0;
double radius2 = 3.0;
// 计算面积
double area1 = calculateCircleArea(radius1);
double area2 = calculateCircleArea(radius2);
// 比较面积
int result = compareCircleAreas(area1, area2);
switch (result)
{
case 1:
printf("第一个圆的面积更大。\n");
break;
case 2:
printf("第二个圆的面积更大。\n");
break;
case 0:
printf("两个圆的面积相等。\n");
break;
}
return 0;
}