#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Manila, Philippines
    Posts
    32
    Rep Power
    2

    Functions/Methods and Their Parameters


    Dear guys,

    I have been writing functions/methods in C and Java, but it is still not so clear to me when I should include the parameters or not. Basically, what I do is put them/remove them trial-error style, until one works. Sometimes, it works with the parameters, sometimes it doesn't, sometimes it both works.

    My main question is, when is it essential to include the parameters in the functions/methods?

    If someone could explain this in detail, beginner level, it would be much appreciated. Examples in C or Java are welcome. Thank you very much.
  2. #2
  3. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,095
    Rep Power
    1990
    Originally Posted by kathy025
    My main question is, when is it essential to include the parameters in the functions/methods?
    When they are needed by the function.

    Really, that's all there is to it. If your function has to do some sort of manipulation of some form of data that you give to it, then you will need to pass in parameters. If it doesn't need to do anything with any outside values, then don't pass in any parameters.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    12
    It would help if you had some examples of what you are trying to do.

    That aside, in C functions have definitions either at the top of their source files or in separate header files. These definitions tell you (and the compiler) two critical things: what the function returns and what the function takes as arguments/parameters.

    Most library and language documentation provide function definitions, and any time you can't find library documentation you can just read the header files yourself to see what arguments the functions take and what values they return. For example, check out [this page] from the glibc documentation. Its a list of trigonometry functions in glibc. Its just a list of function definitions and a short explanation of how they work when the definition isn't already totally obvious.

    Java is a bit of a mess, especially for beginners, imo, because OOP complicates data structure (nouns) with functionality (verbs) in a bundling that isn't really indicative of what object orientation was originally all about when it was invented (the days of SmallTalk). I would avoid Java in favor of C for now -- you can learn a little C quickly (and that's all there is of C -- its a tiny language), but Java is a bit of a tar pit (a vast ocean of marketing and academic buzzwords from a decade ago). If you want to learn Java later on its easy to get in to, but a background in C will make nearly any language easy to understand in the future, regardless what "level" the language is.

    So anyway, read the function definitions. They tell you everything you need to know: what the function takes in and what the function puts out. In pure functions that is all there is to things. Impure functions (functions with side-effects) are a special case, but aren't difficult to understand (in principle) once you get a grip on pure functions.

IMN logo majestic logo threadwatch logo seochat tools logo