可能重複:
How to modify content of the original variable which is passed by value?爲什麼不返回修改參數的值的函數
我建立一個非常簡單的程序,計算矩形的面積。很簡單,但你會注意到我似乎無法獲得返回值。我一直在看0.可能有一個明顯的答案,或者有些東西我只是不明白。繼承人我的代碼:
#include<stdio.h>
//prototypes
int FindArea(int , int , int);
main()
{
//Area of a Rectangle
int rBase,rHeight,rArea = 0;
//get base
printf("\n\n\tThis program will calculate the Area of a rectangle.");
printf("\n\n\tFirst, enter a value of the base of a rectangle:");
scanf(" %d" , &rBase);
//refresh and get height
system("cls");
printf("\n\n\tNow please enter the height of the same rectangle:");
scanf(" %d" , &rHeight);
//refresh and show output
system("cls");
FindArea (rArea , rBase , rHeight);
printf("\n\n\tThe area of this rectangle is %d" , rArea);
getch();
}//end main
int FindArea (rArea , rBase , rHeight)
{
rArea = (rBase * rHeight);
return (rArea);
}//end FindArea
分配'rArea = 0'在頂部,這就是爲什麼它總是返回0 – squiguy
如果您還沒有一個關於這個頭緒,你還沒有仔細閱讀一個基本的C教程。 -1。 – 2012-10-02 23:12:13
是的,我有。這個例子與我正在做的無關,使用(void)或換句話說沒有返回值。另一個使用的加法和我的教授不會講英語。我使用了一種不同的方法來宣稱變量,只是試驗一種比單獨初始化更快的方法。然而,這與問題無關,我感謝你的反饋,但侮辱我也與我做錯了什麼無關。這是我第一次嘗試創建一個功能,並明顯誤解了這個概念。 – Ev00