x was in the .Local environment of plotz() before but here it it is in the .Global environment and that's why the original didn't work while this one does.
Here are two simpler alternatives
# no parameters
plotz <- function() {
x = seq(from = -5, to = 5, by = .001)
plot(x, exp(x), col = "orange")
points(x, exp(x^2), col = "green")
}
plotz()
# parameterize everything
plotz <- function(begin, finish, incr, col1, col2) {
x = seq(from = begin, to = finish, by = incr)
plot(x, exp(x), col = col1)
points(x, exp(x^2), col = col2)
}
plotz(begin = -4,finish =4,incr = .01,col1 = "pink",col2 = "steelblue")
perhaps one option is to leverage r formulas which are recognisable by their tilde notation i.e. ~ ; this is because you can write them, and they are unevaluated expressions until they are used in some way.
this might look like
myplot <- function(form_1,
form_2,
x_vals){
l <- list(x=x_vals)
plot(form_1,l,col="red")
points(form_2,l,col="blue")
}
myplot(exp(x)~x,
exp(x^2)~x,
seq(from = -4, to = 4, by = .01))