# Return Early Pattern for Functions

**URL:** <https://forum.freecodecamp.org/t/return-early-pattern-for-functions/25899>\
**Category:** General\
**Created:** [August 6, 2016, 3:49pm UTC](https://forum.freecodecamp.org/t/return-early-pattern-for-functions/25899 "2016-08-06T15:49:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mangalita](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@mangalita](https://forum.freecodecamp.org/u/mangalita)\
**Post date:** [August 6, 2016, 3:49pm UTC](https://forum.freecodecamp.org/t/return-early-pattern-for-functions/25899/1 "2016-08-06T15:49:35Z")

</div>

this setup

// Setup  
function abTest(a, b) {  
// Only change code below this line

// Only change code above this line

return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));  
}

// Change values below to test your code  
abTest(-2,2);  
See i got see below wrong  
abTest(-2,2) should return undefined  
abTest(2,-2) should return undefined

---

<div class="post-metadata">

**Author:** ![amazigh1989](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/amazigh1989/32/6200_2.png) [@amazigh1989](https://forum.freecodecamp.org/u/amazigh1989)\
**Post date:** [August 6, 2016, 4:58pm UTC](https://forum.freecodecamp.org/t/return-early-pattern-for-functions/25899/2 "2016-08-06T16:58:36Z")

</div>

we want to know more about the problem.

who are the requirements?

a minur observation is that sqrt function cannot contain negative values

---

<div class="post-metadata">

**Author:** ![amazigh1989](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/amazigh1989/32/6200_2.png) [@amazigh1989](https://forum.freecodecamp.org/u/amazigh1989)\
**Post date:** [August 6, 2016, 5:45pm UTC](https://forum.freecodecamp.org/t/return-early-pattern-for-functions/25899/3 "2016-08-06T17:45:36Z")

</div>

the answer is

// Setup  
function abTest(a, b) {  
// Only change code below this line

if(a\<0 || b\<0){  
return;  
}

// Only change code above this line

return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));  
}

// Change values below to test your code  
abTest(2,2);

---

<div class="post-metadata">

**Author:** ![batjko](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/batjko/32/4115_2.png) [@batjko](https://forum.freecodecamp.org/u/batjko)\
**Post date:** [August 6, 2016, 6:08pm UTC](https://forum.freecodecamp.org/t/return-early-pattern-for-functions/25899/4 "2016-08-06T18:08:37Z")

</div>

What @amazigh1989 said.

But just to add: Since apparently the test specifically asks for ‘undefined’ to be returned in those cases, it would be good practice to actually return that ‘undefined’ _explicitly_, i.e.:

```
if(a<0 || b<0){
  return undefined;
}

```

This helps readability of your code. People will know that you really do intend to return `undefined`.

---

<div class="post-metadata">

**Author:** ![Theoask](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/theoask/32/63200_2.png) [@Theoask](https://forum.freecodecamp.org/u/Theoask)\
**Post date:** [August 24, 2017, 9:30pm UTC](https://forum.freecodecamp.org/t/return-early-pattern-for-functions/25899/5 "2017-08-24T21:30:32Z")

</div>

> [@amazigh1989](#):
>
> if(a\<0 || b\<0){

Hi,

Isn’t if(a\<0 || b\<0){  
equivalent to "if ((a || b ) \<0 ) {  
???

---

<div class="post-metadata">

**Author:** ![DuncantheeDuncan](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/duncantheeduncan/32/97294_2.png) [@DuncantheeDuncan](https://forum.freecodecamp.org/u/DuncantheeDuncan)\
**Post date:** [September 8, 2018, 7:18pm UTC](https://forum.freecodecamp.org/t/return-early-pattern-for-functions/25899/7 "2018-09-08T19:18:09Z")

</div>

👌🏻  
its makes sense this way
