There are many built-in functions in the C++ standard library. You can call these functions within your program. In this C++ tutorial, you will learn:

What is a Function in C++?
Why use functions?
Built-in Functions
User-Defined Functions
Function Declaration/Prototype
Function Definition
Syntax:
Function Call
Passing Arguments

Why use functions?

There are numerous benefits associated with the use of functions. These include:

Each function puts related code together. This makes it easier for programmers to understand code. Functions make programming easier by eliminating code repetition. Functions facilitate code reuse. You can call the same function to perform a task at different sections of the program or even outside the program.

Built-in Functions

In C++ library functions are built-in C++ functions. To use these functions, you simply invoke/call them directly. You do not have to write the functions yourself.

Example 1:

Output:

Here is a screenshot of the code:

Code Explanation:

Include the iostream header file in our program to use its functions. Include the cmath library to use its functions. We want to use the function sqrt() defined in it. Include the std namespace in our code to use its classes without calling it. Call the main() function. The program logic should be added within the body of this function. Declare two double variables, num, and squareRoot. Print some text on the console. The text asks the user to enter a number. Read user input from the keyboard. The input will become the value of variable num. Call the library function sqrt(), which calculates the square root of a number. We passed the parameter num to the function, meaning it will calculate the square root of num. This function is defined in the cmath library. Print the number entered by the user, its square root, and some other text on the console. The program must return value upon successful completion. End of the body of the main() function.

User-Defined Functions

C++ allows programmers to define their own functions. The purpose of the function is to group related code. The code is then given a unique identifier, the function name. The function can be called/invoked from any other part of the program. It will then execute the code defined within its body.

Example 2:

Output:

Here is a screenshot of the code:

Code Explanation:

Include the iostream header file in our program to use its functions. Include the std namespace in our code to use its classes without calling it. Create a user-defined function named sayHello(). Print some text on the console when the sayHello() function is called. End of the body of the sayHello() function. Call the main() function. The program logic should be added within the body of this function. Call/invoke the function named sayHello(). The program must return value upon successful completion. End of the body of the main() function.

Function Declaration/Prototype

If you define a user-defined function after the main() function, the C++ compiler will return an error. The reason is that the compiler does not know the details of the user-defined function. The details include its name, the types of arguments, and their return types. In C++, the function declaration/prototype declares a function without a body. This gives the compiler details of the user-defined function.

In the declaration/prototype, we include the return type, the function name, and argument types. The names of arguments are not added. However, adding the argument names generates no error.

Function Definition

The purpose of the function declaration is to tell the C++ compiler about the function name, the return type, and parameters. A function definition tells the C++ compiler about the function body.

Syntax:

From the above, a function definition has the function header and body. Here is an explanation of the parameters:

return_datatype- Some functions return value. This parameter denotes the data type of the return value. Some functions don’t return value. In that case, the value of this parameter becomes void. function_name- This is the name of the function. The function name and parameters form the function signature. Parameters- This is the type, the order, and the number of function parameters. Some functions don’t have parameters. function body- these are statements defining what the function will do.

Function Call

For a function to perform the specified task and return output, it must be called. When you call a function, it executes the statements added within its body.

A program is called by its name. If the function takes parameters, their values should be passed during the call. If the service takes no parameters, don’t pass any value during the call.

Passing Arguments

In C++, an argument/parameter is the data passed to a function during its call. The values must be initialized to their respective variables. When calling a function, the arguments must match in number. The means the values you pass must be equal to the number of parameters. Again, the values must also match the parameters in terms of type. If the first parameter is an integer, the value passed to it must be an integer. One can assign default values to function parameters. If you don’t pass a value for the parameter during the function call, the default value will be used.

Example 3: How to Write and Call a Function

Output:

Here is a screenshot of the code:

Code Explanation:

Include the iostream header file in our program to use its functions. Include the std namespace in our code to use its classes without calling it. Declare a function named addFunc() that takes two integer parameters. This creates the function prototype. Call the main() function. The program logic should be added within the body of this function. Declare three integer variables, x, y, and sum. Print some text on the console. The text asks the user to enter two numbers. Read the user input from the keyboard. The user should enter two numbers for variables x and y, separated by space. Call the function addFunc() and passing to it the parameters x and y. The function will operate on these parameters and assign the output to the variable sum. Print the values of variables x, y, and sum on the console alongside other text. The function must return value upon successful completion. End of the body of the main() function. Function definition. We are defining the function addFunc(). We will state what the function will do within its body, the { }. Declaring an integer variable named addFunc. Adding the values of parameters num1 and num2 and assigning the result to variable addFunc. The addFunc() function should return the value of addFunc variable. End of the function body, that is, function definition.

Summary:

A function in C++ helps you group related code into one. Functions facilitate code reuse. Instead of writing similar code, again and again, you simply group it into a function. You can then call the function from anywhere within the code. Functions can be library or user-defined. Library functions are the functions built-in various C++ functions. To use library functions, you simply include its library of definition and call the function. You don’t define the function. User-defined functions are the functions you define as a C++ programmer. A function declaration tells the compiler about the function name, return type, and parameter types. A function definition adds the body of the function. If a function takes parameters, their values should be passed during the function call.