In this code I seem to get zero, I'm not too familiar with as to why I can't change the variable length with the function I created. Any help could be useful.
#include <stdio.h>
double get_length(double a);
int main(int argc, char* argv[])
{
double length = 0;
get_length(length);
printf("%lf", length);
return 0;
}
double get_length(double a)
{
printf("What is the rectangle's length?\n");
scanf("%lf", &a);
return a;
}
When it prints it returns 0.0000
You're not storing the return value. Change:
to:
There's no need to pass
length
when you do this.The other way to do it is to pass an address:
Note that
%f
, not%lf
, is the correct format specifier for adouble
inprintf()
. Using%lf
is correct forscanf()
, however.C is "pass-by-value", which means the value is actually copied in. So changing the value by using the reference doesn't actually change the original reference.
There are two ways around this:
1) store to a local value and then capture the return value:
2) pass a pointer: