Thread: Float function prototype inside main?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Float function prototype inside main?

    Hi,
    I'm trying to write a code for square using function. However I get the error when I declare the function prototype outside (similar to what I do for int functions), While It works fine when I declare the float function prototype inside main. Why it differ from int function? and Do I need to write prototype in every function where I need to call Square.

    Working Code:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    
    int main( )
    
    {
    float square(float x) ;
    float a, b ;
    	printf ( "\nEnter any number " ) ;
    	scanf ( "%f", &a ) ;
    	b = square ( a ) ;
    printf ( "\nSquare of %f is %f", a, b ) ;
    }
    
    float square ( float x )
    {
    float y ;
    y = x * x ;
    return ( y ) ;
    }
    Code Not working (With prototype outside main)


    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    
    int main( )
    float square(float x) ;
    
    {
    
    float a, b ;
    	printf ( "\nEnter any number " ) ;
    	scanf ( "%f", &a ) ;
    	b = square ( a ) ;
    printf ( "\nSquare of %f is %f", a, b ) ;
    }
    
    float square ( float x )
    {
    float y ;
    y = x * x ;
    return ( y ) ;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Code Not working (With prototype outside main)
    No, this is outside main

    float square(float x) ;
    int main( )

    What you have is just a syntax error.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    Oops, yes. Got it. I was declaring prototype below main(). It should be above. Much thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-15-2011, 01:47 PM
  2. main prototype / main.h
    By boxden in forum C Programming
    Replies: 3
    Last Post: 03-16-2010, 04:43 AM
  3. Why put the prototype inside the function definition?
    By Overworked_PhD in forum C Programming
    Replies: 14
    Last Post: 01-11-2010, 01:48 PM
  4. calling a function inside main()
    By mero24 in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2005, 01:22 AM
  5. extra braces inside main
    By JaWiB in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 06:20 PM