Functions

Functions can carry out a procedure or compute and return values. We can make a function call as a statement or we can use it as an expression. We have learned that functions can be variables. A table can use those variables to store them as properties.

Functions are the most important means of abstraction in Lua. One function that we have used many times is: print. In the following example, the print function is being told to execute one piece of data—the string, "My favorite number is 8":

print("My favorite number is 8") -- My favorite number is 8

Another way of saying this is that print is being called with one argument. print is only one of the many built-in functions that Lua has, but almost any program you write will involve you defining your own functions.

Defining a function

When trying to define a function, you have to give it a name that you can call out to when you want to return a value. You then have to create a statement of what the value will output and then apply end to your function after you have finished defining it. For example:

function myName()
  print("My name is Jane.")
end

myName()  -- My name is Jane.

Notice that the function name is myName and is used to call out what's inside the function definition print("My name is Jane.").

An alternative to defining a function is as follows:

function myName(Name)
  print("My name is " .. Name .. ".")
end

myName("Jane")  -- My name is Jane.
myName("Cory")  -- My name is Cory.
myName("Diane")  -- My name is Diane.

The new myName function has one argument using the variable Name. The string, "My name is " is concatenated with Name and then a period as the printed result. When the function is called, we used three different names as an argument and the result is printed with a new customized name for each line.

More display functions

In Corona, you can change the appearance of the status bar on your device. This is a one-line setting in your code that takes effect once you launch your application.

display.setStatusBar( mode )—Hides or changes the appearance of the status bar on iOS devices (iPad, iPhone, and iPod Touch) and Android 2.x devices. Android 3.x devices are not supported.

The argument mode should be one of the following:

  • display.HiddenStatusBar: To hide the status bar, you can use the following line at the beginning of your code:
    display.setStatusBar( display.HiddenStatusBar )
    More display functions
  • display.DefaultStatusBar: To show the default status bar, you can use the following line at the beginning of your code:
    display.setStatusBar(display.DefaultStatusBar)
    More display functions
  • display.TranslucentStatusBar: To show the translucent status bar, you can use the following line at the beginning of your code:
    display.setStatusBar(display.TranslucentStatusBar)
    More display functions
  • display.DarkStatusBar: To show the dark status bar, you can use the following line at the beginning of your code:
    display.setStatusBar(display.DarkStatusBar)
    More display functions

Content size properties

When you want to obtain display information on your device, you can use the content size properties to return the values.

  • display.contentWidth—Returns the original width of the content in pixels. This will default to the screen width.
  • display.contentHeightReturns the original height of the content in pixels. This will default to the screen height.
  • display.viewableContentWidthA read-only property that contains the width of the viewable screen area in pixels, within the coordinate system of the original content. Accessing this property will display how the content is viewed, whether you're in portrait or landscape mode. For example:
      print( display.viewableContentWidth )
  • display.viewableContentHeightA read-only property that contains the height of the viewable screen area in pixels within the coordinate system of the original content. Accessing this property will display how the content is viewed, whether you're in portrait or landscape mode. For example:
      print( display.viewableContentHeight )
  • display.statusBarHeightA read-only property representing the height of the status bar in pixels (only valid on iOS devices). For example:
      print(display.statusBarHeight)