Ask coding questions

← Back to all posts
Coding etiquette
ZoeyHunter (0)

Hello, I am working on a code in python with def variable():
and I was wondering if I need to have a return function at the end even if it does not have a parameter and runs fine without it?

Answered by Coder100 (18241) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (18241)

No, the return parameter is only if:

  • you want to exit the function early (if the return statement is called, the rest of the code in the function won't run)
  • you want to return a value.
EpicGamer007 (1736)

what is the purpose of your function? Is it to output something or do you want to get something from it? If you want to get something from it, its best to use return. However, if you only want output, then dont return anything. For example,

def add(x, y): # im not sure if u need to assign types to arguments/parameteres in python
  return x + y
print(add(1, 1)) # -> 2

however, you can do something like

def add_and_print(x, y):
  print((x + y))
add_and_print(1, 1) # -> 2
EmmaGao8 (33)

def variable():
return "Hi"
print(variable())

OUT:
Hi

EpicGamer007 (1736)

def variable():
return "Hi"
print(variable())

OUT:
Hi

@EmmaGao8 this wont do anything lol. you are calling itself but the return comes before it

EmmaGao8 (33)

If you want to get a variable out of it, it is best to use return

EmmaGao8 (33)

What does the variable func do?