Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

Functions are an elementary part of programming languages. With them programs can be simplified and presented in a manageable and reusable way (modularisation). Functions also help us to keep our code organised and understandable. 

Functions act as jumps in the program. In the event of a function call, the program jumps to the position where the function is defined and then returns after the function has been executed.

Most programming languages have many built in or native functions. To find out how the functions work, and what parameters they expect, we need to look at the documentation (link).


Syntax of functions

There are two parts of a function. First there is the the function declaration, where the name of the function along with its properties and behaviors are defined. Next there is the function call, where the function is then actually executed. Functions may be called multiple times.

Functions can also accept input values (called parameters) that are sent to the function when called. These input parameters can change the behaviour of the function.

Functions can also return a value. This means when we return to the line where the function was called, it has now carried a number with it. In processing, functions that have no return value are declared with “void” return type.


Function Declaration

This is how the function is named, its return type and parameters defined with sudo code.


datatype functionName (parameter,.....)

{

<function contents>

<possible return value>

}


Function call 

This is how we execute a function:


functionName(parameter,.....) ;