C++

Functions In C++ With Example – Q&A

A function is a block of code that is used to carry out a specific task. For instance, if you were writing a large C++ program and wanted to carry out a specific task repeatedly, like displaying values from 1 to 10, you would need to write a few lines of code and repeat them each time you displayed values. You can also create a function that contains these lines of code and call it each time you wish to display values. Your code would become easier to read, understand, and reuse.

Syntax of Function

return_type function_name (parameter_list)
{
   //C++ Statements
}

To better comprehend this idea, let’s use a straightforward example.

An example of a basic function

#include <iostream>
using namespace std;
/* This function adds two integer values
 * and returns the result
 */int
sum(int num1, int num2){
   int num3 = num1+num2; return num3;
}

int main(){
   //Calling the function
   cout<<sum(1,99);
   return 0;
}

Output:

100

The identical program can be written as follows: Well, the purpose of this program is to help you grasp function declaration, a crucial concept in the world of functions. Let’s look at the program first, and then we’ll talk about function declaration, definition, and calling at the end.

#include <iostream>
using namespace std;
//Function declaration
int sum(int,int);

//Main function
int main(){
   //Calling the function
   cout<<sum(1,99);
   return 0;
}
/* Function is defined after the main method 
 */
int sum(int num1, int num2){
   int num3 = num1+num2;
   return num3;
}

Function Declaration: As you can see, I wrote the identical program in two different methods. The first program lacked any function declarations, whereas the second version starts with a function declaration. The point is that when you define the function in your program before the main() method, you don’t need to declare it first. However, if you write your function after the main() function, as we did in the second program, you must do so in order to avoid a compilation error.

syntax of function declaration:

return_type function_name(parameter_list);

Note: While providing parameter_list you can avoid the parameter names, just like I did in the above example. I have given int sum(int,int); instead of int sum(int num1,int num2);.

Function definition: Writing the full body of function is known as defining a function.
syntax of function definition:

return_type function_name(parameter_list) {
    //Statements inside function
}

Calling function: We can call the function like this:

function_name(parameters);

Now that we are aware of how functions operate, let’s look at the different sorts of functions in C++.

Types of function

We have two types of function in C++:
C++ types of functions: built-in and user-defined

1) Built-in functions
2) User-defined functions

1) Included features

The term “library functions” also applies to built-in functions. Since these functions are already present in C++ libraries like iostream and cmath, we don’t need to declare or define them. When necessary, we can phone them directly.

An illustration is a built-in function in C++.

X to the power of y is represented in this case by the built-in function pow(x,y). Since this function is declared in the cmath header file, we have used the #include command to include the file in our application.

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    /* Calling the built-in function 
     * pow(x, y) which is x to the power y
     * We are directly calling this function
     */
    cout<<pow(2,5);
    return 0;
}


Output:

32

2) User-defined functions

C++ functions

User-defined functions have been demonstrated to us previously; the tutorial’s opening example is one such instance. User-defined functions are those that we declare and include in our programs. Let’s look at another user-defined function example.

User-defined functions

#include <iostream>
#include <cmath>
using namespace std;
//Declaring the function sum
int sum(int,int);

int main(){
   int x, y;
   cout<<"enter first number: ";
   cin>> x;

   cout<<"enter second number: ";
   cin>>y;

   cout<<"Sum of these two :"<<sum(x,y);
   return 0;
}
//Defining the function sum
int sum(int a, int b) {
   int c = a+b;
   return c;
}

Output:

enter first number: 22
enter second number: 19
Sum of these two :41

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button