Blog
25+ Best FREE CAD Software (2021)
CAD software refers to a type of software program used by engineers and designers to create 2D and 3D...
A function in C++ refers to a group of statements that takes input, processes it, and returns an output. The idea behind a function is to combine common tasks that are done repeatedly. If you have different inputs, you will not write the same code again. You will simply call the function with a different set of data called parameters.
Each C++ program has at least one function, the main() function. You can divide your code into different functions. This division should be such that every function does a specific task.
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:
There are numerous benefits associated with the use of functions. These include:
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.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num, squareRoot;
cout << "Enter number: ";
cin >> num;
squareRoot = sqrt(num);
cout << "The square root of " << num << " is: " << squareRoot;
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
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.
#include <iostream>
using namespace std;
void sayHello() {
cout << "Hello!";
}
int main() {
sayHello();
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
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.
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.
return_datatype function_name( parameters) {
function body
}
From the above, a function definition has the function header and body. Here is an explanation of the parameters:
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.
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.
#include <iostream>
using namespace std;
int addFunc(int, int);
int main() {
int x, y, sum;
cout << "Enter two numbers: ";
cin >> x >> y;
sum = addFunc(x, y);
cout <<"The sum of "<<x<< " and " <<y<<" is: "<<sum;
return 0;
}
int addFunc(int num1, int num2) {
int addFunc;
addFunc = num1 + num2;
return addFunc;
}
Output:
Here is a screenshot of the code:
Code Explanation:
CAD software refers to a type of software program used by engineers and designers to create 2D and 3D...
What is R Software? R is a programming language and free software developed by Ross Ihaka and...
What is Histogram? Histogram is a type of bar chart that is used to represent statistical...
PC optimization improves the life of your PC, and prevents the virus, bugs, malware from infecting your...
Fiverr is a website that helps you to get your job as a freelancer. It offers jobs related to...
Before we learn about MEAN Stack Developer, let's understand- What is Mean Stack? Mean Stack...