function type void or float

This is a discussion on function type void or float within the C Programming forums, part of the General Programming Boards category; When do you use void function() and when to use float function()? I downloaded some code for the bisection method. ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    190

    function type void or float

    When do you use void function() and when to use float function()?
    I downloaded some code for the bisection method. It calls a function f:
    Code:
    float Root(float left, float right, float tol, int *count)
    {
    float p;
    /* the root is bracketed by the values of left and right.
    The value of p is the midpoint of this interval.
    The variable count measures the number of times the algorithm
    executes*/
    while(fabs(left-right) > tol) // program stops when the interval < tol
    	{
    	p=(left+right)/2.0;
    	if(f(p) ==0)  return p;
    	if(f(p)*f(right) < 0) left=p; else right=p;
    	(*count)++;
    	}
    return p;
    }
    This next part of my main function tries to find the first value of the function f that is less than 0
    Code:
    while (f(guess) > 0)
        {
    
            guess = guess + 0.1;
    
        }
    before calling the function root above.
    my function f is
    Code:
    float f(float x)
    {
       return c1_global*exp(-2*a_global*x)+eta_glob*((1+0.5*(4*a_global*a_global+1))*cos(x+phi_glob)-2*a_global*sin(x+phi_glob))+b_global*b_global-omega_2_glob;
    
    }
    However I get an error conflicting types for f. I tried changing it to void (however the original author put float)

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,406
    Changing float for void will not accomplish anything if you still want your function to return a value
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by a.mlw.walker View Post
    When do you use void function() and when to use float function()?
    Use void when you don't plan on returning anything. Use float when you plan on returning a float.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-24-2011, 04:37 AM
  2. GLfloat vs float, GLvoid vs void...
    By pro-grammin in forum Game Programming
    Replies: 2
    Last Post: 01-19-2003, 08:40 PM
  3. void function(type **, type *) // more info. PLEASE HELP
    By Seek n Destroy in forum C Programming
    Replies: 5
    Last Post: 09-14-2002, 07:44 PM
  4. void function(type **, type *)
    By John Wallace in forum C Programming
    Replies: 3
    Last Post: 09-14-2002, 02:37 AM
  5. Replies: 5
    Last Post: 05-04-2002, 12:31 PM

Tags for this Thread


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