Topics List
Basic Concepts

Functions
Understanding Functions in Programming

In programming, everything revolves around functions, which are essentially sets of instructions. Each function contains a series of commands that dictate what the program should do. Functions can also contain other functions, allowing for complex behavior to be built up from simpler components.
Some functions are created manually by you, the programmer, to perform specific tasks within your program. These functions are written by you to meet the unique requirements of your application.
Other functions are created automatically by Kenzie, our programming environment. These functions serve as placeholders where you can add your own code. Kenzie generates these functions based on the structure of your program and the actions you perform within the IDE.
Additionally, our programming environment includes an extensive library of pre-written functions that provide common functionality. These functions are available for you to use in your program, and Kenzie will automatically add them to your project if you use them in your code. This allows you to leverage existing code and functionality, saving you time and effort in your development process.

Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

A function in programming can receive parameters, which are values that are passed to the function when it is called. If a function does not have any parameters, it will have the parameter event by default. event is a set of properties related to the object that called the function, and these properties can be used during the execution of the function. For example, through event, you can get the name of the object that performed the call.
A function can be defined as local, which means it will only be available for a limited scope of code, depending on where it was defined.
Additionally, a function can return one or more values, or it may not return anything at all, as it is created solely to perform an action.
Functions can also be associated with events from objects placed in the Designer area. In another section where we explain all about the designer components, we will explain how to associate events with functions. This means that those functions will be executed automatically once the programmed event fires up.

Local Function


If a function is defined inside another function, it will only be available within the scope of that outer function. Any attempt to call that inner function from outside its scope will result in an error in the application. This is because the inner function is considered a local function and its scope is limited to the function in which it is defined.
In the following example, we have defined a local function inside another function (outerFunction). This inner function (innerFunction) is only accessible within the outerFunction. When we try to call innerFunction from outside outerFunction, it generates an error because the scope of innerFunction is limited to where it was created.

Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

In the following example, we have two functions defined: button_pressed and another_button.
  • button_pressed is connected to a button named ''button_one'' in the designer area. When this button is pressed, the button_pressed function is executed. Inside button_pressed, there is a local function say_hello defined. This say_hello function simply displays a message box with the text ''Hello''. The say_hello function is then called inside button_pressed, so when ''button_one'' is pressed, it will display the message box with ''Hello''.

  • another_button is connected to a button named ''button_two'' in the designer area. When this button is pressed, the another_button function is executed. Inside another_button, there is an attempt to call the say_hello function. However, since say_hello is a local function defined inside button_pressed, it is not accessible from another_button. This attempt to call say_hello from another_button will generate an error because say_hello is out of scope.




Global Function


Global functions are functions that are accessible from anywhere in your code, regardless of where they are defined. They are not limited to a specific scope and can be called from any part of your program.
Here´s an example:

Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

In this example, globalFunction is a global function because it is defined outside of any other function or block. This means that you can call globalFunction from anywhere in your code, and it will print ''This is a global function''.
Global functions are useful for defining commonly used functionality that needs to be accessed from multiple parts of your program. However, it´s important to use them judiciously, as having too many global functions can lead to code that is hard to maintain and debug.

Functions with parameters


Functions with parameters allow you to pass values to the function when you call it, which the function can then use to perform its task. Parameters are variables that are declared in the function definition and are used to receive the values passed to the function. Here´s an example:

Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

In this example, the greet function takes a parameter name, which is used to personalize the greeting message. When you call greet(''Alice''), the function prints ''Hello, Alice!''. Similarly, when you call greet(''Bob''), it prints ''Hello, Bob!''.
You can define multiple parameters in a function, separated by commas, and then pass corresponding values when calling the function. For example:

Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

In this example, the addNumbers function takes two parameters, a and b, and returns the sum of a and b. When you call addNumbers(5, 3), the function returns 8, which is then printed to the console.

Recursive functions


Recursive functions are functions that call themselves to solve smaller instances of the same problem. They´re useful for tasks that can be broken down into simpler, similar sub-problems.
Here are some simpler examples of recursive functions:
  1. Factorial Function:

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This function calculates the factorial of a given number n. If n is less than or equal to 1, it returns 1. Otherwise, it multiplies n by the factorial of n-1.

  2. Fibonacci Function:

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This function calculates the nth Fibonacci number. If n is 0 or 1, it returns n. Otherwise, it returns the sum of the previous two Fibonacci numbers.

  3. Countdown Function:

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This function prints numbers from n to 1, then stops. It uses recursion to decrement n by 1 until n is no longer greater than 0.

  4. Power Function:

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This function calculates base raised to the power of exponent. If exponent is 0, it returns 1. Otherwise, it multiplies base by itself exponent times using recursion.

Remember, recursion can be a powerful tool, but it´s important to ensure that your recursive function has a base case (a condition where the function stops calling itself) to avoid infinite recursion.

More things to consider about functions


  1. Callback Functions:
    A function can be passed as argument to other functions, commonly used in event-driven programming.
    In the following example, performOperation is a function that takes two numbers x and y, computes their sum, and then calls a callback function callback with the result. The printResult function is defined separately and serves as the callback function, which simply prints the result to the console. When performOperation is called with printResult as the callback, it will compute the sum of 10 and 20 and then pass the result (30) to printResult, which will print ''The result is: 30'' to the console.

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

  2. Function Libraries:
    In Kenzie, numerous pre-defined functions are available for specific tasks, accessible through our F1 Help System and Intellisense feature.

  3. Objects as Parameters:
    In Kenzie, you can pass any object as a parameter. For example, you can pass an image or a label, among other objects.