c++ function
In this article, we will learn about the function in c++ and deep dive into the concept of functions. How to make, define, and use of functions in CPP language.
Function in c++
The function is the block of code that performs a particular task.
There are two types of functions in c++ language.
- Built-in function(pre defined function).
- The user defines a function.
Let's take an example, find the square root of the number. This is a problem, we will create a program but we can also solve this problem by making a function.
If the predefined function is available in c++ language then we can use the function by calling easily but if the function is not available in the language then we shall make the user define a function.
The user-defined function allows to user to make his own function and reuse his function whenever need function.
Declaration of function in cpp:
The syntax of declaration of the function:
returntype functonname(parameter1,parameter2,parameter3,....)
{
/////////code
}
For example:
Void display()
{
cout<<" Hello World "<<endl;
}
In the above example, Void is the return type
- The function name is display.
- No parameter pass to function, therefore nothing in the bracket().
- Body of function is the print Hello World.
- No Return type because of the void id return type of function.
Function work in cpp:
function work in c++ |
How to call a function?
Take the above example, how to call this function?
Int main()
{
//calling function
display();
}
Function with parameters :
The function can have parameters for performing the task. The parameter may be int, float, double, pointer, and string. The data passed to the parameter in the two ways
One is Pass by value.
Second is Pass by Reference.
Pass by value :
In the pass by value parameters, new local variables make in the function scope and value of these local variables copy from the value passed in the function during the calling of function.
This is a disadvantage in the sense of memory because new variable create in the memory. Can we perform our task without making new local variables in the function?
The answer is yes. If we make the function with parameter pass by reference then the alias is created without creating a new variable in the memory.
Example:
#include<iostream>
using namespace std;
void fun(int a)
{
cout<<a<<endl;
}
int main()
{
fun(2);
return 0;
}
output:
2
In the above example, the name of the function is fun with one parameter pass by reference and void return type.
How to pass variable to the function bypass by reference?
Pass by reference:
In the pass by reference, the concept of an alias comes. What is an alias? alias means the same person has two names. Same in the pass by reference alias of the variable create without allocated new space in the memory.
This is an advantage of pass by reference but the thing to remember if you call a function which has parameter pass by reference then variable should exist already.
Example:
#include<iostream>
using namespace std;
void fun(int &a)
{
cout<<a<<endl;
}
int main()
{
fun(2);
return 0;
}
Output:
error
In the above code, an error occurs due to parameter pass by reference, and 2 is not a variable. How alias is created. Variable does not exist in the memory that's why alias is not created and error is happening.
Following example with the correct of calling the function having parameter pass by reference.
Example:
#include<iostream>
using namespace std;
void fun(int &a)
{
cout<<a<<endl;
}
int main()
{
int num=2;
fun(num);
return 0;
}
Output:
2
In this code, variable passed into the function then alias create in the function. Variable exist in memory and then the alias is created.
Please write the comment,if you find anything incorrect,any problem in the above topic or you want to share more information about the above topic.
Comments
Post a Comment