Elaborating jdehesa's comment for the benefit of the community.
In the Official Documentation for tf.function, it is mentioned as:
Key Point: Any Python side-effects (appending to a list, printing with
print, etc) will only happen once, when func is traced. To have
side-effects executed into your tf.function they need to be written as
TF ops:
Applying that explanation to your example, the Operations, out = x+5 and tf.print will be stored in the Graph and the operation print doesn't.
It is always recommended to pass Tensors as arguments to the functions instead of Python Scalars so that all the operations will be captured in a Single Graph.
tf.function only allows creating new tf.Variable objects when it is called for the first time.
In general, it is recommended to create stateful objects like tf.Variable outside of tf.function and passing them as arguments.
If we use Python Assignment Operator, a = b, will not be part of the Graph and tf.assign will be. Python Print will not be part of the Graph but tf.Print will be.
Also, if we want even Python Code to be part of Graph, we can use tf.py_function inside our function, but it is not recommended.
Hope this helps. Happy Learning!!