# Broadcasting a scalar

**URL:** <https://discourse.julialang.org/t/broadcasting-a-scalar/3583>\
**Category:** New to Julia\
**Tags:** broadcast\
**Created:** [May 7, 2017, 10:29pm UTC](https://discourse.julialang.org/t/broadcasting-a-scalar/3583 "2017-05-07T22:29:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndbecker/32/10829_2.png) [@ndbecker](https://discourse.julialang.org/u/ndbecker)\
**Post date:** [May 7, 2017, 10:29pm UTC](https://discourse.julialang.org/t/broadcasting-a-scalar/3583/1 "2017-05-07T22:29:24Z")

</div>

Let’s say I have a function like

```julia
function F(x::Vector{T}, y) where T
  for a,b in zip(x,y):
    do_something(a, b)
  end
end

```

Now suppose y can be either a vector or a scalar. To handle this, one choice would be just create another

```julia
function F(x::Vector{T}, y::T) where T
  for a in x:
    do_something(a, y)
  end
end

```

But just for my own education, I’m wondering if I can just have one function F that accepts 2 Vectors, and then when a scalar is passed, broadcast the scalar? What would that look like? Would it be as efficient as dispatching to 2 different functions (one for Vector, one for Scalar)?

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [May 8, 2017, 1:32am UTC](https://discourse.julialang.org/t/broadcasting-a-scalar/3583/2 "2017-05-08T01:32:35Z")

</div>

You can use `generators` to “broadcast” your scalar into your vector version.

```julia
function F(x::Vector{T}, y::T) where T
   F(x, y for i in 1:length(x))
end

```

Of course you could also just use `broadcast` itself: `broadcast(do_something, x, y)` which will treat scalars and vectors correctly.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 8, 2017, 1:35am UTC](https://discourse.julialang.org/t/broadcasting-a-scalar/3583/3 "2017-05-08T01:35:28Z")

</div>

> [@ndbecker](#):
>
> But just for my own education, I’m wondering if I can just have one function F that accepts 2 Vectors, and then when a scalar is passed, broadcast the scalar? What would that look like? Would it be as efficient as dispatching to 2 different functions (one for Vector, one for Scalar)?

Normally, you would just write a function `F(x,y)` that accepts scalars only. Then, to apply it to vectors or a mix of vectors and scalars, you would do `F.(x,y)`.

---

<div class="post-metadata">

**Author:** ![ndbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndbecker/32/10829_2.png) [@ndbecker](https://discourse.julialang.org/u/ndbecker)\
**Post date:** [May 8, 2017, 12:07pm UTC](https://discourse.julialang.org/t/broadcasting-a-scalar/3583/4 "2017-05-08T12:07:37Z")

</div>

That’s great! Couldn’t be easier. Is this documented?

---

<div class="post-metadata">

**Author:** ![ndbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndbecker/32/10829_2.png) [@ndbecker](https://discourse.julialang.org/u/ndbecker)\
**Post date:** [May 8, 2017, 12:57pm UTC](https://discourse.julialang.org/t/broadcasting-a-scalar/3583/5 "2017-05-08T12:57:09Z")

</div>

OK, I see it is explained here  
[https://docs.julialang.org/en/latest/manual/arrays.html](https://docs.julialang.org/en/latest/manual/arrays.html)
