Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Question about procedures

Status
Not open for further replies.

sbix

IS-IT--Management
Nov 19, 2003
493
CA
Hi experts,
in shell scripts funcion must be defined before the main code invokes them.
i.e. like the following:
#!/bin/sh
sub1 (){
blah blah
}

#
# MAIN
#
sub 1
exit


Is it the same also for TCL ?
 
Hi

Yes. Given that procedures can be redefined anytime, that is the only way it can work.

Code:
[b]proc[/b] foo {} { [b]puts[/b] one }

foo

[b]proc[/b] foo {} { [b]puts[/b] two }

foo

[b]proc[/b] foo {} { [b]puts[/b] three }

foo
Code:
[blue]master #[/blue] tclsh proc-sample.tcl]
one
two
three

Feherke.
feherke.github.io
 
There is sort of an exception when dealing with Tk GUI's. If you invoke a procedure from, say, a button, that procedure doesn't have to be defined in preceding lines. Obviously it must be defined by the time the user pushes the button.

_________________
Bob Rashkin
 
Hi

Correct.

As the question was "Is it the same also for TCL ?", the answer is the same yes. Just as in Tcl, in shell scripts the functions definition has to occur before the call in time, not in space.

Maybe this is a better example :
Code:
[b]select[/b] action [b]in[/b] [green][i]'Load function'[/i][/green] [green][i]'Call function'[/i][/green]; [b]do[/b]
  [b]case[/b] [green][i]"$action"[/i][/green] [b]in[/b]
    [green][i]'Load function'[/i][/green]) . [green][i]'function-definition.sh'[/i][/green] ;;
    [green][i]'Call function'[/i][/green]) go_function_go ;;
    *) [b]break[/b] ;;
  [b]esac[/b]
[b]done[/b]
Code:
go_function_go()
{
  [b]echo[/b] [green][i]'Hello World !'[/i][/green]
}
Code:
[blue]master #[/blue] bash function-sample.sh
1) Load function
2) Call function
[blue]#?[/blue] 2
function-sample.sh: line 5: go_function_go: command not found
[blue]#?[/blue] 1
[blue]#?[/blue] 2
Hello World !

Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top