FUNCTIONS
Functions allow to structure programs in segments of code to perform individual tasks. Functions are used to provide modularity to a program. There are two types of functions in programming Languages i.e. Standard Library Functions and User Defined Functions.
Standard Library Functions
These are built-in functions that are defined in the libraries of programming language. These are used to perform basic operations like input(scanf() ,gets()), output(printf(),puts()), mathematical operations(sqrt()), string related operations etc.
These functions are limited and thus can not be used to perform user specific tasks.
User Defined Functions
As the name implies, these are block of code defined by user as per his/her requirements. These are basically modules which facilitate programming. For instance, if you wish to make a program for mini calculator, you can have various user defined functions as add(), subtract(), multiply(), divide() etc. and call those any number of times you want.
PARTS OF A FUNCTION
The general syntax of a function in C++ is
return_type function_name (Argument_List)
{
//Body of function
}
FUNCTION DECLARATION/PROTOTYPE
In C++, identifiers can only be used in expressions if they have been declared before. Thus, functions cannot be called before they are declared. If a user wants to call the function before the body of function is defined, the user needs to declare the prototype of function containing necessary details of function.
Declaring a function is important as compiler does not know about the user defined function in advance. A Function declaration does not contain body of function. The general syntax of Function Declaration is
return_type function_name (Argument_List);
FUNCTION CALL
When a program calls a function, program control is transferred to the called function. A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.
When a program calls a function, program control is transferred to the called function. A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.