DEV Community

wallWrecker
wallWrecker

Posted on

Should i end my function code, with a return?

When i pick my project, i do write it with functional programming so, Is it need to end your function code with a return?

Top comments (4)

Collapse
 
sargalias profile image
Spyros Argalias

There isn't really a short answer to this question. It depends on what the purpose of your function is, which depends on how you're structuring your project, which also depends on whether using primarily an FP style or an OOP style in your project. So it depends.

In functional programming, most functions will be pure, so they generally return something, so they should end with return something. Functions with side effects probably won't return something. This also follows the command-query separation principle.

But if you're asking whether you should return in a function or just let it implicitly return undefined on its own, then it's more common to let the function return undefined implicitly and not use a return statement.

Collapse
 
wallwrecker profile image
wallWrecker

Thank you.

Collapse
 
juancarlospaco profile image
Juan Carlos

Some programming languages use automatic implicit return, like Nim and Rust.

Collapse
 
dallascao profile image
dallascao

In Javascript, we also use functions as a closure. It's like a procedure rather than a function and I usually don't return things.