A capability is a bunch of explanations that takes input, does some particular calculation, and produces yield. The thought is to put some normally or more than once taken care of errands together to make a capability so that as opposed to composing a similar code over and over for various data sources, we can call this capability.
In basic terms, a capability is a block of code that runs just when it is called.
Syntax:
Syntax of Function
Example:
C++
// C++ Program to demonstrate working of a function
#include <iostream>
usingnamespacestd;
// Following function that takes two parameters 'x' and 'y'
// as input and returns max of two input numbers
intmax(intx, inty)
{
if(x > y)
returnx;
else
returny;
}
// main function that doesn't receive any parameter and
// returns integer
intmain()
{
inta = 10, b = 20;
// Calling above function to find max of 'a' and 'b'
intm = max(a, b);
cout << "m is "<< m;
return0;
}
Output
m is 20
For what reason Do We Want Capabilities?
Capabilities help us in diminishing code overt repetitiveness. On the off chance that usefulness is performed at various spots in programming, as opposed to composing a similar code, over and over, we make a capability and call it all over the place. This additionally assists in support as we with making changes in just a single spot in the event that we make changes to the usefulness in future.
Capabilities make code particular. Consider a major record having many lines of code. It turns out to be truly easy to peruse and utilize the code, in the event that the code is partitioned into capabilities.
Capabilities give deliberation. For instance, we can utilize library capabilities without stressing over their inward work.
Capability Announcement
A capability statement educates the compiler concerning the quantity of boundaries, information kinds of boundaries, and returns sort of capability. Composing boundary names in the capability statement is discretionary yet placing them in the definition is essential. The following is an illustration of capability statements. (boundary names are absent in the underneath statements)
Here, we have declared a variable point_var which is a pointer to an int.
We can also declare pointers in the following way:
int* point_var; // preferred syntax
Assigning Addresses to Pointers:
Here is how we can assign addresses to pointers:
int var = 5;
int* point_var = &var;
Here, 5 is assigned to the variable var. And the address of var is assigned to the point_var pointer with the code point_var = &var.
Note: It is a good practice to initialize pointers as soon as they are declared.
Get the Value from the Address Using Pointers:
To get the value pointed by a pointer, we use the * operator. For example:
int var = 5;
// assign address of var to point_varint* point_var = &var;
// access value pointed by point_varcout << *point_var << endl; // Output: 5
In the above code, the address of var is assigned to point_var. We have used the *point_var to get the value stored in that address.
When * is used with pointers, it's called the dereference operator. It operates on a pointer and gives the value pointed by the address stored in the pointer. That is, *point_var = var.
Note:In C++, point_var and *point_var are completely different. We cannot do something like *point_var = &var;. Here, point_var is a pointer that stores the address of variable it points to while *point_var returns the value stored at the address pointed by point_var.
Example 1: Working of C++ Pointers
#include<iostream>usingnamespacestd;
intmain(){
int var = 5;
// store address of varint* point_var = &var;
// print value of varcout << "var = " << var << endl;
// print address of varcout << "Address of var (&var) = " << &var << endl
<< endl;
// print pointer point_varcout << "point_var = " << point_var << endl;
// print the content of the address point_var points tocout << "Content of the address pointed to by point_var (*point_var) = " << *point_var << endl;
return0;
}
var = 5
*point_var = 5
Changing value of var to 7:
var = 7
*point_var = 7
Changing value of *point_var to 16:
var = 16
*point_var = 16
Here point_var holds the address of var, and by dereferencing point_var with *point_var, we can access and modify the value stored at that address, which in turn affects the original variable var.
Common Mistakes When Working with Pointers:
Suppose we want a pointer point_var to point to the address of var. Then,
int var = 5;
// Wrong! // point_var is an address but var is notint* point_var = var;
// Wrong!// &var is an address// *point_var is the value stored in &var
*point_var = &var;
// Correct! // point_var is an address and so is &var
point_var = &var;
// Correct!// both *point_var and var are values
*point_var = var