[英]Need help understanding passing arguments to function
I'm new to C++ and unsure about how to pass arguments to functions. 我是C ++的新手,不确定如何将参数传递给函数。
I'm using a function Distance()
to calculate the distance between two nodes. 我正在使用一个函数
Distance()
来计算两个节点之间的距离。 I declare the function like this: 我这样声明函数:
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;
}
In the main memory I have 2 for
loops. 在主内存中,我有2个
for
循环。 What I need to know is if I can pass the values like this: Distance (i, j, i+1, j+1)
. 我需要知道的是是否可以传递这样的值:
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;
}
}
函数的参数可以作为与该参数的类型匹配的任何表达式提供,也可以强制转换为该参数。
Tips : You should use double instead of int if you want to use sqrt : 提示:如果要使用sqrt,则应使用double而不是int:
http://www.cplusplus.com/reference/clibrary/cmath/sqrt/ http://www.cplusplus.com/reference/clibrary/cmath/sqrt/
It looks as if you are calling your Distance(int, int, int, int)
function correctly. 似乎您正在正确调用
Distance(int, int, int, int)
函数。 The following statement will call Distance()
: 以下语句将调用
Distance()
:
Distance (i, j, i+1, j+1);
This will store the value returned by Distance()
in a variable: 这会将
Distance()
返回的值存储在变量中:
int dist = Distance (i, j, i+1, j+1);
This will compare the value returned by Distance()
(the left operand) to Radio_Range
(the right operand). 这会将
Distance()
(左操作数)返回的值与Radio_Range
(右操作数)进行比较。 If the left operand is less than or equal to the right operand, it will be evaluated to 1
(true). 如果左操作数小于或等于右操作数,它将被评估为
1
(true)。 Otherwise it will be 0
(false). 否则它将为
0
(假)。 If the overall value of the expression inside the if
statement is non-zero, the statement or block immediately following the if
statement will be executed: 如果
if
语句中表达式的总值不为零,则将执行if
语句之后的语句或块:
if(Distance(i, j, i+1, j+1) <= Radio_Range)
//Statement;
or: 要么:
if(Distance(i, j, i+1, j+1) <= Radio_Range){
//Statement;
//Statement;
//...
}
However, the value returned by Distance()
will be truncated to an integer. 但是,
Distance()
返回的值将被截断为整数。 Thus, distance
will not equal the actual distance unless (distance_x * distance_x) + (distance_y * distance_y)
is a perfect square. 因此,除非
(distance_x * distance_x) + (distance_y * distance_y)
是一个完美的平方,否则distance
将不等于实际距离。 For better precision, consider using a double
. 为了获得更高的精度,请考虑使用
double
。 If you intend to have the function return an int
, it would be wise to do an explicit type cast, eg: 如果打算让该函数返回
int
,则进行显式类型转换是明智的,例如:
int distance = (int)sqrt((distance_x * distance_x) + (distance_y * distance_y));
This will ensure that if you or anyone else looks at the code later on, they will not think the function is using the wrong data type. 这样可以确保,如果您或其他任何人以后再看代码,他们将不会认为该函数使用了错误的数据类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.