Functions
A function is a set of statements that are executed together when the function is called. Every function has a name, which is used to call the respective function.
Built-in Functions
C++ has many built-in functions. In order to use them, the required library has to be imported using #include
.
Here a function named sqrt()
is made available from the <cmath>
library:
#include <iostream>#include <cmath>int main() {std::cout << sqrt(10);// Output: 3.16228}
Function Declaration & Definition
A C++ function has two parts:
- Function declaration
- Function definition
The declaration includes the function’s name, return type, and any parameters.
The definition is the actual body of the function which executes when a function is called. The body of a function is typically enclosed in curly braces.
#include <iostream>// Function declarationvoid blah();// Main functionint main() {blah();}// Function definitionvoid blah() {std::cout << "Blah blah";}
Void Functions
In C++, if the type of a function is declared as void
, it does not return a value. These functions are useful for a set of statements that do not require returning a value.
#include <iostream>void print() {std::cout << "Hello World!";}int main() {print();}
Return Values
A function that returns a value must have a return
statement. The data type of the return value also must match the method’s declared return type.
On the other hand, a void
function (one that does not return anything) does not require a return
statement.
#include <iostream>int sum(int a, int b);int main() {int r = sum(10, 20);std::cout << r;}int sum(int a, int b) {return(a + b);}
Function Declarations in Header File
C++ functions typically have two parts: declaration and definition.
Function declarations are generally stored in a header file (.hpp
or .h
) and function definitions (body of the function that defines how it is implemented) are written in the .cpp
file.
main.cpp
#include <iostream>#include "functions.hpp"int main() {std::cout << say_hi("Sabaa");}
functions.hpp
// Function declarationstd::string say_hi(std::string name);
functions.cpp
#include <string>#include "functions.hpp"// Function definitionstd::string say_hi(std::string name) {return "Hey there, " + name + "!\n";}
Function Arguments
In C++, the values passed to a function are known as arguments. They represent the actual input values.
#include <iostream>void print(int);int main() {print(10);// The argument 10 is received as input value}// Parameter a is defined for the function printvoid print(int a) {std::cout << a;}
Overloading
The ability to create multiple functions with the same name but different parameters is called function overloading in C++. It provides a way to reuse the same function name in different contexts by accepting different data types and numbers of arguments.
#include <iostream>using namespace std;void add(int a,int b){cout << "Output, when add function is called with integer parameters: " << a+b << "\n";}void add(string a, string b){cout << "Output, when add function is called with string parameters: " << a+b << "\n";}int main() {int a=10,b=10;string s = "Hello",t="World!";add(a,b);add(s,t);}
This will output:
When add function is called with integer parameters: 20When add function is called with string parameters: HelloWorld!
Recursion
Recursion is a technique that allows a function to call itself. In C++, the function that calls itself is called a recursive function:
#include <iostream>using namespace std;int sum(int m) {if (m > 0) {return m + sum(m - 1); // Recursive call} else {return 0; // Base case}}int main() {int result = sum(5);cout << result;return 0;}
This program will output the following result:
15
All contributors
- Christine_Yang
- garanews
- christian.dinh
- Anonymous contributor
- Anonymous contributor
- Anonymous contributor
- bcsamrudh
- Arinze_Obi
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours