I am trying to write a function that calculates Euclidean Distance between two float points. I coded the function and I called it but I got some errors. How can I fix that issues?
My code:
#include <stdio.h>
#include <math.h>
//Function Declarations
float CalculateDistance(float, float, float, float);
int main ()
{
/* Function Call */
float distance1 = CalculateDistance(float x1,float y1,float x2,float y2);
printf(" Point1 Point 2 Distance\n");
printf("(%4.1f, %4.1f)", x1, y1);
printf("(%4.1f, %4.1f)", x2, y2);
printf("%4.1f\n", distance1);
return 0;
}
//Distance Function
double CalculateDistance(float x1, float y1, float x2, float y2)
{ printf("Enter X1 : ");
scanf("%f",&x1);
printf("Enter Y1 : ");
scanf("%f",&y1);
/////////////////////////////////////
printf("Enter X2 : ");
scanf("%f",&x2);
printf(" Enter Y2 : ");
scanf("%f",&y2);
//////////////////////////////////////
double diffx = x1 - x2;
double diffy = y1 - y2;
double diffx_sqr = pow(diffx,2);
double diffy_sqr = pow(diffy,2);
double distance = sqrt(diffx_sqr diffy_sqr);
return distance;
}
The errors I get:
main.c:18:37: error: expected expression before ‘float’
18 | float distance1 = CalculateDistance(float x1,float y1,float x2,float y2);
| ^~~~~
main.c:18:19: error: too few arguments to function ‘CalculateDistance’
18 | float distance1 = CalculateDistance(float x1,float y1,float x2,float y2);
| ^~~~~~~~~~~~~~~~~
main.c:13:7: note: declared here
13 | float CalculateDistance(float, float, float, float);
| ^~~~~~~~~~~~~~~~~
main.c:22:26: error: ‘x1’ undeclared (first use in this function); did you mean ‘y1’?
22 | printf("(%4.1f, %4.1f)", x1, y1);
| ^~
| y1
main.c:22:26: note: each undeclared identifier is reported only once for each function it appears in
main.c:23:26: error: ‘x2’ undeclared (first use in this function)
I am pretty new in C. Thanks a lot for help.
CodePudding user response:
I'll start by stating that your first issue was not caring enough about how the code looks. Far from being a matter of vanity, code clarity starts with adequate indentation and that's very relevant both for you to more clearly see what's going on and to other people that will eventually get in touch with your code (this situation is a very good example :) ). There are many sources on the web on why indentation is important. Some I've found on a quick search are this one and this one.
To your code: some of these error messages really aren't very intuitive and that can confuse people who are not experienced with the language. But others are clear in what's going on. Namely:
main.c:22:26: error: ‘x1’ undeclared (first use in this function); did you mean ‘y1’?
and
main.c:23:26: error: ‘x2’ undeclared (first use in this function)
Regarding these two errors, the fact is that the compiler couldn't identify where the variables x1
and x2
where declared. You attempt to use them on the printf
calls but there's no definition in the current scope (i.e. the current logical block, in this specific case the main
function).
Nowhere in your code you declare the x1
, x2
, y1
and y2
variables. The first step to fixing your code is doing it.
Another thing that's likely not what you meant to do (given the function expects the data as its parameters) was adding the user input collection to the CalculateDistance
function. Given its name one would expect its purpose was to calculate the distance, nothing more. It's a good practice to split your code in small, specific functions, with few responsibilities.
Another issue is that you declare the CalculateDistance
function with type float
on line 6 and define it with type double
on line 25.
Finally, the last one (not something that would prevent you to compile your code) is that you're defining the x
and y
variables with float
types and, in the CalculateDistance
function you're working with double
s. This is known as implicit conversion and could be the source of, sometimes hard to debug, bugs. In this specific scenario it likely wouldn't because you're moving the float
data into a double
type, which holds values with greater or same precision, but it's important you're aware of that and try to avoid it as much as possible. And, in the scenarios you're aware of what's going on and want/need to convert between different types, a good habit is to do it explicitly so it's clear to you and whoever sees your code that was intentional.
Here is a potential version of your code with the changes I've mentioned above applied:
#include <stdio.h>
#include <math.h>
// Fixed declaration type
double CalculateDistance(float, float, float, float);
int main ()
{
// Variable declarations
float x1;
float y1;
float x2;
float y2;
// Moved input-handling to `main`
printf("Enter X1 : ");
scanf("%f",&x1);
printf("Enter Y1 : ");
scanf("%f",&y1);
/////////////////////////////////////
printf("Enter X2 : ");
scanf("%f",&x2);
printf(" Enter Y2 : ");
scanf("%f",&y2);
//////////////////////////////////////
/* Function Call */
double distance1 = CalculateDistance(x1, y1, x2, y2);
printf(" Point1 Point 2 Distance\n");
printf("(%4.1f, %4.1f)", x1, y1);
printf("(%4.1f, %4.1f)", x2, y2);
// Used `%lf` instead of `%f` to represent `double`s
printf("%4.1lf\n", distance1);
return 0;
}
//Distance Function
double CalculateDistance(float x1, float y1, float x2, float y2)
{
double diffx = x1 - x2;
double diffy = y1 - y2;
double diffx_sqr = pow(diffx,2);
double diffy_sqr = pow(diffy,2);
double distance = sqrt(diffx_sqr diffy_sqr);
return distance;
}
Some final considerations:
- In my opinion a very good introduction to the C language is given by Brian Kernighan and Dennis Ritchie in The C Programming Language. I recommend you focus on the beginning and try to avoid as many bad practices as possible early on
- Pay special attention to code indentation
- Learn how to tweak compiler flags on your environment and search for recommended ones. A few I personally like and use are:
-Wall
,-Wconversion
,-Werror
and-Wpedantic
. You can find more information about them ongcc
's man page and, if you're not usinggcc
, you can look for similar results with flags in your compiler