#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0

    Need a little help understanding these errors/warnings


    I need a hand understanding why I am getting these errors/warnings and what I can do to fix them. Also, could you let me know if what I have done will produce the desired affect. Thanks in advance.

    Code:
    //Write a program with one function called mySum. The function will
    //have two parameters of type integer. The function will simply return
    //the sum of the two parameters passed in.
    //
    //Call the function mySum from the main function, passing in 5 and 8
    //to mySum. Print the results to the screen.
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int mySum;
    {
      int par, a, b;
      par = a + b;
      return (par);
    }
    
    int main ()
    {
       int a = 5, b = 8, par;
     
          par = mySum(a, b);
          printf( "Total value is : \n" );
    
             par = mySum(a);
             printf( "Total value is : \n" );
     
      system("PAUSE");	
      return 0;
    }
    Code:
    12: error: syntax error before '{' token
    14: error: `a' undeclared here (not in a function)
    14: error: `b' undeclared here (not in a function)
    14: warning: data definition has no type or storage class
    15: error: syntax error before "return"
    
    In function `main':
    22: error: called object is not a function
    25: error: called object is not a function
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Post


    Code:
    int mySum(int a,int b)
    {
      int par;
      par = a + b;
      return par;
    }
    
    int main()
    {
      int a = 5, b = 8, par;
      par = mySum(a, b);
      printf( "Total value is : \n" );
      return 0;
    }
    mySum must have the parameters it takes declared on the line announcing the function. Do not use system("pause"); to hold the display. Use getch(); instead and #include <conio.h> at the top if you want to use getch, kbhit and other related functions. Good luck! HTH,
    Graphic
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0
    Originally Posted by GraphicEqualis
    Code:
    int mySum(int a,int b)
    {
      int par;
      par = a + b;
      return par;
    }
    
    int main()
    {
      int a = 5, b = 8, par;
      par = mySum(a, b);
      printf( "Total value is : \n" );
      return 0;
    }
    mySum must have the parameters it takes declared on the line announcing the function. Do not use system("pause"); to hold the display. Use getch(); instead and #include <conio.h> at the top if you want to use getch, kbhit and other related functions. Good luck! HTH,
    Graphic
    Thanks for the quick response. Please forgive me if this is the wrong question, but are the changes for C instead of C++? I don't know all the "language" yet. I will try the suggestions in the mean time. Thanks again.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0
    OK. So I got it to compile now without errors or warnings. However, I don't get an "answer" when I run the program. Any ideas?

    new code:
    Code:
    //Write a program with one function called mySum. The function will
    //have two parameters of type integer. The function will simply return
    //the sum of the two parameters passed in.
    //
    //Call the function mySum from the main function, passing in 5 and 8
    //to mySum. Print the results to the screen.
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int mySum(int a,int b)
    {
      int par;
      par = a + b;
      return par;
    }
    
    int main ()
    {
       int a = 5, b = 8, par;
     
          par = mySum(a, b);
          printf( "Total value is : \n" );
     
      system("PAUSE");	
      return 0;
    }
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,156
    Rep Power
    2222
    Could it be because you're not telling it to output the value of par (which I assume is the "answer" you want to display)?
    printf( "Total value is : \n" );

    If you want to printf the value of par, then that should be something like:
    printf( "Total value is : %d\n", par);
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0
    :chomp:

    :tntworth:

    :cheers:

IMN logo majestic logo threadwatch logo seochat tools logo