Like Tree2Likes
  • 2 Post By rcgldr

arguments of functions

This is a discussion on arguments of functions within the C Programming forums, part of the General Programming Boards category; Hi folks, I am a bit confused about how specific one must be with arguments when declaring a function. I'll ...

  1. #1
    Registered User SCRIPT_KITTEH's Avatar
    Join Date
    Apr 2013
    Posts
    74

    Question arguments of functions

    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.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,313
    Quote Originally Posted by SCRIPT_KITTEH View Post
    The arguments "guess * guess - x" are passed to it.
    "guess * guess - x" is an expression that produces a single value, and that single value is what is passed as an argument.

  3. #3
    Registered User SCRIPT_KITTEH's Avatar
    Join Date
    Apr 2013
    Posts
    74
    Quote Originally Posted by rcgldr View Post
    "guess * guess - x" is an expression that produces a single value, and that single value is what is passed as an argument.
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inlined functions as arguments
    By DarkAlex in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2009, 07:06 AM
  2. Functions as arguments
    By jw232 in forum C++ Programming
    Replies: 2
    Last Post: 04-19-2008, 11:11 AM
  3. variable arguments in functions
    By Stonehambey in forum C++ Programming
    Replies: 11
    Last Post: 02-22-2008, 04:10 AM
  4. help with functions using variables as arguments
    By kristentx in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2006, 09:36 AM
  5. passing arguments through functions
    By cwd in forum C Programming
    Replies: 2
    Last Post: 09-30-2001, 06:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21