Functions in C++
Functions in C++
Function is a piece of code written to perform a specific task. Every C++ program must have at least one function to execute. We have already used main function in the previous tutorials. If a C++ program doesn’t have main function then the program cannot be executed. Apart from main function, we can define other functions as well. When a program is executed then main function is called automatically while other functions are called when required.
Syntax
C++ function can be defined as below.
return-type function_name(parameters)
{
funtion’s body
}
where:
The following example shows that how a function is defined and called.
The output of the above program is as below.
In the above program we can see that there are two functions main and factorial. The keyword long means that the function factorial will return a long integer value while it will receive an integer value when being called. This value will be stored in variable n. The keyword return is used to get the result back from the function. The statement in line 22 is called function calling. The value of variable ‘a’ is passed to function factorial and the value to be returned from factorial will be stored in variable ‘f’.
It is also important to note that function factorial is defined before function main. If we define the function factorial after the main function then the program will raise the following error during compilation.
error C3861: ‘factorial’: identifier not found
This error means that the identifier factorial is not known to the main function in line 22. In order to avoid this error either write the function factorial before main function or declare factorial within main as below.
long factorial(int);
This is called function declaration. It tells the compiler that a function having name factorial will be called which will accept an integer value and will return a long integer.
Passing Parameters By Value
In the last example parameter was passed by value. Passing parameter by value means that the value of sending parameter (variable ‘a’ in our example) is copied to the receiving parameter (variable ‘n’ in our example) and after that if any changes are made in receiving parameter then the value of sending parameter is not affected.
Passing Parameters By Reference
Sometimes it may be required that if we change the value of receiving parameter then the value of actual parameter should also be changed. For doing so we pass the parameters by reference as shown below.
If we want to send a parameter by reference, then we need to send the address of the variable instead of its value as shown in line 17 above. In line 17, variable ‘a’ and ‘b’ are passed by values where as variables ‘c’ and ‘d’ are passed by reference. The & sign with a variable means that address of this variable will be passed instead of its value. At the receiving end in line 5, first two parameters are received by values and the values are stored in variables ‘x’ and ‘y’ respectively. The last two variable are received by reference and means that variables ‘sum’ and ‘diff’ will refer to the same memory addresses being used by variable ‘c’ and ‘d’ respectively. So if the values of variables ‘sum’ and ‘diff’ are changed then this change is also reflected in variables ‘x’ and ‘y’ as shown in the output of the above code.
More Related Articles For You
- What is C++
- C++ and Object Oriented Programming OOPS concepts
- Syntax and Structure of C++ program
- Data Types in C++
- C++ Variables
- Types of operators in C++
- Decision making in C++
- C++ Loop Types
- Storage Classes in C++
- Classes and Objects in C++
- Access controls in C++ Classes
- Defining Class and Object in C++
- Accessing Public and Private Data Members in C++
- Member Functions in Classes
- Types of Member Functions in C++
- Inline Functions in C++
- Namespaces in C++
- Function Overloading in Classes in C++
- Constructors and Destructors in C++
- Static Keywords in C++
- Const Keyword in C++ Programming
- References in C++
- Copy Constructor in C++
- Pointer to Members in C++ Classes
- Introduction to Inheritance
- Types of Inheritance
- Order of Constructor Call in Inheritance
- Upcasting in C++
- Function Overriding in C++
- Virtual Functions in C++
- Abstract Class and Pure Virtual Functions in C++
- Virtual Destructors in C++
- Operator Overloading in C++
C++ Tutorial
C++ Quizzes