Friday, May 3, 2019

Types of function


Types of function

We have two types of function in C++:
1) Built-in functions
2) User-defined functions

1) Build-it functions

Built-in functions are also known as library functions. We need not to declare and define these functions as they are already written in the C++ libraries such as iostream, cmath etc. We can directly call them when we need.

Example: C++ built-in function example

Here we are using built-in function pow(x,y) which is x to the power y. This function is declared in cmath header file so we have included the file in our program using #include directive.
#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(3,2);
    return 0;
}

Output:
9

2) User-defined functions


We have already seen user-defined functions, the example we have given at the beginning of this tutorial is an example of user-defined function. The functions that we declare and write in our programs are user-defined functions. Lets see another example of user-defined functions.

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>> 

Types of function

We have two types of function in C++:
1) Built-in functions
2) User-defined functions

1) Build-it functions

Built-in functions are also known as library functions. We need not to declare and define these functions as they are already written in the C++ libraries such as iostream, cmath etc. We can directly call them when we need.

Example: C++ built-in function example

Here we are using built-in function pow(x,y) which is x to the power y. This function is declared in cmath header file so we have included the file in our program using #include directive.
#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,3);
    return 0;
}

Output:
8

2) User-defined functions


We have already seen user-defined functions, the example we have given at the beginning of this tutorial is an example of user-defined function. The functions that we declare and write in our programs are user-defined functions. Lets see another example of user-defined functions.

User-defined functions

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

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

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

   cout<<"Sum of these two :"<<sum(a,b);
   return 0;
}
//Defining the function sum
int sum(int x, int y) {
   int c = x+y;
   return c;
Output:
enter first number: 22
enter second number: 19
Sum of these two :41

No comments:

Post a Comment

Array

Arrays  An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by ad...