Sponsored Content
Full Discussion: Help Help Help in recursion
Top Forums Shell Programming and Scripting Help Help Help in recursion Post 302112612 by Raghuram.P on Thursday 29th of March 2007 05:58:24 AM
Old 03-29-2007
HI,
I have made a small change in your code and it is working fine for me...try it...
factorial()
{
if [ $1 -gt 1 ]
then
y=`expr $1 - 1`
factorial $y
#x=$(( $1 \* factorial $y )) -- I have commented this
x=$(( $1 * $? ))
return $x
else
return 1
fi
}
echo -n "Enter number = "; read n
factorial $n
echo $?

Thanks
Raghuram
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

recursion

I'm using the UNIX csh and i wish to use recursion to nav my way up (or down as it is) a given folder. My little test script is called "r" and takes a folder as argv (or $1) #!/bin/tcsh -f set allFiles = `ls -A $argv` cd $argv while ($#allFiles) if (-d... (1 Reply)
Discussion started by: gsjf
1 Replies

2. Shell Programming and Scripting

recursion too deep

I am running a korn shell script which has a recursive function. The script ran for 117 iterations and ended up with the following error "recursion too deep". what should be done to avert this? Thanks in advance Swamy p.s. I am on UNIX MPRAS V4 (3 Replies)
Discussion started by: swamy455
3 Replies

3. Programming

Recursion

I want to halt a tail recursive function after certain validation. I want to come out of entire recursion without unwinding phase. How can i achieve that . The coding is done in C language. (5 Replies)
Discussion started by: joshighanshyam
5 Replies

4. Shell Programming and Scripting

recursion script problem

Hi Guys,, I tried to create a recursive function in unix. The following is the code. #/bin/sh function(){ n=$1; if ; then out=1; echo "inside if for 0"; else out = `$n * function "$n-1"`; echo "inside if for $n-1; fi (3 Replies)
Discussion started by: mac4rfree
3 Replies

5. Programming

C Recursion (explain)

Hi, Question: how come the output is like that? Can explain to me abit. I am learning C. Thanks! #include <stdio.h> #include <string.h> void printit(char line_of_char, int index); int main() { char line_of_char; int index = -1; strcpy(line_of_char, "This is a string."); ... (5 Replies)
Discussion started by: seede
5 Replies

6. Shell Programming and Scripting

script recursion

Can someone please explain me why the following script calls it self recursively: #!/bin/bash echo Called $0 while this not: #!/bin/bash echo Called $($0) Thanks (6 Replies)
Discussion started by: superpointer
6 Replies

7. UNIX for Advanced & Expert Users

Recursion list for rm -R in find

In the following command: find / -ctime +3 -exec rm -R {}\; how is the recursion list built for the actual rm ? F'rinstance; I had a case where a user typed this as root using '/' instead of '.' so everything in the root level was going to be traversed. They hit <ctrl>C before too much was... (5 Replies)
Discussion started by: port43
5 Replies

8. Shell Programming and Scripting

Bash variable recursion

Not sure how to ask this question. I want concatenate strings and variable recursively into new variable. For example: infile01=/dir/subfolder/file01.txt infile02=/dir/subfolder/file02.txt infile03=/dir/subfolder/file03.txt for i in {01..03} do u=${"infile"$i} echo $u doneI got error... (7 Replies)
Discussion started by: yifangt
7 Replies

9. Solaris

BIND 9, disable recursion

Hi, I am trying to disable the recursion on DNS server (Solaris 10). I have added the lines in the named.conf as below: allow-query-cache { none; }; recursion no; Then restarted the solaris DNS services svcadm refresh svc:/network/dns/server:default Still I am able to... (0 Replies)
Discussion started by: snchaudhari2
0 Replies

10. UNIX for Beginners Questions & Answers

Copy directory withOUT recursion

Hi, I cannot find a way to copy a directory to another location with all attributes (mode, ownership, timestamps) but withOUT recursion (after so many years of working with Linux). Say I want to create /home/jail/tmp exactly like /tmp but with nothing in it. Here is what I tried: ... (7 Replies)
Discussion started by: chebarbudo
7 Replies
return(1T)						       Tcl Built-In Commands							return(1T)

__________________________________________________________________________________________________________________________________________________

NAME
return - Return from a procedure SYNOPSIS
return ?-code code? ?-errorinfo info? ?-errorcode code? ?string? _________________________________________________________________ DESCRIPTION
Return immediately from the current procedure (or top-level command or source command), with string as the return value. If string is not specified then an empty string will be returned as result. EXCEPTIONAL RETURN CODES
In addition to the result of a procedure, the return code of a procedure may also be set by return through use of the -code option. In the usual case where the -code option isn't specified the procedure will return normally. However, the -code option may be used to generate an exceptional return from the procedure. Code may have any of the following values: ok (or 0) Normal return: same as if the option is omitted. The return code of the procedure is 0 (TCL_OK). error(1) Error return: the return code of the procedure is 1 (TCL_ERROR). The procedure command behaves in its calling context as if it were the command error result. See below for additional options. return(2) The return code of the procedure is 2 (TCL_RETURN). The procedure command behaves in its calling context as if it were the command return (with no arguments). break(3TCL) The return code of the procedure is 3 (TCL_BREAK). The procedure command behaves in its calling context as if it were the command break. continue(4) The return code of the procedure is 4 (TCL_CONTINUE). The procedure command behaves in its calling context as if it were the command continue. value Value must be an integer; it will be returned as the return code for the current procedure. The -code option is rarely used. It is provided so that procedures that implement new control structures can reflect exceptional condi- tions back to their callers. Two additional options, -errorinfo and -errorcode, may be used to provide additional information during error returns. These options are ignored unless code is error. The -errorinfo option specifies an initial stack trace for the errorInfo variable; if it is not specified then the stack trace left in errorInfo will include the call to the procedure and higher levels on the stack but it will not include any information about the context of the error within the procedure. Typically the info value is supplied from the value left in errorInfo after a catch command trapped an error within the procedure. If the -errorcode option is specified then code provides a value for the errorCode variable. If the option is not specified then errorCode will default to NONE. EXAMPLES
First, a simple example of using return to return from a procedure, interrupting the procedure body. proc printOneLine {} { puts "line 1" ;# This line will be printed. return puts "line 2" ;# This line will not be printed. } Next, an example of using return to set the value returned by the procedure. proc returnX {} {return X} puts [returnX] ;# prints "X" Next, a more complete example, using return -code error to report invalid arguments. proc factorial {n} { if {![string is integer $n] || ($n < 0)} { return -code error "expected non-negative integer, but got "$n"" } if {$n < 2} { return 1 } set m [expr {$n - 1}] set code [catch {factorial $m} factor] if {$code != 0} { return -code $code $factor } set product [expr {$n * $factor}] if {$product < 0} { return -code error "overflow computing factorial of $n" } return $product } Next, a procedure replacement for break. proc myBreak {} { return -code break } SEE ALSO
break(1T), catch(1T), continue(1T), error(1T), proc(1T), source(1T), tclvars(1T) KEYWORDS
break, catch, continue, error, procedure, return ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +--------------------+-----------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +--------------------+-----------------+ |Availability | SUNWTcl | +--------------------+-----------------+ |Interface Stability | Uncommitted | +--------------------+-----------------+ NOTES
Source for Tcl is available on http://opensolaris.org. Tcl 7.0 return(1T)
All times are GMT -4. The time now is 11:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy