> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/sidef-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/sidef-lang/programming_tasks/a/apply_a_callback_to_an_array.md).

# Apply a callback to an array

Defining a callback function:

```ruby
func callback(i) { say i**2 }
```

The function will get called for each element:

```ruby
[1,2,3,4].each(callback)
```

Same as above, but with the function inlined:

```ruby
[1,2,3,4].each{|i| say i**2 }
```

For creating a new array, we can use the `Array.map` method:

```ruby
[1,2,3,4,5].map{|i| i**2 }
```
