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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call a procedure with two or more given parameters

Status
Not open for further replies.

paskali

Programmer
Apr 6, 2012
13
IT
Hi, i am attempting to write a simple script to sum two given numbers:

#A simple script that sum two given numbers
proc Sum {number1 number2} {
return [expr $number1+$number2];
}
proc main {} {
puts "Sum between two numbers";
puts "";
puts -nonewline "Insert the numbers:"
flush stdout;
?
#sum given numbers
puts "The sum is: [Sum $number1 $number2]";
}
#call main
main;

What might i write in the place of question mark?
And, if i insert a string or a char in the place of numbers, what happens?

Thanks!
 
Next time post the code between [ignore]
Code:
[/ignore][/b] and [b][ignore]
[/ignore]
tags, so it appears like this:
Code:
#A simple script that sum two given numbers
proc Sum {number1 number2} {
    return [expr $number1+$number2]
}

proc main {} {
    puts "Sum between two numbers"
    puts ""

    puts "Insert the numbers:"

    puts -nonewline "number1 = "
    flush stdout
    gets stdin num01

    puts -nonewline "number2 = "
    flush stdout
    gets stdin num02

    #sum given numbers
    puts "The sum is: [Sum $num01 $num02]"
}

#call main
main
Output:
Code:
C:\Users\Roman\Work>tclsh zemir2.tcl
Sum between two numbers

Insert the numbers:
number1 = 1
number2 = 5
The sum is: 6
 
According with previous suggestion, i have changed the scripts to avoid problems with incorrect input (e.g. a char or a string):
Code:
############################################
#A simple script that sum two given numbers#
############################################
proc Sum {number1 number2} {
    return [expr $number1+$number2]
}
proc main {} {
	set number1 " ";
	set number2 " ";
    puts "Sum between two numbers"
    puts ""
	while {![string is integer -strict $number1] || ![string is integer -strict $number2]} {
		puts "Insert the numbers:"
		puts -nonewline "number1 = "
    	flush stdout
    	gets stdin number1
		puts -nonewline "number2 = "
    	flush stdout
    	gets stdin number2
		#check for invalid input (example a char or a string)
		if {![string is integer -strict $number1] || ![string is integer -strict $number2]} {
			puts "Error, repeat again";
		}
	}
	#sum given numbers
    puts "The sum is: [Sum $number1 $number2]"
}
#call main
main
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top