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

此函数接受任何输入参数,并且也返回值。

更新日期:2019 年 7 月 30 日

9K 以上的浏览量

开启你的职业生涯

完成课程以获得认证

开始
广告
© . All rights reserved.