Float function prototype inside main?

This is a discussion on Float function prototype inside main? within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to write a code for square using function. However I get the error when I declare the ...

  1. #1
    URS
    URS is offline
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    URS
    URS is offline
    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, 12: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, 12:48 PM
  4. calling a function inside main()
    By mero24 in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2005, 12:22 AM
  5. extra braces inside main
    By JaWiB in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 06:20 PM

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