“Stack too deep, try removing local variables”

If you are a solidity programmer, you know this better!.. But if you are a beginner you tend to face this issue quite frequently.

Why this issue occurs?

In solidity programming, every function has a local variable limitation, such as it cannot exceed 16 local variables. For instance, Let’s assume the below function.

  • you can have around 16 local variables (including parameters and return parameters)

The above function has many variables defined that include the passing parameters. Assume if we have to perform additional calculations, this will trigger the “Stack too deep” issue.

How to solve it? just create internal functions and perform your business logic in that. It should get rid of this issue. For instance, the above function can be re-written like below.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store