In programming terms, a function is a block of statements that performs a specific task. Functions accept data, process it, and return a result. Functions are written primarily to support the concept of reusability. Once a function is written, it can be called easily, without having to write the same code again and again.
Different functional languages use different syntax to write a function.
Before writing a function, a programmer must know the following points −
Purpose of function should be known to the programmer.
Algorithm of the function should be known to the programmer.
Functions data variables & their goal should be known to the programmer.
Function's data should be known to the programmer that is called by the user.
When a function is "called", the program "transfers" the control to execute the function and its "flow of control" is as below −
The program reaches to the statement containing a "function call".
The first line inside the function is executed.
All the statements inside the function are executed from top to bottom.
When the function is executed successfully, the control goes back to the statement where it started from.
Any data computed and returned by the function is used in place of the function in the original line of code.
The general syntax of a function looks as follows −
returnType functionName(type1 argument1, type2 argument2, . . . ) { // function body }
Let’s take an example to understand how a function can be defined in C++ which is an object-oriented programming language. The following code has a function that adds two numbers and provides its result as the output.
#include <stdio.h> int addNum(int a, int b); // function prototype int main() { int sum; sum = addNum(5,6); // function call printf("sum = %d",sum); return 0; } int addNum (int a,int b) { // function definition int result; result = a + b; return result; // return statement }
It will produce the following output −
Sum = 11
Let’s see how the same function can be defined in Erlang, which is a functional programming language.
-module(helloworld). -export([add/2,start/0]). add(A,B) -> C = A + B, io:fwrite("~w~n",[C]). start() -> add(5,6).
It will produce the following output −
11
A function prototype is a declaration of the function that includes return-type, function-name & arguments-list. It is similar to function definition without function-body.
For Example − Some programming languages supports function prototyping & some are not.
In C++, we can make function prototype of function ‘sum’ like this −
int sum(int a, int b)
Note − Programming languages like Python, Erlang, etc doesn’t supports function prototyping, we need to declare the complete function.
The function prototype is used by the compiler when the function is called. Compiler uses it to ensure correct return-type, proper arguments list are passed-in, & their return-type is correct.
A function signature is similar to function prototype in which number of parameters, datatype of parameters & order of appearance should be in similar order. For Example −
void Sum(int a, int b, int c); // function 1 void Sum(float a, float b, float c); // function 2 void Sum(float a, float b, float c); // function 3
Function1 and Function2 have different signatures. Function2 and Function3 have same signatures.
Note − Function overloading and Function overriding which we will discuss in the subsequent chapters are based on the concept of function signatures.
Function overloading is possible when a class has multiple functions with the same name but different signatures.
Function overriding is possible when a derived class function has the same name and signature as its base class.