# Use of function/return

**URL:** <https://discuss.python.org/t/use-of-function-return/11430>\
**Category:** Python Help\
**Created:** [October 24, 2021, 10:22pm UTC](https://discuss.python.org/t/use-of-function-return/11430 "2021-10-24T22:22:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giorgina99](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@Giorgina99](https://discuss.python.org/u/Giorgina99)\
**Post date:** [October 24, 2021, 10:22pm UTC](https://discuss.python.org/t/use-of-function-return/11430/1 "2021-10-24T22:22:59Z")

</div>

Hello all,

I had the task to code the following:

Take a list of integers and returns the value of these numbers added up, but only if they are odd.

- Example input: [1,5,3,2]
- Output: 9

I did the code below and it worked perfectly.

* * *

`numbers ` `=` `[` `1` `,` `5` `,` `3` `,` `2` `]`

`print` `(numbers)`

`add_up_the_odds ` `=` `[]`

`for` `number ` `in` `numbers:`

` ` `if` `number ` `%` `2` `=` `=` `1` `:`

` ` `add_up_the_odds.append(number)`

`print` `(add_up_the_odds)`

`print` `(` `sum` `(add_up_the_odds))`

* * *

Then I tried to re-code it using function definition / return:

* * *

`def` `add_up_the_odds(numbers):`

` ` `odds ` `=` `[]`

` ` `for` `number ` `in` `range` `(` `1` `,` `len` `(numbers)):`

` ` `if` `number ` `%` `2` `=` `=` `1` `:`

` ` `odds.append(number)`

` ` `return` `odds`

`numbers ` `=` `[` `1` `,` `5` `,` `3` `,` `2` `]`

`print` `(` `sum` `(odds))`

* * *

But I couldn’t make it working, anybody can help with that?

---

<div class="post-metadata">

**Author:** ![Quercus](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/quercus/32/31483_2.png) [@Quercus](https://discuss.python.org/u/Quercus)\
**Post date:** [October 24, 2021, 11:36pm UTC](https://discuss.python.org/t/use-of-function-return/11430/2 "2021-10-24T23:36:15Z")

</div>

You have:

```auto
for number in range ( 1 , len (numbers)):

```

Remember that the index of the first element in a `list` is `0`.

---

<div class="post-metadata">

**Author:** ![BowlOfRed](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bowlofred/32/3379_2.png) [@BowlOfRed](https://discuss.python.org/u/BowlOfRed)\
**Post date:** [October 24, 2021, 11:58pm UTC](https://discuss.python.org/t/use-of-function-return/11430/3 "2021-10-24T23:58:39Z")

</div>

Besides turning it into a function, you changed what you were doing.

In the first program you used:

```auto
for number in numbers:

```

Here `number` is set to each of the elements of the list in turn. So you can add them up (although it’s unclear why you couldn’t have just summed the original `numbers` in the first place).

But in your function you run:

```auto
for number in range ( 1 , len (numbers)):

```

This just returns numbers in a row (1, 2, 3, …). The actual contents of `numbers` is never examined.
