FAQ: Functions - Calling A Function

This community-built FAQ covers the “Calling A Function” exercise from the lesson “Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Lua

FAQs on the exercise Calling A Function

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why do you add parenthesis at the end of a function’s name?

Suppose you have declared a function named greeting:

function greeting()
    print("Hello there!")
    print("Nice to see you!")
    return "Greeting Success"
end

You can think of the function as a recipe. By writing down the recipe in your cookbook, you have detailed the steps needed to cook the recipe. But, you haven’t started cooking the recipe. Nothing happens yet except that there is a recipe named greeting in the cookbook. None of the steps in the recipe are performed/executed.
If we do something like

print(greeting)
// Output:
// function: 0x5643a19170b0

then we still haven’t started cooking yet. The above will just print information about greeting that it is a function and it will show some memory address where the function is stored (like a recipe is written down on a specific page).
When you add parentheses at the end of the function name, then you are calling the function i.e. now you finally start cooking the recipe.

greeting()
// Output:
// Hello there!
// Nice to see you!

print(greeting())
// Output:
// Hello there!
// Nice to see you!
// Greeting Success

We used parentheses when declaring the function as well, but there was the function keyword before the function name and parentheses like so:

function greeting()
   -- print statements here
end

Because of the function keyword and above syntax, the interpreter recognizes that a function/recipe is being entered in the cookbook, but is not being cooked yet.
When we call the function by omitting the function keyword and adding parentheses like so:

greeting()

then the statements (steps) in the body of the function (recipe) are executed (performed), and you see output on the screen.

1 Like