passing variable to function

Hi,

I am trying to sum up numbered columns and in order to tidy up the program I have wrote a function to do the adding of some numbers. I have a problem though with passing a variable to the function in the UNIX bash shell. The function only gives the first number in the variable list and does not add the rest. Can a variable not be passed to the function or am I missing something quite obvious here. The function that I wrote and the function call is shown below;

function add_num () {
sum=0
for NUM in $1
do
sum=`expr $sum + $NUM`
done
}

I call this function by function_name Variable_name as so:
add_num $column1
add_num $column2

Very puzzled,
Knotty.

This works...

add_num () {
for NUM in $1
do
sum=`expr $sum + $NUM`
echo sum $sum
done
}

sum=0
add_num 1
add_num 2

and gives this result...

sum 1
sum 3

please use code tags for code

$1 means the first argument.

function add_num () {
sum=0
for NUM in $@
do
sum=`expr $sum + $NUM`
done
}

Yeah thanks reborg. I was thinking that there was something that I was doing wrong. The positional parameter referencing thinglymijig. Thanks again.

If you want a running total that is increased with every call to the function, don't reset sum to 0:

add_num()
{
  sum=${sum:-0} ## set to 0 only if sum is empty
  for num
  do
    sum=$(( $sum + $num ))
  done 
}

That assumes that the contents of your variable is something like:
column1="12 34 56 78 90"

If you want to add columns in a file, use awk:

awk '{
   column1 += $1
   column2 += $2
}
END {
  print "Column1: " column1
  print "Column2: " column2
}' FILE