Thread: problem with a function

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    5

    Question problem with a function

    Hi

    I am playing with functions for the first time [note, I am using C, not C++, so main ( ) is OK]. Here is my program:

    Code:
     	/* test for functions*/
    	
    	#include <stdio.h>
    	float FUNCTION;
    	main ( )
    	{
    		float xi, yi, x_new, y_new, h;	
    		xi = 1; yi = 1; h = 0.1;
    		y_new = yi + h * FUNCTION( xi, yi );
    		x_new = xi + h;
    		printf( "x\fy\n" );
    		printf( "%f\t%f\n", x_new, y_new );
    		}
    	}
    	float FUNCTION( float X, float Y )
    	{
    		return X + 3*Y/X;
    	}
    The compiler returns:

    line 12: Parse Error, expecting `','' or `SEP'
    'y_new = yi + h * FUNCTION( xi, yi )'
    aborting compile
    What is wrong? (Thanks)

  2. #2
    .........
    Join Date
    Nov 2002
    Posts
    303
    Few things.

    Code:
    float FUNCTION;
    Instead of declaring a function prototype you are telling the compiler you want a floating point variable called FUNCTION. Try this
    Code:
    /* See how it matches your definition */
    float FUNCTION(float X, float Y);
    Also you really should return 0 and use int main (void). My compiler gives me warnings so it can't be a good thing. I'm pretty sure that main() isn't acceptable in c99 also. Another thing, althought it's a matter of style, usually names in all caps are used for defines. There was also an extra } in there. Goodluck.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    5
    Dear helper,

    I followed all your advice, and now my program compiles.

    Thank you.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Just a side note:

    >>I am using C, not C++, so main ( ) is OK<<
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    14
    I am happy that your program compiles.

    Just a little piece of advice,
    It is always good practice to use a return type,
    and have the function return a value.

    Stay away from void main()
    Indeed it is acceptable but ... think about
    return to VOID (?????????)

    I hope that this advice is good for you.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stay away from void main()
    Good so far...

    Indeed it is acceptable but
    Here's your problem. void main is not valid. A simple read of the FAQ would have answered this for you.
    5.1.2.2.3 Program Termination

    [#1]
    ...
    If the return type is not compatible with int, the termination status returned to the host enviornment is unspeficied.
    In short, if you want void man, use Java.

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

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Correct me if I'm wrong, but doesn't C assume 'int' if no type is specified? So 'main ( )' is equivalent to 'int main ( )'.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Read Hammer's link.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I did... It didn't seem too clear on that point.

    edit: Never mind... I didn't see the word 'only'.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM