In principle, cython handles global variables as python does, no matter whether it's a C or Python type. Have a look at this part of the FAQ.
So your (second) example would not work, you'd have to use global variable
at the beginning of your function, like this:
def func2(double x):
global a, b, c
a = x + 2
b = (x + 2) ** 2
c = (x + 2) ** 3
return a + b + c
However, at this point I would like to question, whether you really need to do this. Quite generally, there are good arguments, why global variables are bad. So you seriously might want to reconsider.
I assume, that your three doubles are just a toy example, so I'm not sure what your actual use case is. Judging from your (first) example, reusing code could be done instead by extending the function by another parameter, like so:
def func(double x, double y=0):
cdef double a, b, c
a = x + y
b = (x + y) ** 2
c = (x + y) ** 3
return a + b + c
This would at least cover your examples func1
and func2
here by using y = 0
and y = 1
respectively.