簡體   English   中英

如何將您創建的函數調用到 int main() c 編程中?

[英]How to Call a Function you've created into int main() c programming?

我試圖從main函數調用一個函數,但是當我編譯並運行它時它給了我一個隨機數。 我想要求用戶在yrBorn函數中輸入一個數字,然后將其返回到main函數中,最后在終端中顯示它。

int yrBorn(int);
#include<stdio.h>
int yrBorn (int yearBorn)    
{   
    printf("Enter the year you were born in: ");
    scanf("%d", &yearBorn);
    return yearBorn;
}

int main ()
{       
    printf("%d",yrBorn);
}

你必須像這樣理解你的代碼:

int yrBorn(int);  // function prototype say it must accept a int argument
#include<stdio.h>
int yrBorn (int yearBorn) //it must accept a argument of int type because of this definition    
{   
    printf("Enter the year you were born in: ");
    scanf("%d", &yearBorn);
    return yearBorn;
}

int main ()
{       
   printf("%d",yrBorn); // You are passing function pointer here not a function call use like yrBorn(1994)
    printf("%d",yrBorn(1994)); //it will work :)
}
#include<stdio.h>

int yrBorn();
int yrBorn()    
{   
    int yearBorn = 0;
    printf("Enter the year you were born in: ");
    scanf_s("%d", &yearBorn);
    return yearBorn;
}

int main()
{
    printf("%d", yrBorn());
}

修改您的程序以按預期工作!

您的語法不正確。

假設例如,我想創建一個函數,它接受一個數字並返回它的平方,我想,

#include<stdio.h>

int square(int);    //Function Prototype Declaration Telling Compiler  
                    //That there would be function named square whose        
                    //takes an integer and whose return type is integer.

int main()

{
   int x = 4;
   int y ;
   y = square(x);          //Calling Square Function passing Value `x`
                           //And Storing it in `y`.
   printf("%d\n" , y);     //Printing `y`

   //Or You Could As Well Do This
   printf("%d" , square(x));      //Without Storing Printing return
                                  //value of square(x)

  return 0;
 }


 int square(int k){   //Function Definition Contains The Code What The      
  int r = k*k;        //Function Does , Here it take the value passed      
  return r;           //to the function in this case `x` and store it 
                      //in `k` , and then initialise `r` to be `k*k` 
  }                   //and then returns `  

所以在你的代碼中

  int yrBorn(int);
  #include<stdio.h>
  int yrBorn (int yearBorn)    
  {   
      printf("Enter the year you were born in: ");
      scanf("%d", &yearBorn);
      return yearBorn;
  }

   int main ()
   {       
     printf("%d",yrBorn);      //Here You are not calling function.
     printf("%d" , yrBorn(0)); //This will work as in your prototype
                               //declaration you have declared
                               //function to take a int parameter and 
                               //return int
   }

我想你想要的是這個

  #include<stdio.h>
  int yrBorn(void);
  int yrBorn (void)   //Here Function is Expected to take no arguments 
                      //and return int value.
  {
    int yearBorn;
    printf("Enter Your Birth Year : ");
    scanf("%d" , &yearBorn);
    return yearBorn;

   }    

 int main(){
     printf("%d" , yrBorn());     //You Would Call it like this 
   }

請注意括號之間沒有任何內容,因為函數不接受任何參數。

請全心全意地閱讀一本體面的 C 書,忘記你已經知道的一切,否則你以后也會面臨類似的誤解,我建議觀看這個系列Nptel - C 編程簡介並檢查這個權威 C 書指南和最佳 C 編程列表初學者書籍。

printf("%d",yrBorn);

這不是調用函數的方式。 你正在做的而不是調用yrBorn是獲取它的address 然后將該地址解釋為int並按原樣打印。

要調用函數,您需要使用()運算符。 函數的任何參數都將放在括號內。

此外,您的函數正在接受一個參數,但沒有對其值做任何事情。 這個變量應該是函數的局部變量,而不是接受一個參數。

因此,通過這些修復,您的函數將如下所示:

int yrBorn ()    
{   
    int yearBorn;   // local instead of a parameter
    printf("Enter the year you were born in: ");
    scanf("%d", &yearBorn);
    return yearBorn;
}

你這樣稱呼它:

printf("%d",yrBorn());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM