INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Newbie Question about procedures

Newbie Question about procedures

(OP)
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 ?

RE: Newbie Question about procedures

Hi

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

CODE --> proc-sample.tcl

proc foo {} { puts one }

foo

proc foo {} { puts two }

foo

proc foo {} { puts three }

foo 

CODE --> command-line

master # tclsh proc-sample.tcl]
one
two
three 

Feherke.
feherke.github.io

RE: Newbie Question about procedures

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

RE: Newbie Question about procedures

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 --> function-sample.sh

select action in 'Load function' 'Call function'; do
  case "$action" in
    'Load function') . 'function-definition.sh' ;;
    'Call function') go_function_go ;;
    *) break ;;
  esac
done 

CODE --> function-definition.sh

go_function_go()
{
  echo 'Hello World !'
} 

CODE --> command-line

master # bash function-sample.sh
1) Load function
2) Call function
#? 2
function-sample.sh: line 5: go_function_go: command not found
#? 1
#? 2
Hello World ! 

Feherke.
feherke.github.io

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close