Hi folks,
I am a bit confused about how specific one must be with arguments when declaring a function. I'll show you two functions from the book I'm using to learn C to show you what I mean.
Example 1 (greatest common denominator function):
Code:
void gcd (int u, int v) {
int temp;
printf ( "\nThe gcd of %i and %i is ", u, v );
while ( v != 0 ) {
temp = u % v;
u = v;
v = temp;
}
printf ( "%i\n", u);
}
So in that function, there are exactly two arguments, because that's how many arguments the algorithm to find the gcd takes. No problem there, makes sense to me.
Then further in the chapter on functions I run into this,
Example 2 (square root function):
Code:
float absoluteValue (float x) {
if ( x < 0 )
x = -x;
return x;
}
float squareRoot (float x) {
const float epsilon = .00001;
float guess = 1.0;
while ( absoluteValue (guess * guess - x) >= epsilon )
guess = ( x / guess + guess ) / 2.0;
return guess;
}
In this second example, we have a square root function that is preceded by an absolute value function. The absolute value function has the one argument, "float x", however when this function is called within the square root function, the arguments "guess * guess * -x" are passed to it. I'm confused how this absolute value function is working with all of that inside it, when it was originally declared with just "x." The only possibility I can think of is that this expression is treated as a single unit, but I'm not sure. Any clarification anyone could offer would be greatly appreciated.