Functions in C++
A function in C++ is a group of statements that performs a specific task. You need to give a particular name to the function and write some logic inside the function. Then you can invoke that function from the main function.
The syntax for creating a function is as follows:
returnType functionName(parameter1, parameter2,...) {
//function body
}
A function in C++ should have a return_type, a function_name, parentheses with or without parameters and the function body.
- return type - The return type is the data type of the value that the function should will return.
- function name - A unique name that identifies that function.
- parameters - Parameters are variables which are used to pass data or object inside the function. Parameters are declared within parentheses. Example: (x, y)
- function body - The body of the function is where the logic of the code is written.
Function Declaration
A function can be declared by writing its return type, function name, and list of parameters inside the parentheses “( )” or bracket. The function declaration It tells the compiler that the particular function existsis present there. A function must be declared first before it can be called.
Example
int add(int x, int y);
Here in the above example, the return type is int which means the function should return an integer value, add is the function name, x and y are int type parameters for passing two digit into the function, the value of x and y are added and assign to result variable, the return keyword is used to return the value of result variable.
Function Definition
A function definition contains the body of the function. The declaration and definition of a function can be combined and used together, but it should be done before calling it.
Example
int add(int x, int y)
{
int result = x + y;
return result;
}
Calling Functions in C++
The function can be called from anywhere in the program depending upon our requirement. A function can be called from the main function and also from other functions in the program. When a function is invoked, the execution control is transferred to the function that is called to perform its task. After the task is complete, it returns the control to the main function.
Types of Functions in C++
There are two types of functions in C++:
- Built-in functions — These are functions that are already present in C++ standard library. We need not write them ourselves. We can directly use them in our code. They are also called library functions. The built-in functions can be used in a program by importing them via importing them are put in the header files of C++. For example, <cmath> header file contains in-built math functions and <string> header file contains string functions.
- User-defined functions — These functions are created by the user themselves, where the user gives their own declaration and definition.
Example
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name = "Jelly";
cout << "Name : " << name;
}
Output
The general syntax for user-defined functions is given below:
return_type functionName(param1, param2,…,param3)
{
//function body;
}
Example
#include <iostream>
using namespace std;
// declaring a function
void greet()
{
cout << "Hello there!";
}
int main()
{
greet(); // calling the function
return 0;
}