-1

I’m new to C++ and unsure about how to pass arguments to functions.

I’m using a function Distance() to calculate the distance between two nodes. 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. What I need to know is if I can pass the values like this: 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;
    }  
}
4
  • 4
    Looks fine to me. Is there some reason you think it's not right?
    – john
    Oct 30, 2012 at 21:11
  • To get a more accurate distance value, your function should return a floating point type, such as double. Oct 30, 2012 at 21:16
  • You might not want to convert the result of sqrt to int like that.
    – aschepler
    Oct 30, 2012 at 21:16
  • 2
    If your program isn't working, make sure you're explicit about the error you're getting. Also, be a bit more clear in your question. Oct 30, 2012 at 21:18

3 Answers 3

3

Arguments to functions can be supplied as any expression which matches the type of that argument or can be cast to it.

2

Tips : You should use double instead of int if you want to use sqrt :

http://www.cplusplus.com/reference/clibrary/cmath/sqrt/

2

It looks as if you are calling your Distance(int, int, int, int) function correctly. The following statement will call Distance():

Distance (i, j, i+1, j+1);

This will store the value returned by Distance() in a variable:

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). If the left operand is less than or equal to the right operand, it will be evaluated to 1 (true). Otherwise it will be 0 (false). 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(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. Thus, distance will not equal the actual distance unless (distance_x * distance_x) + (distance_y * distance_y) is a perfect square. For better precision, consider using a double. If you intend to have the function return an int, it would be wise to do an explicit type cast, e.g.:

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.