Errror message: Extra parameter in call function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinayak1209
    New Member
    • Jan 2021
    • 1

    Errror message: Extra parameter in call function

    When I'm calling a function
    f=fact(a) then error message is showing up "Extra parameter in call to fact()

    Here is the code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    void main ()
    {
               int a;
               long f,fact();
               clrscr();
               printf ("\n Enter a Number : ");
               scanf ("%d",&a);
               f=fact(a);
               printf ("\n The Factorial of %d is : %ld",a,f);
               getch();
    }
           long fact(int b)
     {
         int i;
         long f=1;
         for (i=1;i<=b;i++)
                f=f*i;
                return(f);
    }
    Last edited by Banfa; Jan 25 '21, 06:24 PM. Reason: Added code tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Your function prototype says the fact function has no arguments. The prototype should be the same as the first line of the function:

    long fact(int);

    Comment

    • AjayGohil
      New Member
      • Apr 2019
      • 83

      #3
      Hello,

      Declaration of function is not proper, use long fact(int a) or you can declare function globally outside the main function.
      Code:
      #include <stdio.h>
      #include <conio.h>
      long fact(int a) ;
      void main ()
      {
                 int a;
                 long f;
                 clrscr();
                 printf ("\n Enter a Number : ");
                 scanf ("%d",&a);
                 f=fact(a);
                 printf ("\n The Factorial of %d is : %ld",a,f);
                 getch();
      }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Not quite correct.

        When you use:

        long f;
        Int a;
        ....etc...

        f = fact(a );

        The compiler must already seen the code for the function. Otherwise the compiler can't know if you are using the function correctly.

        Therefore, you put the function code in your file outside of main(). Unfortunately, if you need this function in another implementation file you must make a copy of the function code and paste it in the second file outside of any function and before the first call.

        Everything works but you now have two copies of the function code. Duplicate code is a big no-no.

        You avoid the duplicate by erasing all of the function code except the first line:

        long fact(int);

        This is the function prototype. It tells the compiler that there is fact function with an int argument
        that returns an int but the function code is somewhere else. Now instead generating an error, the compiler generates an "unresolved external reference". This hands the responsibilty of finding the function code and linking it to the function calls to the linker.

        Comment

        Working...