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.