Functions and return statements in Corona

An inquiry was raised on the different Corona bolster channels: “What is the arrival proclamation and when do I have to utilize it?” Before that question can be replied, you have to comprehend what capacities are and how Lua utilizes them.

Capacities are squares of code that can be reused. Consider changing a vehicle tire:

Take out jack, haul wrench and extra tire from trunk

Put the haul wrench on nut #1

Turn counter-clockwise until the nut falls off

Put the fastener in a sheltered spot

Put the haul wrench on nut #2

Turn counter-clockwise until the nut falls off

Put the fastener in a sheltered spot

Put the carry wrench on nut #3

Pivot counter-clockwise until the nut falls off

Put the fastener in a protected spot

Put the carry wrench on nut #4

Pivot counter-clockwise until the nut falls off

Put the fastener in a protected spot

Put the carry wrench on nut #5

Pivot counter-clockwise until the nut falls off

Put the fastener in a protected spot

Lift the vehicle

Expel the punctured tire

Put the extra tire on

and so on.

PC code executes in a straight manner. If you somehow managed to work this out in PC code you wind up rehashing yourself on numerous occasions. It bodes well to take the fastener evacuation code and put it in a capacity. Think about this pseudo-code:

This decreases our calculation to:

Take out jack, carry wrench and extra tire from trunk

For each lugNutNumber

removeLugNut( lugNutNumber)

Raise the vehicle

Evacuate the punctured tire

Put the extra tire on

The code is considerably more minimal. It follows a fundamental designer guideline called DRY – Don’t Repeat Yourself.

Capacities can be utilized in an assortment of routes in Corona applications. We should take a gander at a fundamental model:

This capacity needn’t bother with any data. It utilizes a current characterized object: player and augmentations its .x position by one. It takes no parameters and doesn’t pass any information back to the calling code. You may utilize this inside another capacity that runs each clock tick:

Since you don’t put anything inside the brackets, you are sending nothing to the capacity. Be that as it may, you could without much of a stretch pass data to the capacity. You might need to make this capacity somewhat more nonexclusive. Rather than movePlayer, you could state moveObject. You could likewise give the speed:

Since you realize how to pass data to a capacity shouldn’t something be said about getting it back? To start with, not all capacities need to send information back, yet when you do, you can do so utilizing Lua’s arrival articulation.

At the machine code level, the entirety of the above capacities have a suggested bring proclamation back. As an accommodation to Lua designers, you don’t have to indicate one on the off chance that you needn’t bother with it, therefore: and

are indistinguishable.

The arrival explanation has two principle purposes. To begin with, it very well may be utilized to compel a capacity to end early. How about we take a gander at that model:

Since the code didn’t have a substantial article to change the estimation of x on, you can leave the capacity and keep away from a potential blunder.

The second utilization of an arrival proclamation is to pass data back to the code that called the capacity, That data could be a straightforward achievement or disappointment marker, or it could go back qualities that are progressively valuable. How about we take a gander at the basic achievement/disappointment circumstance altering above capacity.

To get the information, the code calling the capacity can either store the arrival esteem in a variable or test it in a contingent test.

Once in a while you have to catch the qualities. We should take a gander at this straightforward capacity that includes two numbers together:

Presently you can pass information to the capacity just as get data back utilizing the arrival explanation.

Most programming dialects can just return one worth (however it could be a table, word reference, or rundown with various qualities). Lua, anyway lets you return different qualities. Gives up back to the move object work.

You can see from this instructional exercise how the arrival proclamation can be helpful in your Corona undertakings.

Loot is the Developer Relations Manager for Corona Labs. Other than being enthusiastic about helping different engineers make incredible games utilizing Corona, he is additionally appreciates making games in his extra time. Loot has been coding games since 1979 from PCs to centralized servers. He has more than 16 years proficient involvement with the gaming business.

Leave a Reply

Your email address will not be published. Required fields are marked *