Issue with return statement in function

Greetings,

I have a function which is called in the following logical order MainProgram, calls function1, which calls function2, which calls function3, where there are other statements after function3 that always will be executed, however when I have a ‘return None’ as last command in the ‘else’ of an if-else statement which is at the end of the function3 itself, function3 executes and exits all the way to starting from the beginning of MainProgram and nothing else is been executed after function3 in function2 or function1 and there are plenty of non conditional statements that need to be executed regardless of function3. ‘return None’ itself is part of an else which is not executed during current conditions, non the less just by existing seems to affect the proper path of the program until removed/commented.

Am I doing something wrong?

Can you post any code? Might help see what’s wrong

Bellow is the complete function3 referred from earlier. Toward the very end of the function, I’ve added comments with numbers (1) and (2), for the two rows of interest. MainProgram calls a function1, which calls function2, which on its side calls function3. IsJoint is False and none of all the present ‘return None’ is executed due to errors. Function executes and when it exits, the program does not continue from function2 which called it, with the remaining code, instead it goes all the way back to the beginning of MainProgram. If I comment # (1) which is the last ‘return None’, or instead add a random line of code like uncommenting # (2) , program follows expected program flow and after exiting function3, program continues with the rest in function2 and then, the rest in function1, as expected.

def CalcExternalPoints():

    #set tarPosition and tarJoint
    if IsJoint:
        tarPosition = get_forward_kin([tarPositionX, tarPositionY, tarPositionZ, tarPositionRX, tarPositionRY, tarPositionRZ])
        tarJoint = [tarPositionX, tarPositionY, tarPositionZ, tarPositionRX, tarPositionRY, tarPositionRZ]
    else:
        tarPosition = p[tarPositionX, tarPositionY, tarPositionZ, tarPositionRX, tarPositionRY, tarPositionRZ]
        if get_inverse_kin_has_solution(tarPosition):
            tarJoint = get_inverse_kin(tarPosition)
        else:
            SetGeneralStatus(Busy = False, Error = True)
            LogMessage("CalcExternalPoints: Error, no valid joint position for tarPosition", popup = True, pTitle = "Error!", pError = True)
            return None
        end
    end

    #set prePosition and preJoint - offset is treated only position wise
    if IsJoint:
        prePosition = get_forward_kin(tarJoint, p[offPositionX, offPositionY, offPositionZ, offPositionRX, offPositionRY, offPositionRZ])
        if get_inverse_kin_has_solution(prePosition, tarJoint):
            preJoint = get_inverse_kin(prePosition, tarJoint)
        else:
            SetGeneralStatus(Busy = False, Error = True)
            LogMessage("CalcExternalPoints: Error, no valid joint position for prePosition", popup = True, pTitle = "Error!", pError = True)
            return None
        end
    else:
        prePosition = pose_trans(tarPosition, p[offPositionX, offPositionY, offPositionZ, offPositionRX, offPositionRY, offPositionRZ])
        if get_inverse_kin_has_solution(prePosition):
            preJoint = get_inverse_kin(prePosition)
        else:
            SetGeneralStatus(Busy = False, Error = True)
            LogMessage("CalcExternalPoints: Error, no valid joint position for prePosition", popup = True, pTitle = "Error!", pError = True)
            return None #(1) last return None with no code following it until the end of the function
        end

    end

    #a = 0 #(2) random local variable assignment
end

Huh. I don’t see anything wrong there. I have noticed in the past some oddities with lines of code getting skipped if they are too close to the end of the program. It’s like the time it takes to execute is longer than the time it takes to Halt, or end the program, so they don’t get executed. I see this often when trying to set a digital output as one of my last instructions before a halt.

Looks like if all you have to do is assign a random var like that, I’d just roll with it. Wish I could tell ya something more than that, but no, I don’t think you’re doing anything wrong.

If you replace #(2) with a return None instead of a variable assignment, does it function properly? Or is it only working properly when you have a variable assignment?

Also fixes the problem. I just gave variable assignment as example because I thought its even more weird.