proc(n)

πŸ“¦
version 9.0.3 Β· 2 versions
, in alpine 3.23, alpine 3.24, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

proc(n) πŸ’–

okay bestie, so we're doing proc from the Tcl Built-In Commands set today and honestly? this one is the blueprint. the moment you learn proc you basically unlock Tcl as a language. no cap. ✨


✨ NAME ✨

proc β€” it makes a Tcl procedure. that's it, that's the whole vibe. a brand new command, handcrafted by you. πŸ‘‘


🌸 SYNOPSIS 🌸

proc name args body

three little words and suddenly you're a language designer. iconic.


πŸ¦‹ DESCRIPTION πŸ¦‹

so the proc command creates a new Tcl procedure named name, and ngl it's ruthless about it β€” it replaces any existing command or procedure there may have been by that name. no warning, no "are you sure", just gone. πŸ’€

whenever that new command gets invoked, the contents of body get executed by the Tcl interpreter. that's the whole deal.

normally name is unqualified (meaning it doesn't include the names of any containing namespaces) and the new procedure gets created in the current namespace. but if name does include namespace qualifiers, then the procedure gets created in the specified namespace instead. your call, babe.

now, args β€” that's where it gets juicy. args specifies the formal arguments to the procedure. it's a list, possibly empty, and each element of it specifies one argument. and each of those argument specifiers is also a list, with either one or two fields:

  • if there's only a single field in the specifier, that's the name of the argument. simple. πŸ’…
  • if there are two fields, the first is the argument name and the second is its default value.

here's the chaotic part β€” arguments with default values that are followed by non-defaulted arguments become required arguments. and you need to supply enough actual arguments to allow all arguments up to and including the last required formal argument. like, the vibes are being enforced by the interpreter. 😭

when name gets invoked, a local variable is created for each of the formal arguments to the procedure. its value will be the value of the corresponding argument in the invoking command, or the argument's default value. actual arguments get assigned to formal arguments strictly in order. no skipping, no shuffling, in order. periodt.

arguments with default values don't have to be specified in a procedure invocation. however:

  • there must be enough actual arguments for all the formal arguments that do not have defaults, and
  • there must not be any extra actual arguments.

arguments with default values that are followed by non-defaulted arguments become de-facto required arguments, though this may change in a future version of Tcl. so if you want portable code, you should make sure that all optional arguments come after all required arguments. please. for me. 🫢


🧚 the args special case 🧚

there is one special case to permit procedures with variable numbers of arguments. if the last formal argument has the name args, then a call to the procedure may contain more actual arguments than the procedure has formal arguments.

in that case, all of the actual arguments starting at the one that would be assigned to args get combined into a list (as if the list command had been used), and that combined value is assigned to the local variable args. so args is literally just a list of everything extra. it's giving "catch-all bag of holding". πŸŽ€


πŸ’œ locals, namespaces and all that πŸ’œ

when body is being executed, variable names normally refer to local variables, which get created automatically when referenced and deleted when the procedure returns. one local variable is automatically created for each of the procedure's arguments. no setup needed, they just appear. main character energy honestly.

other variables can only be accessed by invoking one of the global, variable, upvar or namespace upvar commands. that's the guest list, and if you're not on it, you're not getting in. 🚫

the current namespace when body is executed will be the namespace that the procedure's name exists in β€” which will be the namespace that it was created in, unless it has been changed with rename. sneaky little rename. πŸ™ˆ


⭐ what does it even return tho ⭐

the proc command returns an empty string. nothing. silence. aesthetic.

when a procedure is invoked though, the procedure's return value is the value specified in a return command. if the procedure doesn't execute an explicit return, then its return value is the value of the last command executed in the procedure's body. and if an error occurs while executing the procedure body, then the procedure-as-a-whole will return that same error. no covering it up, no repackaging. it's just passed straight through. πŸ’…


🌈 EXAMPLES 🌈

1. this is a procedure that takes two arguments and prints both their sum and their product. it also returns the string "OK" to the caller as an explicit result. look at her go:

proc printSumProduct {x y} {
    set sum [expr {$x + $y}]
    set prod [expr {$x * $y}]
    puts "sum is $sum, product is $prod"
    return "OK"
}

2. this is a procedure that accepts arbitrarily many arguments and prints them out, one by one. args doing exactly what we said it does. slay:

proc printArguments args {
    foreach arg $args {
        puts $arg
    }
}

3. this procedure is a bit like the incr command, except it multiplies the contents of the named variable by the value, which defaults to 2. notice the upvar 1 β€” we're reaching out of our own little local bubble:

proc mult {varName {multiplier 2}} {
    upvar 1 $varName var
    set var [expr {$var * $multiplier}]
}

πŸ‘‘ SEE ALSO πŸ‘‘

go read those next, bestie, they're the supporting cast. 🫢


πŸ’– KEYWORDS πŸ’–

  • argument
  • procedure

ngl if you mess up the ordering of optional vs required args, that's on you, the manpage literally told you. portability is self care. ✨

and that's the whole page, babes β€” written for Tcl's built-in commands, filed under proc(n). mwah, go write some procedures. πŸ’…πŸŒΈπŸ¦‹

πŸ“¦ where this page comes from