Now, when we have learned that what are functions and how can a user declare a function in C++ we can take a look at the various types of functions that one can create in C++. 

Here, are the following types of functions in C++:

  • Built-In Functions
  • User-Defined Functions

Built-In Functions:

As the name suggests, these are the functions that are available in the C++ standard libraries and are already defined. If the programmer wants to use any of these functions then he can do that easily. An example of the built-in function in C++ is the main().

User-Defined Functions:

The second type of functions are the user-defined, that is those which are defined and declared as per the wish of the user.

There are different types of user-defined functions also, they can be classified on the basis of the return type and the number of arguments that they accept from the calling function.

There are four types of user-defined functions in C++:

  1. No return type, with no Arguments.
  2. Return type, with no Arguments.
  3. No return type, with Arguments.
  4. Return type, with Arguments.

No return type, with no Arguments:

The function which does not accept any arguments and has void as the return type.

Example:

void even_number()

{

cout<<"The even numbers between 1 to 20 are: \n"

for (int i=1;i<=20;i++)

{

if(i%2==0)

{

cout<<i;

}

}

Return type, with no Arguments:

The function which does not accept any arguments and can have any data type as the return type, except the void.

Example:

int even_number()

{

cout<<"The even numbers between 1 to 20 are: \n"

for (int i=1;i<=20;i++)

{

if(i%2==0)

{

cout<<i;

}

return 0;

}

No Return type, with Arguments:

The function which accepts arguments from the calling function and has void as the return type.

Example:

void even_number(int n)

{

cout<<"The even numbers between 1 to “<< n << “are: \n";

for (int i=1;i<=n;i++)

{

if(i%2==0)

{

cout<<i;

}

}

Return type, with Arguments:

The function which accepts some arguments and can have any data type as the return type, except void.

Example:

int even_number(int n)

{

cout<<"The even numbers between 1 to “<< n << “are: \n";

for (int i=1;i<=n;i++)

{

if(i%2==0)

{

cout<<i;

}

return 0;

}

 

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019