Re: about return values



dondora wrote:
hi there
first of all I wanna say thank you for answering my previous questions.
Okay~~!

I made a simple shell script like this below in ksh.

sigma ()
{
yo=$1
sum=0
while (( yo != 0 ))
do
(( sum = sum + yo))
(( yo = yo - 1))
done
return $sum
}

sigma 100
echo $?


but the result was 186 not 5050
I can't understand why it printed out like that. so Could you explain
it to me why it is???


It's already been answered two days ago...

Return values from functions have a limited range, one byte. Their purpose
is to return exit status codes (error codes), not arithmetic values. Thus
the term "function" is to some extent a misnomer; consider shell functions
to be rather programs or procedures.

To return values from functions either use variables (the global variable
REPLY had been suggested) or let the function print the value and use the
form res=$( sigma ) to obtain the result.

Janis
.



Relevant Pages

  • about return values
    ... I made a simple shell script like this below in ksh. ... ((sum = sum + yo)) ... sigma 100 ...
    (comp.unix.shell)
  • an integral expression for the sum of divisors function
    ... analytic function. ... expression for the sum of divisors function that I discovered. ... Here we have a multiple integral expression for sigma ...
    (sci.math)
  • Re: Gaussian Noise Generation
    ... You need to sum up some (finite variance) IID samples before ... sigma * sqrt ...
    (comp.dsp)
  • Re: Gaussian Noise Generation
    ... You need to sum up some (finite variance) IID samples before ... sigma * sqrt ...
    (comp.dsp)
  • Re: C++ Bounds Checking
    ... Depends on what you passed SUM() as the argument. ... that allows for runtime checking of uninitialized data similar to bounds ... write sigma ... end program sum1 ...
    (comp.lang.fortran)