When using 'return' in build in function, when not?



board = list()
for i in range(0,5):

def print_board(board):
for row in board:
print row

HI, I am confusing again between when i should use 'return' and should not use 'return' in built in function.
The code line above in Battleship assignment, the output error when i used 'return row' and 'print print_board(board).


Replace this line with your code.


return literally means your function hands, something back, so if further result with the output of the function are needed, return is a good choice

in this case, the purpose of your function is to print all rows in board. Using a return keyword causes the function to end, so using return in your function would mean your loop ends in its first run/iteration, not all rows would be printed

when to use what, depends on the purpose and design of your program


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.