Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7670741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:53:33+00:00 2026-05-31T15:53:33+00:00

I have a question about passing parameters in Tcl regarding to the following code:

  • 0

I have a question about passing parameters in Tcl regarding to the following code:

set name "Ronaldo"

proc GET_PLAYER_INFO {player_id {player_name "$name"}} {
    global name
    puts $player_name
}

regarding to the code above, we have a global variable “name”, and in the parameter list of proc GET_PLAYER_INFO, the default value of parameter player_name is set to “$name”? if the value of name is “ronaldo”, it is already double quotation,do we need to put double quotation in the parameters list like this: player_name “$name”? and before we execute the “global name” command, is the default value of player_name is “Ronaldo”? is so, why we need to have “global name” command in our proc?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:53:34+00:00Added an answer on May 31, 2026 at 3:53 pm

    That won’t work as it stands; the $name won’t be evaluated at all so the default will be those literal five characters.

    If you’re binding the default value at the time you create the procedure, you’d do it like this:

    proc GET_PLAYER_INFO [list player_id [list player_name $name]] {
        ...
    }
    

    That is, the arguments to proc are just normal things you can construct with Tcl commands and substitutions. This is one of the great things about Tcl.

    However, if you’re wanting to evaluate that $name at the time the procedure is called, you’ve got to do it differently. If you’ve got some kind of value that will never be used for the player name (e.g., the empty string) then it’s pretty easy:

    proc GET_PLAYER_INFO {player_id {player_name ""}} {
        if {$player_name eq ""} {
            set player_name $::name
        }
        ...
    }
    

    Note that I’ve used the fully-qualified variable name there. There are other ways to get that name too (e.g., with global, with upvar, with variable, …)

    The place where things get tricky is when you’ve not got a suitable sentinel value at all. At that point, you have to see how many arguments were actually supplied:

    proc GET_PLAYER_INFO {player_id {player_name ""}} {
        if {[llength [info level 0]] == 2} {
            set player_name $::name
        }
        ...
    }
    

    The command info level 0 returns the full list of argument words to the current procedure call. This includes the GET_PLAYER_INFO itself and would be a list of length 2 or 3 in a valid call to the definition above. Once the list is available, checking its length is a trivial exercise in llength and numeric comparison. (Using a sentinel value is easier though, and works in 99.99% of cases.)

    The final option is to use the special args formal parameter and do the parsing manually:

    proc GET_PLAYER_INFO args {
        if {[llength $args] < 1 || [llength $args] > 2} {
            return -code error "wrong # args: should be \"GET_PLAYER_INFO player_id ?player_name?\""
        }
        set player_id [lindex $args 0]
        if {[llength $args] > 1} {
            set player_name [lindex $args 1]
        } else {
            set player_name $::name
        }
        ...
    }
    

    As you can see, this is rather long-winded…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about best practices regarding how one should approach storing complex
I have a question about using os.execvp in Python. I have the following bit
I've a question about passing in parameters via the command line. My main() looks
I have a question about templates that can be used with parameters that are
I have a simple question about passing custom objects via a webservice. I created
There was a question regarding passing taking the following function template, and instantiating it
I'm following a tutorial. (Real World Haskell) And I have one beginner question about
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have question about normalization. Suppose I have an applications dealing with songs. First
I have a question about using streams in .NET to load files from disk.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.