Functions are used for Placing or Storing the Code which is to be Repeated Several Times. For Example, if we need Same Code, then we must have to Write that Code Again and Again So that for Removing this Task, we uses functions.
The Functions are Some Storage Area which Contains set of Statements and the Function Executes all the Contained Statements when a Special Call is made to them. Once a Code is stored in the Function, then we can Store that Function any time and Any Time we can call that Functions.
Functions are used for performing the repetitive task or we can say the functions are those which provides us the better efficiency of a program it provides us the facility to make a functions which contains a set of instructions of the repetitive types or we need them in a program at various places Thus a functions provides us the ability to make a function which contains a code and then use them when a functions can call then it executes the statements those are contained in it.
We’ll be covering the following topics in this tutorial:
Functions Provides us Following Features
• Reusability of Code : Means Once a Code has Developed then we can use that Code any Time.
• Remove Redundancy: Means a user doesn’t need to Write Code Again and Again.
• Decrease Complexity: Means a Large program will be Stored in the Two or More Functions. So that this will makes easy for a user to understand that Code.
There are Two Types of Functions
• Built in Functions
• User Defined functions
The Functions those are provided by C Language are refers to the Built in Functions For example. cin and cout, getch , Clrscr are the Examples of Built in Functions. So that all the functions those are provided by the C++ Language are Pre defined and Stored in the Form of header Files so that a user doesn’t need to Know how this Function has developed and a user just use that Function.
But in the other hand the Functions those are developed by the user for their Programs are known as User Defined Programs. When a user wants to make his Own Function, then he must have to follow the Following Operations.
• Function Declaration or Prototyping
• Function Defining
• Calling a Function
1) Function Declaration or Prototyping:For using a Function a user must have to declare a Function. The Function declaration contains the Name of Function, Return type of the Function and also the Number of Arguments that a User will takes for performing the Operation.
The Function Prototyping Contains
1) Return Type of a function: The Return Function determines whether a Function will return any value to the Function. If a Function is declared with the void Keyword or if a Function Contains a void then that’s means a Function Never Returns a value. Means a Function will Executes his statements one by one. And if a Function Contain any other data type means if a Function Contains int or Float then the Function must return a value to the user.
2) Name of Function : The Name of Function must be valid and the name of function must be Start from any Alphabet and the Name of Function doesn’t Contains any Spaces and the Doesn’t Contains any Special Character For Example Space , * sign etc.
3) Argument List: A Function may have zero or More Arguments. So that if we want to call a Function. Then we must have to Supply Some Arguments or we must have to pass some values those are also called as the Argument List. So that The Argument List is the total Number of Arguments or the Parameters those a Function Will takes. So that We must have Supply Some Arguments to the Functions,. The Arguments those are used by the Function Calling are known as the Actual Arguments and the Arguments those are used in the Function declaration are Known as the Formal Arguments, When we call any Function then the Actual Arguments will Match the Formal Arguments and if a proper Match is Found, then this will Executes the Statements of the Function otherwise this will gives you an error Message.
There are Two Ways for Calling a Function
1) Call by Value:-when we call a Function and if a function can accept the Arguments from the Called Function, Then we must have to Supply some Arguments to the Function. So that the Arguments those are passed to that function just contains the values from the variables but not an Actual Address of the variable.
So that generally when we call a Function then we will just pass the variables or the Arguments and we doesn’t Pass the Address of Variables , So that the function will never effects on the Values or on the variables. So Call by value is just the Concept in which you must have to Remember that the values those are Passed to the Functions will never effect the Actual Values those are Stored into the variables.
2) Call By Reference :-When a function is called by the reference then the values those are passed in the calling functions are affected when they are passed by Reference Means they change their value when they passed by the References. In the Call by Reference we pass the Address of the variables whose Arguments are also Send. So that when we use the Reference then, we pass the Address the Variables.
When we pass the Address of variables to the Arguments then a Function may effect on the Variables. Means When a Function will Change the Values then the values of Variables gets Automatically Changed. And When a Function performs Some Operation on the Passed values, then this will also effect on the Actual Values.