Find the answer to your Linux question:
Results 1 to 6 of 6
hi, This is the code for a function: Code: #include <stdio.h> main&#40;&#41; &#123; float square&#40;&#41;; float a, b; printf&#40;"Enter any number\n"&#41;; scanf&#40;"%f",&a&#41;; b=square&#40;a&#41;; printf&#40;" sqaure of %f is %f\n",a,b&#41;; &#125; ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2004
    Posts
    46

    error coding a function in c


    hi,
    This is the code for a function:

    Code:
    #include <stdio.h>
    main&#40;&#41;
    &#123;
      float square&#40;&#41;;
      float a, b;
    
      printf&#40;"Enter any number\n"&#41;;
      scanf&#40;"%f",&a&#41;;
    
      b=square&#40;a&#41;;
      printf&#40;" sqaure of %f is %f\n",a,b&#41;;
    
    &#125;
    float square&#40;float x&#41;
    &#123;
       float y;
    
      y=x*x;
      return&#40;y&#41;;
    
    &#125;
    ***MOD edit: Please use the code bbcode enclosures when posting code!***

    and this is the error of result when run the program:
    [root@localhost lbin]# ./l138
    Enter any number
    2.5
    sqaure of 2.500000 is 0.000000
    plz tell me where is the mistake.

  2. #2
    Linux Newbie
    Join Date
    Jan 2004
    Location
    Houston, Tx
    Posts
    144
    Try this:

    Code:
    #include <stdio.h>
                                                                                    
    float square&#40;float x&#41;;
                                                                                    
    main&#40;&#41;
    &#123;
    float a, b;
                                                                                    
    printf&#40;"Enter any number\n"&#41;;
    scanf&#40;"%f",&a&#41;;
                                                                                    
    b=square&#40;a&#41;;
    printf&#40;" sqaure of %f is %f\n",a,b&#41;;
                                                                                    
    &#125;
    float square&#40;float x&#41;
    &#123;
    float y;
                                                                                    
    y=x*x;
    return y;
                                                                                    
    &#125;

  3. #3
    Linux Guru sarumont's Avatar
    Join Date
    Apr 2003
    Location
    /dev/urandom
    Posts
    3,682
    Yeah...mjordand's code should work (I'm too lazy to try it ). The problem was (if you don't see it) that you can't prototype your functions in main; they must be prototyped before main.
    "Time is an illusion. Lunchtime, doubly so."
    ~Douglas Adams, The Hitchhiker's Guide to the Galaxy

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Nov 2004
    Posts
    5
    Problem is with function declaration . Declare it before main

    cheer's
    naveen

  6. #5
    Linux User
    Join Date
    Jul 2004
    Posts
    256

    Re: error coding a function in c

    Quote Originally Posted by mines2s
    [root@localhost lbin]# ./l138
    Enter any number
    2.5
    sqaure of 2.500000 is 0.000000
    plz tell me where is the mistake.
    You should really create a user. Running as root all the time, and while programming is not the best idea.

    You can reduce your function to
    Code:
    float square&#40;float x&#41;
    &#123;
      return x*x;
    &#125;
    --monkey

  7. #6
    Linux User
    Join Date
    Oct 2004
    Location
    /dev/random
    Posts
    404
    Quote Originally Posted by sarumont
    Yeah...mjordand's code should work (I'm too lazy to try it ). The problem was (if you don't see it) that you can't prototype your functions in main; they must be prototyped before main.
    Not necessarily, they just have to be prototyped before being used...
    So, they can be prototyped inside main() too...

    The problem in this case was that the prototype and actual function are different - the prototype does not take any param and the actual function takes one.
    So, the compiler generates the call with NO PARAM (i.e. param goes as default value of the data type - which is 0) - the compiler sees that the function doesn't take params due to the most immediate declaration.

    Try the following code:

    Code:
    #include <stdio.h>
    
    void main&#40;&#41;
    &#123;
    	float square&#40;&#41;;
    	float a = 2.5, b;
    	b = square&#40;a&#41;;
    	printf&#40;"%f => %f\n", a, b&#41;;
    &#125;
    float square&#40;float x&#41;
    &#123;
    	printf&#40;"Inside square&#40;%f&#41;..\n", x&#41;;
    	return &#40;x*x&#41;;
    &#125;
    This is a working code - same as the one originally posted - except for the printf() in square().

    Now, you just change the declaration of square() from
    Code:
    float square&#40;&#41;;
    in main() to
    Code:
    float square&#40;float&#41;;
    in main() and the code works as expected...

    Or, the best option is - as you said - declare it outside main().
    Again, it need not be before main() as such - just that it (the declaration) should come before the first call to the function.
    The Unforgiven
    Registered Linux User #358564

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •