The proc Command In TCL
Here we are going to write a file named proccmd.tcl and then we will run it as, tclsh proccmd.tcl
#exponent
proc exp {base pow} {
set val [expr {$base ** $pow}];
return $val;
}
puts "2 raised to the power of 3 is [exp 2 3]";
#factorial
proc fac {num} {
if {$num <= 1} {
return 1;
}
return [expr {$num * [fac [expr {$num - 1}]]}];
}
puts "Factorial of 5 is [fac 10]";
#fibonacci
proc fib {num} {
set num1 0;
set num2 1;
puts $num1;
puts $num2;
for {set idx 2} {$idx < $num} {incr idx} {
set nxt [expr {$num1 + $num2}];
puts $nxt;
set num1 $num2;
set num2 $nxt;
}
}
puts "10 numbers in a fibonacci series:";
fib 10;
Happy reading and thank you. Meet you soon.
Comments
Post a Comment