Custom function / variables in formulas?

Is it possible to define a custom function or define variables within a formula for use in the formula? Apologies if this is a basic question.

You write formula code using Python language. Almost everything that works in Python works in Grist formula.

You need no special defining in Python. Just assign a value to it and reuse.
Example:

var1 = 5
var1+=1
return var1