Trying to implement count_collatz_step function

Can anyone give me a clue why this code doesn't work as expected? The function count_collatz_step() take one parameter which a number that need to calculate collatz until it reaches 1. The func should return steps it takes.

# 
# Count steps of collatz conjecture takes, until the number reach 1.
#

count_collatz_step() {
   step=0
    if [ $1 % 2 == 0 ]
    then 
        $1=$(($1/2))
        step=$((step + 1))
    else
        $1=$((3*$n+1))
        step=$((step + 1))
    fi
    echo $step
}

echo "Input start point: "
read -n start
echo "Input end point: "
read -n end

count=0

while [ $start -lt $end ]
do
    echo $start + ":" + count_collatz_step $start
    if [ $count % 7 -eq 0 ]
    then 
        echo "\n"
    fi
    start=$((start+1))
    count=$((count+1))
done

I am assuming this is homework but IF NOT then:-

Take a look at using $1 in your script, (assuming the shell is bash).

This is not homework. I actually implemented this in C and want to make it work on Bash. I just started to learn Bash a few days ago.

Hi Bunching,
Please show us your working C code. I'm guessing that you have misunderstood how echo and read work in bash and that your function might want to return a value rather than print a value, but without a description of what you're trying to do (rather than just a statement that your shell script isn't working), it is hard to guess how it should be corrected. But, one clear thing is that there is nothing in your code that invokes your function. (You print the name of the function as one of the arguments to one of your echo statements, but you do not call the function.)

Wisecracker,
Note that in a bash function, $1 is the 1st command line argument passed to the function; not a command line argument passed to the script containing the function definition.

Yes but would this part still work, inside and/or outside of a function?

 $1=$(($1/2)) 

No, because you cannot assign a value to a positional parameter this way. What you can do is

set $(($1/2))

but only if you can accept to loose the current value of $1 and all other positional parameters ($2, $3, ...), if present.

Hello folks, this is my working C code. I'd like to translate this code into bash.

#include <stdio.h>

int count_collatz_steps (int number) {
	
	int step = 0;
	
	while (number != 1) {
		if (number % 2 == 0) {
			number = number/2;
			++step;
		} else {
			number = 3*number + 1;
			++step;
		}
	}
	
	return step;
}

int main(int argc, char *argv[]) {
	
	int start;
	int	end;
	int column = 0;
	int i;
	int count = 1;
	
	printf("Enter a starting point: ");
	scanf("%d", &start);
	printf("Enter an ending point: ");
	scanf("%d", &end);
	
	while (start < end && start > 1 && end < 1000 && end > start && end < 10000) {
		printf("%2d:%d \t", start, count_collatz_steps(start));
		if ( count % 7 == 0) {
			printf("\n");
		}
		++count;
		++start;
	}
	
	return 0;
}

Bash:
The only thing that bug is how do I call a function with argument in Bash?

count_collatz_step() {
   step=0
   num=$1
    if [ $num % 2 == 0 ]
    then
        $num=$(($num/2))
        $step=$((step + 1))
    else
        $num=$((3*$num+1))
        $step=$((step + 1))
    fi
    return $step
}

echo "Input start point: "
read start
echo "Input end point: "
read end

count=0

while [ $start -lt $end ]
do
    echo $start + ":" + count_collatz_step $start
    if [ $count % 7 -eq 0 ]
    then
        echo "\n"
    fi
    start=$((start+1))
    count=$((count+1))
done

Hi,
Your function in bash return always 1 (loop while is missing),here a corrected version:

count_collatz_step() {
   step=0
   num=$1
   while [ $num -ne 1 ]
   do
    if [ $((num % 2)) -eq 0 ]
    then
        num=$((num/2))
        step=$((step + 1))
    else
        num=$((3*num+1))
        step=$((step + 1))
    fi
   done
   echo $step
}

echo "Input start point: "
read start
echo "Input end point: "
read end

count=0

while [ $start -lt $end ]
do
    echo $start + ":" + $(count_collatz_step $start)
    if [ $((count % 7)) -eq 0 ]
    then
        echo -e "\n"
    fi
    start=$((start+1))
    count=$((count+1))
done

Regards.

Assigning the parameter to a (local) variable in the function is the right thing to do.
But - you can't use return in bash like you do in e.g. C as it will return an exit status into the $? special parameter, not a value to be used by the caller. Print the value instead.
On the caller's side, you need to execute the function, by either running it on the command line or by using the command substitution mechanism, which also allows to intercept the stdout and use it for e.g. an assignment. Try echo $start + ":" + $(count_collatz_step $start)