Like Tree2Likes
  • 2 Post By Elkvis

interesting function parameter (for a noob)

This is a discussion on interesting function parameter (for a noob) within the C++ Programming forums, part of the General Programming Boards category; Hey all, I have this function to make and it has to be like this. Code: int ReadScore( string prompt ...

  1. #1
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38

    interesting function parameter (for a noob)

    Hey all, I have this function to make and it has to be like this.
    Code:
    int ReadScore( string prompt )
    What would the reason of having a prompt parameter?

    Thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    So that you could err... prompt the user for input, yet be able to reuse the function with different wording for the prompt.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    But can't you already do that without a parameter?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,403
    Quote Originally Posted by XNOViiCE View Post
    But can't you already do that without a parameter?
    you certainly can, but if you don't want your code littered with stuff like this:
    Code:
    std::cout << "Enter a number:";
    int score = ReadScore();
    when you could instead have this:
    Code:
    int score = ReadScore("Enter a number:");
    it keeps the code a bit more readable (in my opinion), and you know exactly what input the prompt is for.

  5. #5
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    I see! Thanks! :-D

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,403
    keep in mind that the snippet you provided passes the prompt string by value, and this may not be desirable, if the prompt string is very long. it's best to pass by (const) reference so that no copy is made, saving CPU time and memory.

    like this:
    Code:
    int ReadScore(const string& prompt)
    antred and stahta01 like this.

  7. #7
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    I would if I could but this is a homework assignment. :b but thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function parameter/ function argument
    By codecaine_21 in forum C Programming
    Replies: 2
    Last Post: 09-24-2010, 08:09 PM
  2. how to add a parameter to the function
    By jayfriend in forum C Programming
    Replies: 5
    Last Post: 01-16-2007, 08:31 PM
  3. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  4. Interesting virtual function problem.
    By Sebastiani in forum C++ Programming
    Replies: 12
    Last Post: 09-02-2003, 10:08 PM
  5. Parameter in a function
    By cpluspluser in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2003, 07:48 PM

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