Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Def and Return Function in python

I’m having some problems with the def and return function in Python.
On the top of my program I’ve defined:

from subprogram import subprogram

then I’ve defined the def in which I’ve included the values I wanted to be returned:

def subprogram(ssh, x_off, y_off, data_array, i, j):
    if j==1:
        print('shutdonw X')
        # Run command.
        ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(x_off)
        var_colonna_1 = data_array[i][j]
        return var_colonna_1
        print(var_colonna_1)
    
    if j==2:
        print('shutdown Y')
        # Run command.
        ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(y_off)
        var_colonna_2 = data_array[i][j]
        return var_colonna_2

this is then called in the main program as:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for j in range(5, lunghezza_colonna):
  if data_array[i][j] == 'V':
      subprogram(ssh, x_off, y_off, data_array, i, j)
print(var_colonna_1)

I was expecting that every time the subprogram is called, it returns the var_colonna_1 or var_colonna_2… But the values I see when I print var_colonna_1 is always 0 even if, internally the other commands are execute (so X and Y are set in shut down).
Can you help me? I don’t see my coding mistake.

>Solution :

The function doesn’t return the variable, only the value on it.

If you want to get the returned value on var_colonna_1, you should asign it, as Sayse said, you should do:

var_colonna_1 = subprogram(ssh, x_off, y_off, data_array, i, j)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading