Bar Akiva
Bar Akiva

Reputation: 1209

Best practice for returning a variable that exists in global scope?

Is the return statement in here unnecessary?

var fahrenheit;
var celsius;
function cToFConvert () {
       celsius = temperatureInput.value;
       fahrenheit = celsius * (9/5) +32;
       console.log(fahrenheit);
       return fahrenheit;
}

I can get the fahrenheit value even if I do not use the return statement. Does that mean using a return is redundant if the variable was declared in global scope?

Upvotes: 1

Views: 2209

Answers (6)

Juan Cortés
Juan Cortés

Reputation: 21102

Avoid working with variables outside of the method, that would be my suggestion. Pass the variables to the method as parameters and return the result. If you still want to work with the variables outside the method, then the return is redundant because you're already modifying the variable inside the method.

function cToFConvert(celsius) {
  return celsius * (9 / 5) + 32;
}
var celsius = 123; //temperatureInput.value;
var farenheit = cToFConvert(celsius);

console.log(farenheit);

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386654

Function and pure function.

The concept to keep a function pure is the idea to use the function without side effects. This means, the function should rely on own parameters and return something which is related to the parameters only.

That means a function should have some input and and some output.

In this case for converting a temperature, you have a value Celsius and want to get the value in Farenheit. This is a good example how to write a function which is resusable for any purpose and could be inserted into a library without changes.

How does it work?

You may think of an input and an output based on the input.

function convertCelsiusToFarenheit(celsius) {
    return celsius * 9 / 5 + 32;
}

Now you can use the function with a wanted input and store the output to a variable

var myFarenheit = convertCelsiusToFarenheit(temperatureInput.value);

Or if you like to convert a bunch of values, you could use the function as callback

var myFarenheitData = [-10, 0, 10, 20, 30, 40].map(convertCelsiusToFarenheit);

With this in mind, its is easier to write a multipurpose function.

Upvotes: 6

A.T.
A.T.

Reputation: 26312

var farenheit=0, celsius=13; //definded in global space
function cToFConvert () {
       farenheit = celsius * (9/5) +32; //using global variables, no need to return
}
function checkFarenheit(){
  alert(farenheit); //checking global variable.
}
cToFConvert();
checkFarenheit();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Note: this is not at all good practice as it will be more error prone, use promises or callbacks methods.

Upvotes: 0

Teja
Teja

Reputation: 1254

Though it is redundant here, both of the approaches differ.

Case 1: You do not use return.

Here, the calling method doesn't expect a value.

Eg. var y=cToFConvert();

`y` will be `undefined`.

Case 2: When you use return.

The calling method expects a value to be returned.

Eg. var y=cToFConvert();

y will have the value farenheit.

Over the two, I personally prefer Case 1, because by looking at the function call, you can clearly say what the function returns. Also as mentioned in other answer, do not use Global variables unless there is no way out.

Upvotes: 0

Suren Srapyan
Suren Srapyan

Reputation: 68665

DON'T USE GLOBAL VARIABLES.

var farenheit;
var celsius;
function cToFConvert () {
       celsius = temperatureInput.value;
       farenheit = celsius * (9/5) +32;
       console.log(farenheit);
       return farenheit;
}

You can remove the return statement. No it is not necessary. Why you use global variable, you will have access to it anywhere and can change it's value anywhere.

For alternate you can do

function cToFConvert(celsiusValue){
  return celsiusValue* (9/5) +32;
}

And call like

var fahrenheit = cToFConvert(temperatureInput.value);

Upvotes: 2

ankurjhawar
ankurjhawar

Reputation: 315

Here I think is reductant, but I think this would not be a valid implementation either. Instead you can do:

var cToFConvert = function() {
var celsius = temperatureInput.value;
var farenheit = celsius * (9/5) +32;
console.log(farenheit);
return farenheit;
}

and then

var convertedValue = cToFConvert();

Hope this helps!

Upvotes: 0

Related Questions