我是C++的新手,不確定如何將參數傳遞給函數。需要幫助理解傳遞參數到函數
我正在使用函數Distance()
來計算兩個節點之間的距離。 我聲明函數是這樣的:
int Distance(int x1, int y1, int x2 , int y2)
{
int distance_x = x1-x2;
int distance_y = y1- y2;
int distance = sqrt((distance_x * distance_x) + (distance_y * distance_y));
return distance;
}
在主存儲器我有2個for
循環。 我需要知道的是,如果我可以通過像這樣的值:Distance (i, j, i+1, j+1)
。
for(int i = 0; i < No_Max; i++)
{
for(int j = 0; j < No_Max; j++)
{
if(Distance(i, j, i+1, j+1) <= Radio_Range) // the function
node_degree[i] = node_degree[i] + 1;
cout << node_degree[i] << endl;
}
}
對我來說很好。有沒有理由認爲它不正確? – john
爲了得到更準確的距離值,你的函數應該返回一個浮點類型,比如'double'。 –
您可能不想像'sqrt'那樣將'sqrt'的結果轉換爲'int'。 – aschepler