Prototypes or no?

This is a discussion on Prototypes or no? within the C Programming forums, part of the General Programming Boards category; I've just learned about functions and I learned that you can put a prototype before the main function and it ...

  1. #1
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Smile Prototypes or no?

    I've just learned about functions and I learned that you can put a prototype before the main function and it has to have the ; semicolons and the compiler will not give error!

    But I also think that it is annoying to put the prototype, then the function and then the return!
    For example, let's take printf, it is a function in the stdio.h! You do not have to put return or prototypes!You just call the function, give its arguments and that's it!

    Can you pls give me an example, how can u use a function like subtract, let's say made by you ?
    Let's say I want to make the function subtract that will subtract y from x

    Is it possible to do this???

    Code:
    #include <stdio.h>
    
    int subtract(int x, int y)
    {
        printf ("%d", x - y);
    }
    int main()
    {
    	int x;
    	int y;
        scanf("%d", &x);
        scanf("%d", &y);
    
        subtract();
    }
    I tried it but it doesn't work!

    It tells me too few arguments for subtract in an error message!

    Can you pls put it for me, how you would do it the right way without the prototypes, just define the function, I want the function to subtract x and y!

    and then just call it

    subtract() like you call printf()

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    x and y in main and x y in subtract are two different sets of variables.
    Code:
    subtract(x,y);

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    For example, let's take printf, it is a function in the stdio.h! You do not have to put return or prototypes!You just call the function, give its arguments and that's it!
    You're confused. The actual compiled printf() funciton is in a library file somewhere. What you find in stdio.h is actually, that's right, the prototype.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by cookie View Post
    For example, let's take printf, it is a function in the stdio.h! You do not have to put return or prototypes!You just call the function, give its arguments and that's it!
    I don't understand your problem. The function prototype for printf() is inside stdio.h. When you #include <stdio.h>, that prototype is included.

    You might be able to get away with the following code on your compiler and just include printf()'s prototype without including stdio.h.:

    Code:
    int printf(const char *, ...);
    
    int main(void)
    {
    	printf("Hello, world....\n");
    	return 0;
    }
    I wouldn't recommend doing it regularly, though, but just as a test in this case. I would strongly recommend you include stdio.h when wanting to use any of the functions contained declared inside.

    Quote Originally Posted by cookie View Post
    Can you pls give me an example, how can u use a function like subtract, let's say made by you ?
    Let's say I want to make the function subtract that will subtract y from x

    Is it possible to do this???

    Code:
    #include <stdio.h>
    
    int subtract(int x, int y)
    {
        printf ("&#37;d", x - y);
    }
    int main()
    {
    	int x;
    	int y;
        scanf("%d", &x);
        scanf("%d", &y);
    
        subtract();
    }
    I tried it but it doesn't work!
    Add this line after you include stdio.h:

    Code:
    int substract(int, int);
    Quote Originally Posted by cookie View Post
    It tells me too few arguments for subtract in an error message!
    That's because the standard idea for a C89 compiler to do is guess you want a function that accepts one int and returns an int if you use a function it hasn't seen before.

    Quote Originally Posted by cookie View Post
    Can you pls put it for me, how you would do it the right way without the prototypes, just define the function, I want the function to subtract x and y!

    and then just call it

    subtract() like you call printf()
    You can't/shouldn't eliminate function prototypes. As C99 takes over, I believe you'll get in trouble with this because function prototypes are required.

    If you want to be like printf(), make your own header file. Name it something like mysubtract.h. Write the following inside of it:

    Code:
    int subtract(int, int);
    Then make a program called mysubtract.c, where this line is included:

    Code:
    #include "mysubtract.h"
    Inside mysubtract.c, write the function definition for subtract().

  5. #5
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Ok!

    So I define the prorotype:
    int add(int x, int y);

    this has to be before main! Why do I have to put this too! int add(int, int) why don't I just leave it with the prototype???

    Now another thing!

    The function definition! Why does it have to be after main???

    And one more thing! When I define the function, do I just call its name or its parameters too!

    Like this
    Code:
    int add (int x, int y)
    {
        printf("%d", x + y);
    }
    Also what is with that return thing!

    why do I have to put
    return x + y ;

    They are really confusing!

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Re-read the book's description on functions. You may have to read this a few times if you've never encountered functions in math or anywhere else before.

    Everything about return and such should be explained.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > this has to be before main! Why do I have to put this too! int add(int, int)
    > why don't I just leave it with the prototype???
    The prototype is the envelope, the function body is the letter inside the envelope. You need both if you want something to happen.

    > The function definition! Why does it have to be after main???
    It can be anywhere after the prototype.
    Some people prefer to see main() first, then the worker functions in a top-down arrangement.
    Others prefer a bottom-up style starting with the minor functions finishing with main at the end of the file.
    However, when your programs no longer fit inside one source file, the whole "before or after main" doesn't mean a great deal.

    > And one more thing! When I define the function, do I just call its name or its parameters too
    When you prototype a function, you can omit the variable names. So these two are equivalent.
    Code:
    int find ( const char*, const char* );
    int find ( const char*haystack, const char*needle );
    Though obviously the latter provides more information about what the parameters intend to be used for.

    The function definition must always name the parameters, otherwise the function body has nothing to work with.
    Code:
    int find ( const char*haystack, const char*needle )
    {
      return strstr( haystack, needle ) != 0;
    }
    Normally, the prototype is created from the definition by copy/paste and appending a ;

    Calling the function, you can use any variable name you like, or even literal constants
    Code:
    char a[] = "hello", b[]="he";
    find( a, b );
    find( "hello", "he" );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Thank You!

    I understand it all with the prototypes and functions but one more thing remains!

    in the function:

    Code:
    int mult (int one, int two)
    {
        return one * two;
    }
    Is return one * two the same thing as if I would put:
    printf("%d\n", one * two);

    The thing I still am confused on is the return statement of a function, what do I use it for??

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    the return value goes where the function was called.
    Is return one * two the same thing as if I would put:
    printf("&#37;d\n", one * two);
    no.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    These do much the same thing
    Code:
    void mult ( int a, int b );
    int main ( ) {
      mult( 3, 6 );
      return 0;
    }
    void mult ( int a, int b ) {
      printf( "Result=%d\n", a * b );
    }
    Code:
    int mult ( int a, int b );
    int main ( ) {
      printf( "Result=%d\n", mult( 3, 6 ) );
      return 0;
    }
    int mult ( int a, int b ) {
      return a * b;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    This might clear it up:

    Without a function:
    Code:
    #include <stdio.h>
    int main(void)
    {
      printf("&#37;d\n", 5 * 3);
      return 0;
    }
    With a function:
    Code:
    #include <stdio.h>
    int mul(int one, int two)
    {
      return one * two;
    }
    
    int main(void)
    {
      printf("%d\n", mul(5, 3));
      return 0;
    }
    Last edited by itsme86; 06-19-2007 at 11:16 AM.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    As the compiler is compiling your code it comes across function calls within said code. When it gets to these function calls, the compiler has to do type checking of arguments passed into the functions (making sure you don't try to pass a char* into a function that expects a float for example). In order to do this type checking, it must have already seen that function earlier, either the complete function or a prototype. The prototype is there to declare to the compiler that you will be using a function named X and it has parameter(s) Y of type(s) Z so that this type checking can occur as the compiler is checking your code for mistakes and comes upon a call to function X. The compiler will say "ah, function X... I've seen this before and I know what arguments it takes and what their types are supposed to be so I can now check and make sure the programmer isn't trying to do something stupid."

    You don't need a prototype at all if you've already defined the function in full prior to it's first call from elsewhere in the code... the compiler is able to take the same information it needs for later type-checking from a full definition of the function as it would get from the simpler prototype. Once you start working on programs that contain more than one source file however it's better to have a prototype in a header that can be included by all the source files in case you need to call these functions from the different source files. These headers are mostly just function prototypes and do not contain the function body/code for those function. The actual code for the functions will be in a source file that you create, or a library file that the vendor provides to you and is linked to your program during the linking stage of the executable build process.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Thanks Guys!

    I can understand it now! Functions really aren't hard! But Call by value and character arrays are!

    What is this call by value, what do they mean it stores memory in an imaginary value!

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by cookie View Post
    I can understand it now! Functions really aren't hard! But Call by value and character arrays are!

    What is this call by value, what do they mean it stores memory in an imaginary value!
    Before asking these questions, read the book a few times. Then read another online tutorial on the same subject a few times.

    What they are trying to explain is that if you pass a variable to a function, the function receives a copy of the value of the variable you give it. If you call a function with a variable, and that function changes the variable, the variable will not be changed inside the original function that called this function. If you want it to be changed, then you need to deal with pointers, which I doubt you really covered, yet.

  15. #15
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Yeah, I amreading books and 2 tutorials at the same time!

    But what they do, they expect people to know what to do from their one program!

    For example, if you have the second edition of The C Programming Language book go to section 1,9 They make a program that prints the longest line from the input! And then, they give exercises to write prorgrams that have nothing in common with the program they made!

    And I get confused because they did not teach me in the book how to do it, they say we will cover this in following chapters! Well, I can't go to the following chapters without knowing this right, it will make no sense! As the online tutorials they resume to the basics only and they expect you to be advanced, they put them as a review for programmers to remember or check something! They do not put them for begginers from 0 level to expert level!

    That is why I ask to many questions on the board!

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

Similar Threads

  1. Function prototypes - needed yes or no?
    By steve1_rm in forum C Programming
    Replies: 4
    Last Post: 02-20-2009, 07:39 AM
  2. Class prototypes
    By m00se123 in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2002, 02:06 AM
  3. Is there a difference between these prototypes?
    By Captain Penguin in forum C++ Programming
    Replies: 5
    Last Post: 06-11-2002, 10:28 AM
  4. function prototypes and call statements.
    By mutu in forum C Programming
    Replies: 0
    Last Post: 04-04-2002, 11:39 PM
  5. Default values in function prototypes
    By wdicks in forum C Programming
    Replies: 13
    Last Post: 10-10-2001, 01:06 AM

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