# Not all code paths return a value

**URL:** https://forum.makecode.com/t/not-all-code-paths-return-a-value/4508
**Category:** Help
**Created:** [November 19, 2020, 8:54pm UTC](https://forum.makecode.com/t/not-all-code-paths-return-a-value/4508 "2020-11-19T20:54:23Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![GameGod](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gamegod/32/2756_2.png) [@GameGod](https://forum.makecode.com/u/GameGod)
#### Post date: [November 19, 2020, 8:54pm UTC](https://forum.makecode.com/t/not-all-code-paths-return-a-value/4508/1 "2020-11-19T20:54:23Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/6/60b921c928024273863d051dfa636f84f5954f73.png)  
What is the error?

---

<div class="post-metadata">

### Author: ![UnsignedArduino](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unsignedarduino/32/592_2.png) [@UnsignedArduino](https://forum.makecode.com/u/UnsignedArduino)
#### Post date: [November 19, 2020, 9:11pm UTC](https://forum.makecode.com/t/not-all-code-paths-return-a-value/4508/2 "2020-11-19T21:11:32Z")

</div>

Say we pass in `w` to `WhatToGet`.

Then it won’t execute `return`\*.

That’s a problem.

* * *

\*Well technically it will automatically return _something_ but IDK what it is in TypeScript. In Python, that would be `None` which is TypeScript’s `null`.

---

<div class="post-metadata">

### Author: ![GameGod](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gamegod/32/2756_2.png) [@GameGod](https://forum.makecode.com/u/GameGod)
#### Post date: [November 19, 2020, 9:20pm UTC](https://forum.makecode.com/t/not-all-code-paths-return-a-value/4508/3 "2020-11-19T21:20:37Z")

</div>

So if I put an else, it will work?

---

<div class="post-metadata">

### Author: ![UnsignedArduino](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unsignedarduino/32/592_2.png) [@UnsignedArduino](https://forum.makecode.com/u/UnsignedArduino)
#### Post date: [November 19, 2020, 9:25pm UTC](https://forum.makecode.com/t/not-all-code-paths-return-a-value/4508/4 "2020-11-19T21:25:47Z")

</div>

You need to always return something no matter what happens in the function. And they have to be the same type.

So something like this:

```typescript
function foo(x: number): boolean {
    if (x== 0) {
        return true
    }
}

```

Will give an error, because `x` can be 1, so it won’t execute `return true`.

Something like this:

```typescript
function foo(x: number): boolean {
    if (x== 0) {
        return true
    } else {
        return false
    }
}

```

Will be syntactically correct. But notice how `true` and `false` are both `booleans`. You couldn’t do this:

```typescript
function foo(x: number): boolean {
    if (x== 0) {
        return true
    } else {
        return "bad value"
    }
}

```

---

<div class="post-metadata">

### Author: ![GameGod](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gamegod/32/2756_2.png) [@GameGod](https://forum.makecode.com/u/GameGod)
#### Post date: [November 19, 2020, 9:29pm UTC](https://forum.makecode.com/t/not-all-code-paths-return-a-value/4508/5 "2020-11-19T21:29:31Z")

</div>

But you could do

```
if (ReturedValue == 0) {
        splash ("Bad Vaue")
   
    }

```

Right?

---

<div class="post-metadata">

### Author: ![UnsignedArduino](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unsignedarduino/32/592_2.png) [@UnsignedArduino](https://forum.makecode.com/u/UnsignedArduino)
#### Post date: [November 19, 2020, 9:30pm UTC](https://forum.makecode.com/t/not-all-code-paths-return-a-value/4508/6 "2020-11-19T21:30:11Z")

</div>

Are you in a function? You will still need to return a value.

---

<div class="post-metadata">

### Author: ![GameGod](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gamegod/32/2756_2.png) [@GameGod](https://forum.makecode.com/u/GameGod)
#### Post date: [November 19, 2020, 9:30pm UTC](https://forum.makecode.com/t/not-all-code-paths-return-a-value/4508/7 "2020-11-19T21:30:28Z")

</div>

No, separate.

---

<div class="post-metadata">

### Author: ![UnsignedArduino](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unsignedarduino/32/592_2.png) [@UnsignedArduino](https://forum.makecode.com/u/UnsignedArduino)
#### Post date: [November 19, 2020, 9:32pm UTC](https://forum.makecode.com/t/not-all-code-paths-return-a-value/4508/8 "2020-11-19T21:32:00Z")

</div>

Yes, but you still have the problem with your original function.

---

<div class="post-metadata">

### Author: ![charliegregg](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/charliegregg/32/2411_2.png) [@charliegregg](https://forum.makecode.com/u/charliegregg)
#### Post date: [November 19, 2020, 9:33pm UTC](https://forum.makecode.com/t/not-all-code-paths-return-a-value/4508/9 "2020-11-19T21:33:01Z")

</div>

Yes If your function sometimes returns a value then It will get confused, If you use (if returned value == 0 then “bad value…”) that will be fine.

---

<div class="post-metadata">

### Author: ![charliegregg](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/charliegregg/32/2411_2.png) [@charliegregg](https://forum.makecode.com/u/charliegregg)
#### Post date: [November 19, 2020, 9:33pm UTC](https://forum.makecode.com/t/not-all-code-paths-return-a-value/4508/10 "2020-11-19T21:33:29Z")

</div>

But you always have to return or not return.

---

<div class="post-metadata">

### Author: ![GameGod](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gamegod/32/2756_2.png) [@GameGod](https://forum.makecode.com/u/GameGod)
#### Post date: [November 19, 2020, 9:48pm UTC](https://forum.makecode.com/t/not-all-code-paths-return-a-value/4508/11 "2020-11-19T21:48:12Z")

</div>

> [@UnsignedArduino](#):
>
> Yes, but you still have the problem with your original function.

Oh, I fixed that with the else statment.

---

<div class="post-metadata">

### Author: ![Dreadmask197](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dreadmask197/32/10494_2.png) [@Dreadmask197](https://forum.makecode.com/u/Dreadmask197)
#### Post date: [November 20, 2020, 1:19pm UTC](https://forum.makecode.com/t/not-all-code-paths-return-a-value/4508/12 "2020-11-20T13:19:02Z")

</div>

At all times, you have to put return blocks at the end of a function if you have another return block at the end
