Back
Function

By Sean Toru | last updated 28th December 2020


F

Function

What is a function?

A function is one of the fundamental building blocks of computer programming. It’s a set of grouped instructions that does something specific. It can have a name, one or more ‘inputs’ (aka ‘arguments’) and an output. Functions can be used for pretty much anything. So you might write a function that checks the username and password that a user entered, or to get specific information from a database, or to respond to a user’s click on a button on your app.

The ‘grammar’ of a function changes between different programming languages, but in general they have names, arguments, the function’s code itself, and a return statement. So for example here’s a PHP function that checks if an email is valid, and returns a ‘boolean’ response - i.e. true if the email address is properly formatted, and false otherwise.

function checkEmail (string $email) : bool {
  if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    return TRUE;
  } else {
    return FALSE;
  }
}

Multiple functions and ‘variables’ can be grouped together into ‘classes’, and this is what forms the structure of the traditional computer program. Understanding the nuts and bolts of how functions work in your language of choice is essential before going on to think about more complex architectural questions.

Related Terms

Do you want to contribute to this page?