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\n");
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\n");
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
在这里,此函数采用任何输入参数,并且还返回值。
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短