00 days
00 hours
00 minutes
00 seconds

What is Function?


Function is a unit of block code that executes a specific task. It is a type of procedure organized to produce a single result. In a word, the function takes in data - an input calculates it and gives back a result - output. The function itself is a program fragment that produces a specified effect. Once the function is set, it can be used over and over again. Depending on the purpose the function remains more or less fixed.

With the help of functions, it’s possible to build the entire software apps. A great code structure includes blocks of statements that perform a particular task. The act of using the function sounds as ‘calling the function’. The execution of the programming function starts from the moment of ‘calling’ it. As soon as the function is ‘called’, the program begins to perform the first statement inside the function. If needed, you can ‘call’ functions from the internal functions.

The programming function is described as follows - the keyword function, accompanied by a name, and by parentheses ().

The function combines a sequence of statements - function body. Any data or value can be passed to the function, and the function will return the value or data. It consists of a function header and a function body. The program always starts working from the top to the bottom. The program is divided into two functions - addition and main, where 'main' is called automatically. 'Addition' are produced if only 'are called' from main.

The name means the value which may subsist of letters, digits, dollars, and emphasizes. The parentheses contain parameter names divided by comma. It behaves as a regular variable. What’s inside the braces {} is a block of statements which specifies the task.

Advantages of Functions:

  • Convenience. If any overall program seems too hard, you can divide it into small sub-steps.
  • You can reuse code. No need to rewrite it. Define it once and call the same function as many times as you need. Use the same code to produce different results.
  • You can test particular parts of the code structure separately from the whole program. Thus, you can confirm the function performs exactly what it is designed to perform.

Types of Functions:

  • Library Functions - those built-in functions supported by R Language. Here come such as print(), sqrt(), etc.
  • User Defined Functions - new created functions to make particular tasks which can be called multiple time.

Related terms: CodingScripting, JavaScripting

References and further reading: