函数的定义
函数是一个完成特定功能的代码模块,其程序代码独立,通常要求有返回值,也可以是空值。
一般形式如下:
<数据类型> <函数名称>( <形式参数说明> ) {
语句序列;
return[(<表达式>)];
}
- <数据类型>:是整个函数的返回值类型;
- return[(<表达式>)]:语句中表达式的值,要和函数的<数据类型>保持一致,如无返回值应该写为void型;
- <形式参数说明>:是逗号","分隔的多个变量的说明形式;
- 大括弧对 {<语句序列> }:称为函数体;
- <语句序列>:是大于等于零个语句构成的。
例子:
int fun(int n){
n++;
return n;
}
函数的使用
函数的使用也叫函数的调用,形式如下:
函数名称(〈实际参数〉)
实参就是在使用函数时,调用函数传递给被调用函数的数据。需要确切的数据
函数调用可以作为一个运算量出现在表达式中,也可以单独形成一个语句。对于无返回值的函数来讲,只能形成一个函数调用语句。
例子:
#include <stdio.h>
int fun(int n);
int main()
{
printf("%d", fun(5));
return 0;
}
int fun(int n)
{
if (n == 1 || n == 0)
return 1;
return n * fun(n - 1);
}
函数的参数传递
函数之间的参数传递方式:
- 全局变量;
- 复制传递方式;
- 地址传递方式。
全局变量:
全局变量就是在函数体外说明的变量,它们在程序中的每个函数里都是可见的
全局变量一经定义后就会在程序的任何地方可见。函数调用的位置不同,程序的执行结果可能会受到影响。不建议使用。
复制传递方式(值传递):
调用函数将实参传递给被调用函数,被调用函数将创建同类型的形参并用实参初始化;
形参是新开辟的存储空间,因此,在函数中改变形参的值,不会影响到实参。
地址传递方式(引用传递):
按地址传递,实参为变量的地址,而形参为同类型的指针;
被调用函数中对形参的操作,将直接改变实参的值(被调用函数对指针的目标操作,相当于对实参本身的操作)。
#include <stdio.h>
void swap1(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
void swap2(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int main()
{
int a = 1, b = 2;
// 值传递
// swap1(a, b);
// 引用传递
swap2(&a, &b);
printf("a = %d, b = %d\r\n", a, b); // 输出 "a = 1, b = 2"
}
函数的传参-数组
全局数组传递方式
复制传递方式:
实参为数组的指针,形参为数组名(本质是一个指针变量)。
地址传递方式:
实参为数组的指针,形参为同类型的指针变量。
#include <stdio.h>
int array_sum(int data[], int n);
int main(int argc, char *argv[])
{
int a[] = {5, 9, 10, 3, 10};
int sum = 0;
sum = array_sum(a, sizeof(a) / sizeof(int));
printf("sum=%d\n", sum);
return 0;
}
int array_sum(int data[], int n) // int data[] = a;error int * data = a;
{ // int n = sizeof(a)/sizeof(int);
int ret = 0;
int i;
for (i = 0; i < n; i++)
{
printf("%d\n", data[i]);
ret += data[i];
}
return ret;
}
例子:编写一个函数,统计字符串中小写字母的个数,并把字符串中的小写字母转化成大写字母。
#include <stdio.h>
#include <string.h>
int fun(char *str);
int main()
{
char str[] = "Hello World";//不能使用*str
int count = fun(str);
printf("%d,%s\r\n", count, str);//8,HELLO WORLD
return 0;
}
int fun(char *str)
{
int count = 0;
while (*str)
{
if ((*str >= 'a') && (*str <= 'z'))
{
*str = (char)((unsigned int)*str - ' ');
count++;
}
str++;
}
return count;
}
例子:将英语句子倒序。
#include <stdio.h>
#define N 32
void swap(char *head, char *tail);
int main(int argc, char *argv[])
{
char buff[N] = "I love China";
char *head = buff;
char *tail = buff;
while (*tail != '\0')
tail++;
swap(buff, tail - 1);
while (*head != '\0')
{
while (*head == N)
head++;
tail = head;
while (*tail != N && '\0' != *tail)
tail++;
swap(head, tail - 1);
head = tail;
}
puts(buff);//China love I
return 0;
}
void swap(char *head, char *tail)
{
while (head < tail)
{
*head ^= *tail;
*tail ^= *head;
*head++ ^= *tail--;
}
}
指针函数
指针函数:指一个函数的返回值为地址量的函数
指针函数的定义的一般形式如下
<数据类型> * <函数名称>(<参数说明>) {
语句序列;
}
返回值:全局变量的地址/static变量的地址/字符串常量的地址/堆的地址
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
char str[20] = "Hello World";//正确,全局变量的地址
char* fun() {
return str;
}
int main() {
printf("%s", fun());
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
char* fun() {
//char str[20] = "Hello World";//错误写法,不能返回局部变量,加上static正确
static char str[20] = "Hello World";//正确,static变量的地址
//char* str = "Hello World";//正确,字符串常量的地址
return str;
}
int main() {
printf("%s", fun());//错误返回,烫烫烫烫烫烫烫烫8鲔y?
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
char* fun() {
char* str = "Hello World";//正确,字符串常量的地址
return str;
}
int main() {
printf("%s", fun());
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* fun(void) {
char* str = NULL;
str = (char*)malloc(20 * sizeof(char));
strcpy(str, "Hello World");
return str;//返回堆地址
}
int main() {
char* p = NULL;
p = fun();
printf("%s\r\n", p);
if (p != NULL)
{
free(p);
p = NULL;
}
return 0;
}
例子:清空字符串空格
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
char* del_space(char* s);
int main() {
char* r;
char str[] = " Hello Wor ld";
printf("%s\r\n", str);
r = del_space(str);
printf("============\r\n");
puts(str);
return 0;
}
char* del_space(char* s) {
char* r = s;
char* p = s;
while (*s) {
if (*s == ' ') {
s++;
}
else {
*p = *s;
s++;
p++;
}
}
*p = '\0';
return r;
}
函数指针
定义方式:
函数返回值类型 (* 指针变量名) (函数参数列表);
函数指针调用:
#include <stdio.h>
//方式一
int fun(int x);
int (*p)(int x);
int main() {
p = fun(5);
printf("%d\r\n", *p);
return 0;
}
int fun(int x) {
return x;
}
#include <stdio.h>
//方式二
int fun(int x);
int main() {
int(*p)(int);
p = fun;
int y;
y = (*p)(5);
printf("%d\r\n", y);
return 0;
}
int fun(int x) {
return x;
}
#include <stdio.h>
//方式三
int fun(int x);
int main() {
int(*p)(int);
p = &fun;
int y;
y = p(5);
printf("%d\r\n", y);
return 0;
}
int fun(int x) {
return x;
}
函数指针作为参数使用:
#include <stdio.h>
typedef void(*fun)(int);
//前加一个typedef关键字,定义一个名为fun函数指针类型,而不是一个fun变量。
//形式同 typedef int* PINT;
void fun1(int x);
void fun2(int x);
void callback(fun f, int x);
int main() {
callback(fun1, 10);
callback(fun2, 20);
return 0;
}
void callback(fun f, int x) {
f(x);
}
void fun1(int x) {
printf("%d\r\n", x);
}
void fun2(int x) {
printf("%d\r\n", x);
}
函数指针数组
#include <stdio.h>
typedef void (*p_fun_array)(int, int);
void fun1(int x1, int x2);
void fun2(int x1, int x2);
p_fun_array func_array[2] = {
fun1,
fun2
};
int main() {
func_array[0](1, 2);
func_array[1](3, 4);
return 0;
}
void fun1(int x1, int x2) {
printf("fun1 x1=%d,x2=%d\r\n",x1,x2);
}
void fun2(int x1, int x2) {
printf("fun2 x1=%d,x2=%d\r\n", x1, x2);
}
#include <stdio.h>
void fun1(int x1, int x2);
void fun2(int x1, int x2);
int main() {
void (*fun[2])(int, int);
fun[0] = fun1;
fun[1] = fun2;
fun[0](1, 2);
fun[1](3, 4);
return 0;
}
void fun1(int x1, int x2) {
printf("fun1 x1=%d,x2=%d\r\n", x1, x2);
}
void fun2(int x1, int x2) {
printf("fun2 x1=%d,x2=%d\r\n", x1, x2);
}
回调函数
回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
//实现方法
void led_ctrl(int status) {
printf("led status = %d\r\n", status);
}
void buzzer_ctrl(int status) {
printf("buzzer status = %d\r\n", status);
}
//定义函数指针
typedef void(*ctrl_t)(int status);
ctrl_t ctrl_t_func[] = {
[0] = led_ctrl,
[1] = buzzer_ctrl
};
//定义函数指针数组,用于其他位置调用
ctrl_t ctrl[2];
void init() {
ctrl[0] = ctrl_t_func[0];
ctrl[1] = ctrl_t_func[1];
}
int main() {
init();
//通过下标调用目标函数
ctrl[0](1);
ctrl[1](1);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
//用户层代码
int add(int a, int b) {
return (a + b);
}
//驱动层
typedef int(*pAdd_t)(int, int);
static pAdd_t pAdd;
void callback_register(pAdd_t callback) {
pAdd = callback;
}
//底层调用
void drive_fun() {
pAdd(1, 2);
}
int main() {
callback_register(add);
return 0;
}
例子:四则运算的简单回调函数
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
/****************************************
* 函数指针结构体
***************************************/
typedef struct _OP {
float (*p_add)(float, float);
float (*p_sub)(float, float);
float (*p_mul)(float, float);
float (*p_div)(float, float);
} OP;
/****************************************
* 加减乘除函数
***************************************/
float ADD(float a, float b)
{
return a + b;
}
float SUB(float a, float b)
{
return a - b;
}
float MUL(float a, float b)
{
return a * b;
}
float DIV(float a, float b)
{
return a / b;
}
/****************************************
* 初始化函数指针
***************************************/
void init_op(OP* op)
{
op->p_add = ADD;
op->p_sub = SUB;
op->p_mul = &MUL;
op->p_div = &DIV;
}
/****************************************
* 库函数
***************************************/
float add_sub_mul_div(float a, float b, float (*op_func)(float, float))
{
return (*op_func)(a, b);
}
int main(int argc, char* argv[])
{
OP* op = (OP*)malloc(sizeof(OP));
init_op(op);
/* 直接使用函数指针调用函数 */
printf("ADD = %f, SUB = %f, MUL = %f, DIV = %f\n", (op->p_add)(1.3, 2.2),
(*op->p_sub)(1.3, 2.2),
(op->p_mul)(1.3, 2.2),
(*op->p_div)(1.3, 2.2));
/* 调用回调函数 */
printf("ADD = %f, SUB = %f, MUL = %f, DIV = %f\n",
add_sub_mul_div(1.3, 2.2, ADD),
add_sub_mul_div(1.3, 2.2, SUB),
add_sub_mul_div(1.3, 2.2, MUL),
add_sub_mul_div(1.3, 2.2, DIV));
return 0;
}
例子:物联网中AliOS-Things 注册AT指令
typedef struct
{
char *prefix;
char *postfix;
at_recv_cb cb;
}want_recv_t;
//赋值,初始化
want_recv_t at_xxx_cb[] = {
{AT_RECV_OK_PREFIX, NULL, at_ok_cb},
{AT_RECV_ERROR_PREFIX, AT_RECV_ERROR_POSTFIX, at_error_cb},
{AT_RECV_CPIN_PREFIX, AT_RECV_CPIN_POSTFIX, at_cpin_cb},
{AT_RECV_QGPS_PREFIX, AT_RECV_QGPS_POSTFIX, at_qgps_cb},
{AT_RECV_QGPSLOC_PREFIX, AT_RECV_QGPSLOC_POSTFIX, at_qgpsloc_cb},
{AT_RECV_HTTP_GPS_PREFIX, AT_RECV_HTTP_GPS_POSTFIX, at_http_gps_cb},
{AT_RECV_QIACT_PREFIX, AT_RECV_QIACT_POSTFIX, at_qiact_cb},
{AT_RECV_CONNECT_PREFIX, NULL, at_connect_cb},
{AT_RECV_QHTTPGET_PREFIX, AT_RECV_QHTTPGET_POSTFIX, at_qhttpget_cb},
{AT_MQTT_QMTCONN_PREFIX, AT_MQTT_QMTCONN_POSTFIX, at_mqtt_qmtconn_cb},
{AT_MQTT_SEND_PREFIX, NULL, at_mqtt_send_cb},
{AT_MQTT_QMTRECV_PREFIX, AT_MQTT_QMTRECV_POSTFIX, at_mqtt_mqtrecv_cb},
};
// 循环注册
void at_register_ncb(void)
{
int ret;
int i;
for (i = 0; i < sizeof(at_xxx_cb) / sizeof(want_recv_t); i++)
{
ret = at_register_callback(at_dev_fd,
at_xxx_cb[i].prefix,
at_xxx_cb[i].postfix,
recv_buf,
BUF_SIZE - 1,
at_xxx_cb[i].cb,
NULL);
if (ret == 0)
{
printf("i = %d,%s,register ok!\r\n", i, at_xxx_cb[i].prefix);
}
else
{
printf("i = %d,%s,register faild!\r\n", i, at_xxx_cb[i].prefix);
}
}
}