One of the most useful parts of any programming language is its ability to make segments of code available, that can be called upon to perform a specific task or operation. This is the purpose of procedures. Procedures in Tcl/Tk follow the form:
proc procName {arg1 arg2 argN} {body}
procName Name of the procedure
args Arguments or parameters passed to procedure
body Body of the procedure
The proc command is used for creating both functions and procedures.
What is the difference between a function and a procedure?
The difference is in the return. Functions perform a given task with some form of return value; where as procedures perform a given task with no return value. So remember to look for the return in a procedure. If you see it, then you can determine that the proc is performing as a function. However, absence of the return statement does not automatically imply that no return is occurring in the procedure. We will discuss the return statement and functions in just a bit. For now, let's take a look at a simple example:
######################################
# Written by Nelson
# Example ch4p8.tcl
# This program offers an example of
# the use of procedures.
#
######################################
####################
# Procedure section
####################
######################################################
# Procedure cow collects 2 variables then performs
# as addition operation then returns that computation.
######################################################
proc {cow} {x y} {
return [expr $x+$y]
}
##################################
# Try passing different values
# to the procedure that are caught
# as x and y then examine the output
# for each values you try.
##################################
set bingo [cow 9 3]
if [winfo exists .msg] {destroy .msg}
message .msg -width 15c -text "The return is $bingo"
pack .msg
What should I get from this example?
One point that should be obvious by now is that good notation of a program is very important. Also notice how I have provided a section for procedures to be placed in my program. I prefer the procedures to be placed near the top of the program since this is common in other languages like Pascal. I feel it is an excellent place to see some of the characteristics of a program you might be reading for the first time. In the example ch4p8.tcl the procedure cow collects 2 parameters (x and y). I always like to think of this concept of passing parameters as a football play where the procedure is like a receiver, the procedure call is like a quarterback and the parameters are the football. The receiver is only prepared to catch a football with N parameters so, the quarterback should only pass the correct type of football.
The procedure (receiver):
proc {cow} {x y} {
return [expr $x+$y]
}
The procedure call (quarterback):
set bingo [cow 9 3]
The parameters (football):
9 and 3
Then the return statement sends the results of our [ ] operation back from where the call was made.
Do I always use return?
Procedures need not always return something. They can merely perform an operation. I think this is the time to note that the return statement is not required to get a result returned from a procedure. Try it for yourself by changing:
Change the line that reads:
proc {cow} {x y} {
return [expr $x+$y]
}
To:
proc {cow} {x y} {
expr $x+$y
}
Assignment 4.8
Using the concepts from example ch4p8.tcl, write a program with a procedure
to allow you to fill out the following table:
Table 1
x+y
x%y x/y x*y
x=0
y=7 _____ _____ _____ _____
x=1
y=7 _____ _____ _____ _____
x=2
y=7 _____ _____ _____ _____
x=3
y=7 _____ _____ _____ _____
x=4
y=7 _____ _____ _____ _____
x=5
y=7 _____ _____ _____ _____
x=6
y=7 _____ _____ _____ _____
x=7
y=7 _____ _____ _____ _____
x=8
y=7 _____ _____ _____ _____
x=9
y=7 _____ _____ _____ _____
What did you find interesting from this exercise?
What about patterns?
Table 2
z= (x+y) for each.
x+y+z x%y+z x/y+z x*y+z
x=0
y=7 _____ _____ _____ _____
x=1
y=8 _____ _____ _____ _____
x=2
y=9 _____ _____ _____ _____
x=3
y=10 _____ _____ _____ _____
x=4
y=11 _____ _____ _____ _____
x=5
y=12 _____ _____ _____ _____
x=6
y=13 _____ _____ _____ _____
x=7
y=14 _____ _____ _____ _____
x=8
y=15 _____ _____ _____ _____
x=9
y=16 _____ _____ _____ _____
What did you find interesting from this exercise?
What about patterns?
Table 3
x= (y*y) for each.
z= (x+y) for each.
x+y+z x%y+z x/y+z x*y+z
y=1 _____ _____ _____ _____
y=2 _____ _____ _____ _____
y=3 _____ _____ _____ _____
y=4 _____ _____ _____ _____
y=5 _____ _____ _____ _____
y=6 _____ _____ _____ _____
y=7 _____ _____ _____ _____
y=8 _____ _____ _____ _____
y=9 _____ _____ _____ _____
y=10 _____ _____ _____ _____
What did you find interesting from this exercise?
What about patterns?
Assignment 4.9
Describe how you would address the issue of the last line in the procedure
causing unwanted return?
Develop a snippet of code to address your description:
Assignment 4.10
A.) Write a program that contains a procedure that will create
message widgets. The procedure should receive the widgetName, width,
backgroundColor, textYouWantToDisplay, name the procedure msgCreate .
B.) Write a program that contains a procedure that will create
button widgets. The procedure should receive the widgetName, text4YourButton,
name the procedure buttonCreate .