CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266

    Modification by a function of the value of it's parameter

    This is a theoretical question about languages and development theoretically.

    What do software development theorists say about functions that modify the values of parameters passed to them? Is it bad practice, theoretically speaking? It is of course common for this to be done. In C++, it is very common for pointers to be passed as a parameter, and it is common for parameters to be passed using references.

    JScript does not support the modification of non-object parameter values by functions, and some JScript programmers consider all languages that do to be undesirable old technology. Is C++ and VB old technology? Does Java and C# have equivalent capability or do they not support modification of parameter values by functions?
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Modification by a function of the value of it's parameter

    C# supports it. By default classes are passed by reference. POD types are passed by value, but there is a 'ref' keyword that can be used to pass the POD type by reference.

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Modification by a function of the value of it's parameter

    I think, that theoreticaly it is ok to modify parameters if you work on basis of procedural paradigm.

    C# has the ref (also out) keyword as mentioned, but as far as I know, Java doesn't have similar feature.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  4. #4

    Re: Modification by a function of the value of it's parameter

    I don't know the background to being able to modify parameters sent to a function, but surely it is a by product of passing pointers to functions to prevent you from duplicating memory all over the place.

    Once you pass a pointer, you can modify it, then you're modifying the parameters sent to that function.

    Being a by-product is it a convenient accident that you can then modify those parameters?

    I've no problem with functions modifying parameters as it allows you to be quite creative and flexible with your functions.

    Maybe there needs to be better protection of that memory though? - allowing you to pass pointers to functions, but not modifying them?

    I can't remeber how this would be achieved in C++ as I haven't tried it, but maybe it is possible.

  5. #5
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266

    Re: Modification by a function of the value of it's parameter

    A function's inability to modify parameters seems inefficient and cumbersome. The solution being suggested elsewhere is to return the created or modified data, however JScript (the language being discussed elsewhere) does allow members of objects to be modified. If returning modified data is the solution, then that means that if a megabyte of data is being processed, an additional copy is needed for the modified data.

    I hope a Java expert can contribute here to be clear how it works relevant to this.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  6. #6
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266

    Re: Modification by a function of the value of it's parameter

    Quote Originally Posted by goatslayer
    Being a by-product is it a convenient accident that you can then modify those parameters?
    Many years before C even existed, it was very common for assembler programmers to pass addresses (pointers) to data instead of the data. It is extremely basic to the way that processor instructions work; that is, processor instructions use addresses of data to process the data.

    If you read a good book about C/C++, it will emphasize the efficiency of using pointers and references to pass data to functions. A C++ reference is essentially a pointer except the language is more strict about how references are used. A good book will say to use a reference (or maybe a pointer) to pass large amounts of data, even if the data is not being modified. For C++, usually a reference should be used; usually (not always) a pointer is needed only if interfacing outside of C++, such as in a Windows API function.
    Last edited by Sam Hobbs; September 12th, 2008 at 05:58 AM.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Modification by a function of the value of it's parameter

    I think that what's lurking in the back here is something that was discussed ages ago.

    Back then it was considered bad practice to let a function modify parameters sent to it. I believe the reason behind this was that a function then was only something like sin(). "Functions" where it should be possible to alter the parameters were called 'procedure' (pascal/ada) and didn't have any return value. Also, it was even considered bad practice to 'var' declare a parameter that shouldn't be altered (to avoid call by value for efficiency reasons).
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  8. #8
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266

    Re: Modification by a function of the value of it's parameter

    See Pascal (programming language) - Wikipedia. Pascal is an implementation of structured programming methodolgy. Pascal has pointers and does have functions that return values. Programming "back then" (when Pascal was designed) was not as primitive as you think.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  9. #9
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266

    Re: Modification by a function of the value of it's parameter

    Quote Originally Posted by S_M_A
    "Functions" where it should be possible to alter the parameters were called 'procedure' (pascal/ada) and didn't have any return value.
    Perhaps you are thinking of Basic, which Pascal was designed to replace.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  10. #10

    Re: Modification by a function of the value of it's parameter

    Is anyone else losing the point of this question/discussion and what the OP wants out of it?

    If returning modified data is the solution, then that means that if a megabyte of data is being processed, an additional copy is needed for the modified data.
    This is a disadvantage to not passing pointers to functions.

    As such, use pointers/references if your language allows it, const those parameters if you need to worry about not modifying them.

    And if your language doesn't allow it, there's no problem surely.

  11. #11
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266

    Re: Modification by a function of the value of it's parameter

    Quote Originally Posted by goatslayer
    Is anyone else losing the point of this question/discussion and what the OP wants out of it?
    Thank you for trying to keep this discussion on-topic. Note that I am the "OP" and I am the person that made the comment you are quoting.

    Quote Originally Posted by goatslayer
    As such, use pointers/references if your language allows it, const those parameters if you need to worry about not modifying them.

    And if your language doesn't allow it, there's no problem surely.
    Note that I did not ask for individual opinion; I asked for information about what software development theorists (experts) say. Have you read a book about software development? Or are you an expert yourself that has written an article in an authoritative journal or something such as that? If not, then I would prefer to not fill this discussion with opinions that will make it unlikely that more useful posts will be provided.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  12. #12

    Re: Modification by a function of the value of it's parameter

    Yea, you are right, you asked What do software development theorists have to say about it.

    I'm not currently an academic in the subject nor a writer, I just write the code, so where does that leave me? theorist, probably not. Expert - ? getting there, however subjective that term obviously is. Code Monkey? - I guess I'm a little above that.

    Still.

    1) If a langage allows it, you can pretty much take it for granted that the person who wrote that language has little or no problem with "modification by function of the value of its parameters", just how one might use that ability. I believe those guys are "experts".

    2) If that "opinion" is little or no use to you, have a look at this from Stroustrup, his thoughts on it without batting an eye lid: http://www.research.att.com/~bs/bs_f...l-by-reference

    Hope that helps.

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Modification by a function of the value of it's parameter

    I incorrectly stated earlier that C# allowed modification of parameters by functions. In a strict sense this is incorrect because C# doesn't have the concept of functions. There are class methods, but not functions in C#.

    As far as the discussion (wrt C#), if you don't consider parameter modification within a method to be a good practice consider the alternatives. Would that mean all work must happen within one large method or within a single class? Clearly that isn't going to work out.

  14. #14
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Modification by a function of the value of it's parameter

    Quote Originally Posted by Sam Hobbs
    Perhaps you are thinking of Basic, which Pascal was designed to replace
    No, I've been back then.... programming Pascal... and ADA... A long time ago, some memories remain but obviously not all of them.
    Last edited by S_M_A; September 14th, 2008 at 04:14 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  15. #15
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Modification by a function of the value of it's parameter

    Quote Originally Posted by Sam Hobbs
    Perhaps you are thinking of Basic, which Pascal was designed to replace.
    Actually, Pascal was intended as a theoretical teaching language, Wirth did not have any intentions of implementing it at all. Two of his grad students decided to make an implementation, and they wrote the first compiler in Dartmouth Basic. Of course the first (real) Pascal program they wrote was a Pascal compiler.

    Until I relocated a few years ago (circa 1996), I actually had a hard copy of the original Dartmouth program (green-bar fanfold)......
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured