function declaration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • magnacarta
    New Member
    • Jul 2007
    • 3

    function declaration

    #include<iostre am.h> #include<iostre am.h>
    #include<conio. h>
    #include<math.h >

    void display()
    int main()
    {
    int a=9;
    int b=10;
    clrscr();
    cout<<display(a ,b)<<"\n";
    return 0;
    getch();
    }
    void display(int A[] [],int m, int n)
    {
    int i,j;
    for (i=0;i<m;i++)
    for (j=0;j<n;j++)
    cout<<" "<<A[i][j];
    cout<<"\n";
    }

    i have listed my program to call a function above but it is showing an error as below in line 5 called ' Declaration syntax error'

    kindly help me solve this
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by magnacarta
    #include<iostre am.h> #include<iostre am.h>
    #include<conio. h>
    #include<math.h >

    void display()
    int main()
    {
    int a=9;
    int b=10;
    clrscr();
    cout<<display(a ,b)<<"\n";
    return 0;
    getch();
    }
    void display(int A[] [],int m, int n)
    {
    int i,j;
    for (i=0;i<m;i++)
    for (j=0;j<n;j++)
    cout<<" "<<A[i][j];
    cout<<"\n";
    }

    i have listed my program to call a function above but it is showing an error as below in line 5 called ' Declaration syntax error'

    kindly help me solve this
    You have define and declare same function with different parameters!!! I am pointing to void display()

    Regards

    Comment

    • joeschnell
      New Member
      • Apr 2007
      • 47

      #3
      Yeah, I'm just starting CIS247 and went through functions briefly but, he is right on the paramaters. When you declare your function within the () you need to list the same paramaters as you use below in your code using the & before repeated use declarations.

      functions (one, &two, three, four, &five, six)

      int main ()

      ...
      ...
      ...
      ..now you call it
      functions (here you need to call out the parameters the same as above)
      (one, two, three, four, five, six)
      Your code seems pretty sophisticated to not know how to call a function. Hope you get it. Joe

      Comment

      • joeschnell
        New Member
        • Apr 2007
        • 47

        #4
        void display (int, &double, &double, &char, int, double)


        when calling

        void display (int, double, double, char, int, double)

        or close to that--later

        Comment

        • magnacarta
          New Member
          • Jul 2007
          • 3

          #5
          Originally posted by joeschnell
          void display (int, &double, &double, &char, int, double)


          when calling

          void display (int, double, double, char, int, double)

          or close to that--later


          hi Joe, thanx for the help i made the changes but it shows 2 errors now.
          1. size of the type is unknown or zero and 2. declaration is expected

          i m pasting the program again with my changes

          #include<iostre am.h> #include<iostre am.h>
          #include<conio. h>
          #include<math.h >
          void display(int A[] [],int m, int n,)
          int main()
          {
          int a=9;
          int b=10;
          clrscr();
          cout<<display(a ,b)<<"\n";
          return 0;
          getch();
          }
          void display(int A[] [],int m, int n)
          {
          int i,j;
          for (i=0;i<m;i++)
          for (j=0;j<n;j++)
          cout<<" "<<A[i][j];
          cout<<"\n";
          }

          kindly b patient as i have just started learning C++ and am doing it on my own with aid from you guys and this site

          Comment

          • Meetee
            Recognized Expert Contributor
            • Dec 2006
            • 928

            #6
            Originally posted by magnacarta
            hi Joe, thanx for the help i made the changes but it shows 2 errors now.
            1. size of the type is unknown or zero and 2. declaration is expected

            i m pasting the program again with my changes

            #include<iostre am.h> #include<iostre am.h>
            #include<conio. h>
            #include<math.h >
            void display(int A[] [],int m, int n,)
            int main()
            {
            int a=9;
            int b=10;
            clrscr();
            cout<<display(a ,b)<<"\n";
            return 0;
            getch();
            }
            void display(int A[] [],int m, int n)
            {
            int i,j;
            for (i=0;i<m;i++)
            for (j=0;j<n;j++)
            cout<<" "<<A[i][j];
            cout<<"\n";
            }

            kindly b patient as i have just started learning C++ and am doing it on my own with aid from you guys and this site
            Hi magnacarta,

            Again you check your code. You are calling display with two parameters and function is defined/declared with 3 parameters. You can define array locally in display function as you are just printing the value locally.

            Regards

            PS. Kindly put code tags around code

            Comment

            • archonmagnus
              New Member
              • Jun 2007
              • 113

              #7
              Just for comparison:
              [code=cpp]
              #include<iostre am> // removed trailing ".h"
              #include<conio. h> // careful, this is a Windows only library!!!
              #include<cmath> // changed from "math.h" to "cmath"

              // old declaration: notice there is a comma after 'n'
              // and no semicolon at the end of the prototype
              // void display(int A[] [],int m, int n,)

              // new function prototype
              void display (int a, int b);

              int main()
              {
              int a=9;
              int b=10;

              // ...stuff before function call...
              cout<<display(a ,b)<<"\n";
              // ... stuff after function call...
              }

              // old declaration
              //void display(int A[] [],int m, int n)
              void display (int a, int b)
              {
              // ...stuff in function...
              }
              [/code]

              Or if you still need the array, then the function call is the part of the code that needs to be edited. i.e., you'd need to add the array to be passed to the function.

              Comment

              • anubhavit
                New Member
                • Jul 2007
                • 5

                #8
                Originally posted by magnacarta
                #include<iostre am.h> #include<iostre am.h>
                #include<conio. h>
                #include<math.h >

                void display()
                int main()
                {
                int a=9;
                int b=10;
                clrscr();
                cout<<display(a ,b)<<"\n";
                return 0;
                getch();
                }
                void display(int A[] [],int m, int n)
                {
                int i,j;
                for (i=0;i<m;i++)
                for (j=0;j<n;j++)
                cout<<" "<<A[i][j];
                cout<<"\n";
                }

                i have listed my program to call a function above but it is showing an error as below in line 5 called ' Declaration syntax error'

                kindly help me solve this

                The error in the program is that you are not passing the 3 arguments that you are making use of in the function. Change the display function to
                void display(int m, int n)
                {int A[3] [3];
                int i,j;
                for (i=0;i<m;i++)
                for (j=0;j<n;j++)
                printf("%d",A[i][j]);
                printf("\n");
                }
                and your program should work

                Comment

                • magnacarta
                  New Member
                  • Jul 2007
                  • 3

                  #9
                  Originally posted by anubhavit
                  The error in the program is that you are not passing the 3 arguments that you are making use of in the function. Change the display function to
                  void display(int m, int n)
                  {int A[3] [3];
                  int i,j;
                  for (i=0;i<m;i++)
                  for (j=0;j<n;j++)
                  printf("%d",A[i][j]);
                  printf("\n");
                  }
                  and your program should work
                  i made the changes as to my understanding but not there is an error 'Declaration syntax error' in line 5. could some one kindly make this program error free put it here. sorry but thanx

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Originally posted by magnacarta
                    void display(int m, int n)
                    {int A[3] [3];
                    int i,j;
                    for (i=0;i<m;i++)
                    for (j=0;j<n;j++)
                    printf("%d",A[i][j]);
                    printf("\n");
                    }
                    You have nested for loops but no braces:
                    [code=cpp]
                    for (etc...)
                    {
                    for (etc...)
                    {

                    }
                    }
                    [/code]

                    Also, the array is a local variable so there is no way to get values in the array from main(). Did you mean to do that??? Or did you mean to create the array in main() and then call the function to display the array?? I thinkl you still have a logic problem.

                    Comment

                    • phiefer3
                      New Member
                      • Jun 2007
                      • 67

                      #11
                      Originally posted by magnacarta
                      hi Joe, thanx for the help i made the changes but it shows 2 errors now.
                      1. size of the type is unknown or zero and 2. declaration is expected

                      i m pasting the program again with my changes

                      #include<iostre am.h> #include<iostre am.h>
                      #include<conio. h>
                      #include<math.h >
                      void display(int A[] [],int m, int n,)
                      int main()
                      {
                      int a=9;
                      int b=10;
                      clrscr();
                      cout<<display(a ,b)<<"\n";
                      return 0;
                      getch();
                      }
                      void display(int A[] [],int m, int n)
                      {
                      int i,j;
                      for (i=0;i<m;i++)
                      for (j=0;j<n;j++)
                      cout<<" "<<A[i][j];
                      cout<<"\n";
                      }

                      kindly b patient as i have just started learning C++ and am doing it on my own with aid from you guys and this site
                      Magnacarta, so far in every one of your posts, you also forgot the ; at the end of your prototype in line 5:

                      void display(int A[] [],int m, int n,)

                      should be

                      void display(int A[] [],int m, int n,);

                      Comment

                      Working...