Calculating Regression with By-function

Sure. Understanding is more important than implementing, in my opinion.

It's not a function. You've used expr yourself.

Here, lm(Sepal.Width~Sepal.Length) is the expr part. For details, check the documentation of with.

If you have a data frame df of two columns x and y, lm(df) performs linear regression of x on y. In iris, Sepal.Length appears first, and you wanted the opposite regression, and hence the explicit definition.