Thread: interesting function parameter (for a noob)

  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
    28,413
    So that you could err... prompt the user for input, yet be able to reuse the function with different wording for the prompt.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    3,445
    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
    3,445
    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)

  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, 09: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