Thread: function prototype logical explanation?

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    8

    Question function prototype logical explanation?

    In lesson 4 the function protoype is written like this:
    Code:
    int mult ( int x, int y );
    ...
    I do not understand it why int x and int y is in there, I've rewritten the entire function prototype like so:
    Code:
    int func_1(int,int);
    ...
    and it still works exactly as expected? (removing "int,int" breaks it)
    And logical explanation behind this?
    Last edited by unknown000; 08-05-2017 at 04:15 PM.

  2. #2
    Guest
    Guest
    The compiler needs to know the function's parameter types (more specifically, the types' sizes). Naming of parameters is necessary if you need to refer to them in the function body, naturally.
    Last edited by Guest; 08-05-2017 at 04:36 PM.

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    8
    Quote Originally Posted by Guest View Post
    The compiler needs to know the function's parameter types (more specifically, the types' sizes). Naming of parameters is necessary if you need to refer to them in the function body, naturally.
    I can understand why removing "int,int" breaks it but the int "x, int y" thingy doesn't really make any sense to me if you can just type "int, int" with exact same results. Is it like a safer way to do?

  4. #4
    Guest
    Guest
    Maybe not safer, but arguably more self-documenting:
    Code:
    void set_dimensions(int, int); // (width, height)? (height, width)?
    void set_dimensions(int width, int height); // no doubt
    Personally, I never felt the desire to omit them and always use matching parameter names in function prototype and definition.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by unknown000 View Post
    I do not understand it why int x and int y is in there...
    ...
    And logical explanation behind this?
    There are three things connected with creating a function:

    1) Function declaration / prototype
    2) Function definition
    3) Function call

    The function prototype tells the compiler the data type that will be returned from the function, and the parameter list.

    The parameter list tells the compiler:
    1) The number of arguments that will be passed into the function
    2) The order of the arguments that will be passed
    3) The data type of each argument

    Most programmers will usually define the function, possibly changing the formal parameters during it's creation. When finished, the programmer will just copy the first line of the function definition, placing that line at the top of the file or in a header file included by the program, and put a semicolon at the end of the declaration, leaving the parameter names in place. The compiler does not need or require the names at that point, but if the parameter names are well thought out, it does help to self document the function.

    Hope this clarifies this for you.

  6. #6
    Registered User
    Join Date
    Aug 2017
    Posts
    8
    Everyone thanks for explaining, I do understand it better now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  2. Replies: 4
    Last Post: 01-04-2013, 03:10 AM
  3. Function prototype in C
    By Darwin Chan in forum C Programming
    Replies: 4
    Last Post: 01-03-2013, 03:06 AM
  4. function prototype
    By shuo in forum C++ Programming
    Replies: 9
    Last Post: 04-19-2007, 09:25 PM
  5. Replies: 13
    Last Post: 08-24-2006, 12:22 AM

Tags for this Thread