C 函数参数和返回值
我们在这里将介绍基于返回值和参数的不同类型的 C 函数。
因此,一个函数要么接受一些参数,要么不接受任何参数。同样,一个函数可以返回一些内容,否则不会返回任何内容。因此,我们可以将它们分成四种类型。
- 无参数且无返回类型的函数。
- 无参数且返回某种内容的函数。
- 接受参数但不返回任何内容的函数。
- 接受参数且也返回内容的函数。
示例
#include <stdio.h>
void my_function() {
printf("This is a function that takes no argument, and returns nothing.");
}
main() {
my_function();
}输出
This is a function that takes no argument, and returns nothing.
此函数不接受任何输入参数,并且返回类型为 void。因此,这不会返回任何内容。
示例
#include <stdio.h>
int my_function() {
printf("This function takes no argument, But returns 50
");
return 50;
}
main() {
int x;
x = my_function();
printf("Returned Value: %d", x);
}输出
This function takes no argument, But returns 50 Returned Value: 50
此函数不接受任何输入参数,但其返回类型为 int。因此,它返回一个值。
示例
#include <stdio.h>
void my_function(int x) {
printf("This function is taking %d as argument, but returns nothing", x);
return 50;
}
main() {
int x;
x = 10;
my_function(x);
}输出
This function is taking 10 as argument, but returns nothing
此函数接受一个输入参数,但其返回类型为 void。因此,这不会返回任何内容。
示例
#include <stdio.h>
int my_function(int x) {
printf("This will take an argument, and will return its squared value
");
return x * x;
}
main() {
int x, res;
x = 12;
res = my_function(12);
printf("Returned Value: %d", res);
}输出
This function is taking 10 as argument, but returns nothing
此函数接受任何输入参数,并且也返回值。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP