TYPES OF FUNCTIONS IN C++

In this article, we are going to learn about the types of functions in c++

In C++, there are broadly two types of functions

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

User-defined functions

User-defined functions are nothing but custom functions that are manually developed by the user to reduce the complexity and redundancy of the big programs. These user-defined functions are also known as tailor-made functions, resembling that they are built only to satisfy the conditions of the user.

Built-in functions

Built-in functions are also known as library functions. These library functions are pre-defined, which are already having some specific functionality and these functions can be accessed using the specific header files available in C++. these library functions allow us to access the programs directly without defining them likewise we do for user-defined functions.

Some of the most popularly used library functions in C++ are

  • pow()
  • sqrt()
  • min()
  • max()
  • gcd()
  • toupper()
  • tolower()
  • floor()
  • ceil()

TYPES OF ARGUMENTS IN C++

Default arguments in C++

Default arguments of a function are nothing but assigning default values to the parameters during function declaration. These default values will be automatically assigned by the compiler if the calling function doesn’t provide a value to the argument. If the calling function provides a value to the argument, then the default value gets overridden.

Let’s look at a sample program implementing default arguments.

The output of the program is.

When we called the function for the first time, we only passed two arguments which will be assigned to the variables x and y. the default values assigned to z and w in the function declaration didn’t get overridden as there are no extra parameters passed in calling the function.

See also  Types of Arrays

When we called the function for the second time, we passed three arguments which will be assigned to the first three parameters, x, y and z. the default value of z which is assigned during the function declaration will be overridden as we passed arguments in the function call. There is no overriding of the default value to the parameter z as there is no passing of argument in the function call.

When we called the function for the third time, we passed four arguments for the four parameters. So, these four parameters get into the act and the values which are passed as arguments in the function call override the default values.

This is all about default parameters.

Const argument function in C++

In C++, an argument to a function can be declared as constant. When an argument is declared as constant and if we try to modify the value, the compiler generates an error as the condition is violated. This type of declaration is valid only when we pass parameters by reference or pointers.

For making a function or an argument constant, we use the const keyword.

Let’s look at an example program implementing the const keyword.

The output of the program is.

Inline functions

The inline functions are a C++ enhancement feature to increase the execution time of a program. Functions can be instructed to the compiler to make them inline so that the compiler can replace those function definitions wherever they are being called. The compiler replaces the definition of inline functions at compile time instead of referring function definition at runtime.  

See also  Constructors in C++

Advantages of inline functions

  • It speeds up your program by avoiding function calling overhead.
  •  It saves the overhead of variables push/pop on the stack when function calling happens.
  • It saves the overhead of a return call from a function.
  • It increases the locality of reference by utilizing an instruction cache.

Disadvantages of inline functions

  • It increases the executable size due to code expansion.
  •  When used in a header, it makes your header file larger with information which users don’t care about.
  • C++ inlining is resolved at compile time. This means if you change the code of the inlined function, you would need to recompile all the code using it to make sure it will be updated

Let’s look at a sample program implementing inline functions

The output of the program is.

Conclusion

That’s it from this tutorial. We learned about functions, different types of functions and various methods of passing parameters of functions. Hope you find it interesting. Happy coding!

Leave a Comment

Your email address will not be published.

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh