0
点赞
收藏
分享

微信扫一扫

室友只用了一把王者的时间就入门了「函数」

🌟 前言

大家好,我是Edison😎

在这里插入图片描述

今天是我们C语言系列的第三篇「函数」;

「函数」在C语言中占据着重要的位置,是C语言中的主体和核心,所以它的重要性也就不言而喻了。

Let’s get it!


在这里插入图片描述


文章目录


本章目标

在这里插入图片描述

1、函数是什么?

2、C语言中函数的分类

🍑2.1 库函数

为什么会有库函数?

那怎么学习库函数呢?

简单的总结,C语言常用的库函数都有👇

那我们来参照文档,学习两个库函数

strcpy函数

📝代码示例

#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[] = "xxxxxxxxx";//目标空间

	char arr2[] = "edison";

	strcpy(arr1, arr2);//将arr2数组的字符串拷贝到arr1数组中

	printf("%s\n", arr1);

	return 0;
}

运行结果👇

memset函数

📝代码示例

#include <stdio.h>
#include <string.h>

int main()
{
	char arr[] = "hello Edison";

	int n = 5;

	//把arr1的内容替换成xxxxx Ediosn
	char *ret = (char*)memset(arr, 'x', n);

	printf("%s\n", ret);

	return 0;
}

运行结果👇

以上就是我们参照文档,学的两个库函数

🍑2.2 自定义函数

自定义函数的定义语法为👇

//自定义函数
ret_type fun_name(para1, *)
{
	statement;//语句项
	
    return 返回值;//该返回值与返回类型必须相同,如果是void型函数,则不需要返回值,因此可以不写。
}

ret_type 返回类型
fun_name 函数名
para1    函数参数

这里直接看个例题

📝代码示例

#include <stdio.h>

int get_max(int x, int y)
{
	return (x > y) ? (x) : (y); //三目操作符,在操作符中讲过,如果x>y则返回x,反之则返回y;
}

int main()
{
	int a = 20;
	int b = 10;

	int max = get_max(a, b);//定义一个max变量,用来接受最大值;

	printf("max=%d\n", max);
}

运行结果👇

再来看一道例题

📝代码示例

#include <stdio.h>

void Swap(int* pa, int* pb) //我们只需要交换a和b当中的内容,不需要返回值,所以用void
{
	int t = 0;//定义一个临时变量用于交换
	t = *pa;
	*pa = *pb;
	*pb = t;
}

int main()
{
	int a = 10;
	int b = 20;

	printf("交换前: a=%d b=%d\n", a, b);

	Swap(&a, &b);

	printf("交换前: a=%d b=%d\n", a, b);

	return 0;
}

运行结果👇

3、函数的参数

函数的参数分为:

🍑3.1 实际参数

🍑3.2 形式参数

📝代码示例

#include <stdio.h>

void Swap1(int x, int y)
{
	int tmp = 0;
	tmp = x;
	x = y;
	y = tmp;
}

void Swap2(int* px, int* py) //我们只需要交换a和b当中的内容,不需要返回值,所以用void
{
	int tmp = 0;
	tmp = *px;
	*px = *py;
	*py = tmp;
}

int main()
{
	int a = 1;
	int b = 2;

	Swap1(a, b);
	printf("Swap1: a=%d b=%d\n", a, b);

	Swap2(&a, &b);
	printf("Swap2: a=%d b=%d\n", a, b);

	return 0;
}

运行结果👇

4、函数的调用

函数的调用分为:

🍑4.1 传值调用

📝代码示例

#include <stdio.h>

void Swap1(int x, int y)
{
	int tmp = 0;
	tmp = x;
	x = y;
	y = tmp;
}

int main()
{
	int a = 10;
	int b = 20;

	Swap1(a, b);
	printf("Swap1: a=%d b=%d\n", a, b);
	              
	return 0;
}

运行结果👇

🍑4.2 传址调用

📝代码示例

#include <stdio.h>

void Swap2(int* px, int* py)
{
	int tmp = 0;
	tmp = *px;
	*px = *py;
	*py = tmp;
}

int main()
{
	int a = 1;
	int b = 2;

	Swap2(&a, &b);
	printf("Swap2: a=%d b=%d\n", a, b);

	return 0;
}

运行结果👇

传值和传址的使用场景

5、函数的嵌套调用和链式访问

🍑5.1 嵌套调用

📝代码示例

#include <stdio.h>

void fun2()
{
	printf("hello\n");
}

void fun1()
{
	int i = 0;
	for (i = 0; i < 3; i++)
	{
		fun2();
	}
}

int main()
{
	fun1();
	return 0;
}

运行结果👇

🍑5.2 链式访问

📝代码示例

int main()
{

	int len = strlen("abc");

	printf("%d\n", len);

	printf("%d\n", strlen("abc"));//链式访问
}

运行结果👇

下面我们看一个有趣的代码🎃

#include <stdio.h>

int main()
{
    printf("%d", printf("%d", printf("%d", 43))); //结果是啥?
    
    //注:printf函数的返回值是打印在屏幕上字符的个数
    return 0;
}

运行结果👇

6、函数的定义和声明

🍑6.1 函数的声明

🍑6.2 函数的定义

这里我们可以简单的说一下

add.h

放置函数的声明

#ifndef __TEST_H__
#define __TEST_H__

//函数的声明
int Add(int x, int y);

add.c

放置函数的实现

#include "test.h"

//函数Add的实现
int Add(int x, int y)
{
	return x + y;
}

test.c

main函数里面去调用这个函数,要使用之前,只需要包括头文件:#include <add.h>即可;

#include <stdio.h>
#include "add.h"

int main()
{
	int a = 10;
	int b = 20;
	
	int ret = Add(a, b);
	
	printf("%d\n", ret);
	return 0;
}

7、函数递归

🍑7.1 什么是递归?

🍑7.2 递归的条件

这里我们通过例题来详解函数的递归

📃练习一

🧐思路

📝代码示例

#include <stdio.h>

void print(int n)
{
	if (n > 9)
	{
		print(n / 10);
	}
	printf("%d ", n % 10);
}

int main()
{
	int num = 1234;
	print(num);

	return 0;
}

运行结果👇

📃练习二

这道题就交给你们😘

🍑7.3 递归与迭代

📃练习三

🧐思路

📝代码示例

#include <stdio.h>

int Fib(int n)
{
 	if (n <= 2)
		return 1;
	else
		return Fib(n - 1) + Fib(n - 2);
}

int main()
{
	int n = 0;
	scanf("%d", &n);

	int ret = Fib(n);
	printf("%d\n", ret);

	return 0;
}

运行结果👇

但是我们发现有问题

那我们如何改进呢?

📝代码改进

#include <stdio.h>

int Fib(int n)
{
	int a = 1;
	int b = 1;
	int c = 1;

	while (n > 2)
	{
		a = b;
		b = c;
		c = a + b;
		n--;
	}
	return c;
}

int main()
{
	int n = 0;
	scanf("%d", &n);

	int ret = Fib(n);
	printf("%d\n", ret);

	return 0;
}

运行结果👇

什么时候用递归呢?

提示:

🌟总结

举报

相关推荐

0 条评论