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

How to call a procedure with two or more given parameters

How to call a procedure with two or more given parameters

How to call a procedure with two or more given parameters

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

RE: How to call a procedure with two or more given parameters

Next time post the code between [code] and [/code] 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

RE: How to call a procedure with two or more given parameters

(OP)
Ok, thanks.

RE: How to call a procedure with two or more given parameters

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

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