Help with the C Tutorial on functions.

This is a discussion on Help with the C Tutorial on functions. within the C Programming forums, part of the General Programming Boards category; Hi all, I'm currently going through the tutorial on c and I am confused by the following code: Code: #include ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    13

    Help with the C Tutorial on functions.

    Hi all, I'm currently going through the tutorial on c and I am confused by the following code:

    Code:
    #include <stdio.h>
    
    int mult ( int x, int y );
    
    int main()
    {
      int x;
      int y;
      
      printf( "Please input two numbers to be multiplied: " );
      scanf( "%d", &x );
      scanf( "%d", &y );
      printf( "The product of your two numbers is %d\n", mult( x, y ) );
      getchar(); 
    }
    
    int mult (int x, int y)
    {
      return x * y;
    }
    Is there any need for:
    Code:
    int mult (int x, int y)
    Surely,
    Code:
    int mult (x,y)
    does the same?
    For both the prototype and the definition...

    Thanks for any help!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need a prototype if the function is defined before you want to use it:
    Code:
    void foo( void )
    {
        printf("yay");
    }
    
    ...
    
    foo( );
    Don't need a prototype, because the entire function has been defined before I call foo on the last line there.
    Code:
    foo(); /* what's foo? */
    ...
    void foo( void )
    {
        printf("boo");
    }
    Error, because I don't know what foo is yet when I try to call it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Probably... "int" is implied when you don't specify. But why not get used to specifying the type everywhere. Otherwise what's the point of the prototype.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ah, I see what he was talking about. Your prototype can differ from the actual function only in the names of your variables:
    Code:
    int foo( int bar, int baz );
    ...
    int foo( int x, int y )
    {
        ...
    }
    We can do that. We can't do this:
    Code:
    int foo( float bar, int baz );
    ...
    void foo( double x, int y )
    {
        ...
    }
    We can do this:
    Code:
    void foo( int, int );
    ...
    void foo( int a, int b )
    {
        ...
    }
    All types in the prototype must match the definition. The names of variables are irrelevant and can even be excluded from the prototype.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    11
    you should define the XY
    #include <stdio.h>

    int mult ( int x, int y );

    int main()
    {
    int x;
    int y;
    int xy;

    printf( "Please input two numbers to be multiplied: " );
    scanf( "%d", &x );
    scanf( "%d", &y );

    xy=x*y;
    printf( "The product of your two numbers is %d\n", xy );
    getchar();
    getchar();

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    13
    The code was given in the c tutorial:
    [link]
    Cprogramming.com Tutorial: Functions
    [/link]

    Just to clarify that the code give:
    Code:
    #include <stdio.h>
    
    int mult ( int x, int y );
    
    int main()
    {
      int x;
      int y;
      
      printf( "Please input two numbers to be multiplied: " );
      scanf( "%d", &x );
      scanf( "%d", &y );
      printf( "The product of your two numbers is %d\n", mult( x, y ) );
      getchar(); 
    }
    
    int mult (int x, int y)
    {
      return x * y;
    }

    Does the same as:
    Code:
    #include <stdio.h>
    
    int mult ( int x, int y );   //here the type of x and y is not specified
    
    int main()
    {
      int x;
      int y;
      
      printf( "Please input two numbers to be multiplied: " );
      scanf( "%d", &x );
      scanf( "%d", &y );
      printf( "The product of your two numbers is %d\n", mult( x, y ) );
      getchar(); 
    }
    
    int mult (x, y) //here the type of x and y is not specified
    {
      return x * y;
    }
    So if the type, as in this second block of code, is not specified...it is taken to be an int as nonoob said. But if the type is specified, it cant be changed?

    Many thanks.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int mult ( int x, int y );   //here the type of x and y is not specified
    The type is specified.
    Code:
    int mult (x, y) //here the type of x and y is not specified
    You can't do that if you want it to actually compile without warnings/errors. You can't throw out the type.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "int mult(x, y)" defaults to int. If you had declared something else, you'd have a mismatch.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    You have a prototype, which is basically a copy of the function's signature with a semi-colon on the end. The signature and prototype should match almost exactly, with the only allowed difference being the parameter names. You can't just leave out the parameter types.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    13
    Sorry quzah, that one I meant to delete the int's so that the type wouldn't be specified. It compiled and ran just fine without the type of x and y being specified, but I should always specify them is the general consensus? I was just confused as it seemed unnecessary to keep declaring x and y each time they were written?

    Many thanks.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In C99, variables no longer default to 'int'. You must specify the type.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    13
    Ok, brilliant, I think i finally get it.

    So, the prototype is just for the compiler, if the function is defined after main....I'm guessing it would compile with a prototype even if the function wasnt defined at somepoint, but obviously wouldnt run?

    If the definition of the function is before main, there is no need for a prototype.
    The definition has to be the same as the prototype except for the names of the variables.
    When the function is used in main, it can be passed any 2 variables as long as they are the same type as the prototype.

    Going on these assumptions, the following is correct (and follows good practice?):

    Code:
    #include <stdio.h>
    
    int mult ( int x, int y );   //prototype
    
    int main()
    {
      int f;
      int r;
      
      printf( "Please input two numbers to be multiplied: " );
      scanf( "%d", &f );
      scanf( "%d", &r );
      printf( "The product of your two numbers is %d\n", mult(f,r));
      getchar(); 
      getchar();
    }
    
    int mult ( int eggs,int bread)
    {
      return eggs * bread;
    }
    One final question, if all the variables were first declared, would it be then be ok to leave out the types in the prototype and function, as follows:

    Code:
    #include <stdio.h>
    
    int x,y,f,r,eggs,bread;   //declare the variables first
    
    int mult ( x,  y );             //then no need to specify type in protoype?
    int main()
    {
      printf( "Please input two numbers to be multiplied: " );
      scanf( "%d", &f );
      scanf( "%d", &r );
      printf( "The product of your two numbers is %d\n", mult(f,r));
      getchar(); 
      getchar();
    }
    
    int mult (eggs, bread)   //no need to specify a type of eggs or bread as already declared?
    {
      return eggs * bread;
    }
    Do I seem to have the correct ideas?

    Thankyou for your help!

  13. #13
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by folem001 View Post
    Do I seem to have the correct ideas?
    All except for this:

    Quote Originally Posted by folem001 View Post
    When the function is used in main, it can be passed any 2 variables as long as they are the same type as the prototype.
    Functions can take any types as parameters and as many of them as you want (your compiler has to support up to at least 32 for any one function I think).

    Calling a function from main is the exact same as calling a function from anywhere else.

    EDIT: Looking through your code, you do still seem to have some confusion on some points. The variable names aren't important in the function declaration, it only NEEDS the types (but you can put variable names if you want). A function definition NEEDS both types and variables. The variables do not need to be created beforehand, they are simply names for the variables that are passed in.

    Also, if you're getting confused with terminology: Function Declaration/Prototype is what the compiler needs to 'find' the function. It should be located somewhere before the function is called. The function definition is where the actual code of the function is.
    Last edited by DeadPlanet; 05-28-2010 at 06:34 AM.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    13
    Functions can take any types as parameters and as many of them as you want (your compiler has to support up to at least 32 for any one function I think).
    OK, but for it (in this basic example) to give a desired output, they have to be the same as the prototype? For example, if f and r were char's, this wouldn't work..?

    But I understand that you can write a function to take all sorts of types.

    Thanks.

  15. #15
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    It would, because C converts the chars into ints when it is called. If you called it with a float or double then you might get an error.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with functions
    By jusfry01 in forum C Programming
    Replies: 2
    Last Post: 05-22-2010, 07:25 PM
  2. Functions calling other functions.
    By kbro3 in forum C++ Programming
    Replies: 2
    Last Post: 12-27-2009, 12:10 AM
  3. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM

Tags for this Thread


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