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


#1



https://www.codecademy.com/courses/python-beginner-en-4XuFm/0/5?curriculum_id=4f89dab3d788890003000096

board = list()
for i in range(0,5):
board.append(["O"]*5)

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


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).

Thanks!
Hoa


Replace this line with your code.


#2

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


#3

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