In this section we are going to learn how to make a program using functions. Instead of copying a procedure or algorithm each time we need to use it we will learn how to create a block which will be called during the execution of the program or in this case your code. Before we can master the function as a tool in c++ program you need to learn how function are declared and defined. You don’t need to worry where you will define the function in you program because you can declare and define it anywhere but important thing is that you cannot declare it under the main function. Under the function main you need to call function.
So up to now the programs that you’ve written are just like a straight line which has starting point in this case general libraries like stdfx.h or iostream or math.h or algorithm etc. and the ending point is after the main function for example the closing curly brackets. When you’re using functions the execution of the program is a little bit different. After the standard libraries that you’ve included into your program (in order to run program correctly) let’s say that you have defined and decalred the function called add and the function role is to make addition with values of variables a and b. After the function declaration there is a function main and when compiler gets to the point of function call he will immediately go “outside” of function main into the function add. After the function is done the program is back into function main and continues the execution.
- WHEN AND WHY FUNCTIONS ARE USED?
- DECLARATION AND DEFINITION OF FUNCTIONS
- DECLARATION OF FUNCTIONS IN HEADER FILES
- TYPE OF FUNCTIONS
- LIST OF ARGUMENTS
- FUNCTIONS WITHOUT ARGUMENTS
- TRANSFERRING ARGUMENTS BY VALUE
- POINTERS AND REFERENCE AS AN ARGUMENTS
- CHANGE OF POINTERS INSIDE FUNCTIONS
- ARRAYS AS ARGUMENTS
- CONSTANT ARGUMENTS