Tutorial 13



Functions 
Functions in C++ are programs to perform a specific task. A program is divided into steps or modules and each module or step can be provided separate function in order to reduce the complexity of the program. Some points to be considered while writing functions are : functions be given a name, function name is similar to naming rules of variables, function name ends with a set of parentheses to help understand C++ it as a function not a variable, body of the function is enclosed by braces. When function other than main is invoked the control is transferred to other function is called function call and when the execution of that function completes the control is returned to the main function. We may sometimes need to pass some data to the called functions. There are two ways to pass the data namely passing by value and passing by address. Passing by value is the value from main function that is to be used by called function is copied to the variable of called function, however in case of passing by address the address of the memory location of function whose contents are to be used in called function is passed/referred. In passing by address all the processing takes place on the main data, if the calling function has to effect some changes/updation to the main function variable, then it does not have to return the values to the main function and then update the variables there, rather the main function variables are updated in the calling function, whereas in passing by value, this does not happen. We use return statement when we need to return some data from calling function to main function. Syntax is return (data/variable); we need to declare all the functions before or after the include section of the program, this is called prototyping. Function recursion is when a function calls itself repeatedly.