当前位置:网站首页>函数(第一部分)
函数(第一部分)
2022-04-23 15:08:00 【星辰大海征途中的我们】
函数(第一部分)
一、函数是什么?
维基百科中对函数的定义:子程序
一个大型程序中的某部分代码, 由一个或多个语句块组成。它负责完成某项特定任务,而且相较于其他代 码,具备相对的独立性。
二、函数分类
(一)库函数
分类:
. IO函数
**. **字符串操作函数
. 字符操作函数
**. **内存操作函数
**. **时间/日期函数
**. **数学函数
**. **其他库函数
举例1:strcpy函数:
//这是一个可以拷贝的函数,如把arr值拷贝到arr1中。
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[] = {
0 };
char arr2[] = "ABC";
strcpy(arr1, arr2);
printf("%s", arr1);
return 0;
}

举例2: memset 函数:

其意思为,把“void* ptr”后“size-t num”个数字改编成“int main”形式或内容。
#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "hello world!";
memset(arr, '@', 5); //这里还可以写成“arr+一个数字”任意改写不同位置的字符。
printf("%s", arr//意思是将hello这5个字符改为’@‘。
return 0;
}

**注:**但是库函数必须知道的一个秘密就是:使用库函数,必须包含 #include 对应的头文件。
(二)自定义函数
自定义函数和库函数一样,有函数名,返回值类型和函数参数。但是不一样的是这些都是我们自己来设计。这给程序员一个很大的发挥空间。
函数的组成:
//如一个加法函数
int Add(int x,int y) int是返回类型, Add为函数名, 括号内为参数。
{
int z = x+y;
return z; //这些是语句项。
}
举例:写一个函数,求两者最大值:
#include<stdio.h>
int get_max(int x, int y) //这里返回什么就用什么类型,当不需要返回时,用void也行。
{
return (x > y) ? x : y;
}
int main()
{
int a = 0;
int b = 0;
scanf("%d%d", &a, &b);
int A = get_max(a, b);
printf("%d", A);
return 0;
}

举例2:写一个交换两个数的函数:
#include <stdio.h>
void exchange(int x, int y)
{
int z = 0;
z = x;
x = y;
y = z;
}
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
exchange(a, b);
printf("%d %d", a, b);
return 0;
}

这样无法交换两个数字的值。
当换成指针变量时:
#include <stdio.h>
void exchange(int* x, int* y)
{
int z = 0;
z = *x;
*x = *y;
*y = z;
}
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
exchange(&a, &b);
printf("%d %d", a, b);
return 0;
}

之所以会出像这样的情况,是由于函数中的形参与实参的影响。形参只是实参的一份临时拷贝,形参的改变不会影响实参,因此,第一种写法无法实现两个数的交换。
三、函数的形参与实参
【1】实际参数(实参)
真实传给函数的参数,叫做实参。
实参可以是:常量、变量、表达式、函数等。
无论实参是何种类型的量,在进行函数调用时,它们都必须有确定的值,以便把这些值传送给形参。
【2】形式参数(形参)
形式参数是指函数名后括号中的变量,因为形式参数只有在函数被调用的过程中才实例化(分配内存单元),所以叫形式参数。形式参数当函数调用完成之后就自动销毁了。因此形式参数只在函数中有效。
四、函数的调用:
【1】传值调用
函数的形参和实参分别占有不同内存块,对形参的修改不会影响实参。
【2】传址调用
传址调用是把函数外部创建变量的内存地址传递给函数参数的一种调用函数的方式。这种传参方式可以让函数和外边的变量建立起真正的联系,也就是函数内部可以直接操作函数外部的变量。
因此,要使得两数交换成功,应该使用传址调用,使用指针变量,让在函数内部调换的结果传给原代码实现两数值的交换。
版权声明
本文为[星辰大海征途中的我们]所创,转载请带上原文链接,感谢
https://blog.csdn.net/cr2606336607/article/details/124349180
边栏推荐
- Practice of unified storage technology of oppo data Lake
- Have you learned the basic operation of circular queue?
- Share 3 tools, edit 5 works at home and earn more than 400
- Pnpm installation and use
- Tun equipment principle
- [NLP] HMM hidden Markov + Viterbi word segmentation
- 大文件如何快速上传?
- Progress in the treatment of depression
- PSYNC synchronization of redis source code analysis
- Adobe Illustrator menu in Chinese and English
猜你喜欢

8.4 realization of recurrent neural network from zero

Tencent has written a few words, Ali has written them all for a month

nuxt项目:全局获取process.env信息

Leetcode exercise - 396 Rotation function

Leetcode149 - maximum number of points on a line - Math - hash table

Five data types of redis

About UDP receiving ICMP port unreachable

What is the role of the full connection layer?

The win10 taskbar notification area icon is missing

asp. Net method of sending mail using mailmessage
随机推荐
Thinkphp5 + data large screen display effect
1990年1月1日是星期一,定义函数date_to_week(year,month,day),实现功能输入年月日后返回星期几,例如date_to_week(2020,11,1),返回:星期日。 提示:
A series of problems about the best time to buy and sell stocks
UML学习_day2
The life cycle of key value in redis module programming
Swift protocol Association object resource name management multithreading GCD delay once
【thymeleaf】处理空值和使用安全操作符
Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
C语言超全学习路线(收藏让你少走弯路)
nuxt项目:全局获取process.env信息
UML learning_ Day2
Difference between like and regexp
8.3 language model and data set
How to design a good API interface?
Async void caused the program to crash
OPPO数据湖统一存储技术实践
Daily question - leetcode396 - rotation function - recursion
Nuxt project: Global get process Env information
January 1, 1990 is Monday. Define the function date_ to_ Week (year, month, day), which realizes the function of returning the day of the week after inputting the year, month and day, such as date_ to
The win10 taskbar notification area icon is missing