It might because in function rgaussian11 the variables mean and sigma are local variables, however, a and b are defined in the function body and therefore global, so your values outside and inside the function get mixed up...
By default variables in awk are global. The problem is that in the 'body' a is assigned 5, and from appearances the value of a should still be 5 when rgaussian11(a,b) is invoked. However, rgaussian1() sets a and thus it is not.
In awk, it is possible to declare a variable as local to a function. It is done by defining it as an argument, and convention is to separate the local variables with a tab to make it more obvious that the function is not expecting them to be passed in. For instance, your function should be defined like this:
This declares a,b,c and r1 as local variables. If this is done, the value of a in the 'body' of the programme is unchanged.
---------- Post updated at 17:16 ---------- Previous update was at 17:13 ----------
As an afterthought, you might declare your other function in a similar fashion:
Declaring both variables a and b as local to the function.
That's what I want them to be, local to the function as you say. I thought that as I do not want to return their values, I would not include them as function parameters.
---------- Post updated at 04:30 PM ---------- Previous update was at 04:19 PM ----------
Thanks a lot. I did not know this detail of how to define local variables in functions, by declaring all local function variables in the formal declaration of the function.
That's what I want them to be, local to the function as you say. I thought that as I do not want to return their values, I would not include them as function parameters.
What you return, using the 'return' statement has nothing to do with the values that are declared as parameters in the function header. Anything referenced in the header is considered local and changes to those variables does not affect the global copy of a variable with the same name.
Consider this small example:
When executed, the output is this:
The variable 'e' is not referenced in the function header and thus remains 9 after the call to the function demo(). Variable 'a', even though changed in the function, has the original value after the call, as do b, c and d.
One of the most common, and frustrating, mistakes when writing awk programmes is to invoke a function from inside a for loop using 'i' as the index variable only to have it changed unintentionally by the called function.
---------- Post updated at 17:37 ---------- Previous update was at 17:35 ----------
Oops -- looks like we crossed our posts, extra explanation never hurts though
Hi,
I am having script which contains many functions. Need to print each function name at the starting of the function. Like below,
functionname()
{
echo "functionname"
commands....
}
I've tried like below,
func=`grep "()" scriptname | cut -d "(" -f1`
for i in $func
do
nawk -v... (4 Replies)
hey guys, im trying to learn bourne shell atm and I'm having some issues with functions.
so heres my code:
#!/bin/bash
##functions
memory () {
free -m
}
space () {
df -h
}
ip () { (5 Replies)
Hello Unix experts,
If I could get any explanations on why the code below doesn't work it would be great !
My input looks like that ("|" delimited):
Saaaaabbbbbccccc|ok
Sdddddfffffggggg|ok
The goal is, if $2 is "ok", to remove everything before the pattern given in the match function... (5 Replies)
Hi, I have a file like this
SPF_HC00001|iCalcular_Monto_Minimo|--->|SPF_HC00028|pstcObtener_Monto_Minimo
SPF_HC00004|iCalcular_Incrementos|--->|SPF_HC00032|pstcObtener_Num_Incrementos
SPF_HC00005|iCalcular_Articulo_167_Reformado|--->|SPF_HC00031|pstcObtener_Por_CB_Inc... (2 Replies)
I need to detect the number of pages in a print job when it is available so I can warn users when they try to print a report much larger than they expected. Sometimes they are trying to print 1000 page reports when they thought they were getting a 10 page report.
Under linux I am scanning the... (5 Replies)
Hi All,
I am using splice function in for loop to delete particular element from array with one condition.
my $cnt=0;
foreach my $elem (@result)
{
if (condition){
splice(@result, $cnt, 1);}
else{
$cnt++;}
}
Now when in array, two elements comes sequentially with the... (3 Replies)
Actually I got a list of file end with *.txt
I want to use the same command apply to all the *.txt
Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics.
For example:
I got the file below:
file1.txt
file2.txt
file3.txt... (4 Replies)
Hello,
How to execute a call back function after aio_write() or aio_read() in Sun Solaris 5.7?
I have filled the control block struct aiocb as follows:
aio_sigevent.sigev_signo = SIGEV
aio_sigevent.sigev_notify = SIGEV_THREAD
Then I have filled the call back function in ... (0 Replies)