Types of Functions
User Defined Function
User-defined functions are user/customer-defined blocks of code specially customized to reduce the complexity of big programs. They are also commonly known as “tailor-made functions” which are built only to satisfy the condition in which the user is facing issues meanwhile reducing the complexity of the whole program.
Library Function
Library functions are also called “built-in Functions“. These functions are part of a compiler package that is already defined and consists of a special function with special and different meanings. Built-in Function gives us an edge as we can directly use them without defining them whereas in the user-defined function we have to declare and define a function before using them.
For Example: sqrt(), setw(), strcat(), etc.
Functions in C++
A function is a set of statements that takes input, does some specific computation, and produces output. The idea is to put some commonly or repeatedly done tasks together to make a function so that instead of writing the same code again and again for different inputs, we can call this function.
In simple terms, a function is a block of code that runs only when it is called.
Syntax:
Example:
C++
// C++ Program to demonstrate working of a function #include <iostream> using namespace std; // Following function that takes two parameters 'x' and 'y' // as input and returns max of two input numbers int max( int x, int y) { if (x > y) return x; else return y; } // main function that doesn't receive any parameter and // returns integer int main() { int a = 10, b = 20; // Calling above function to find max of 'a' and 'b' int m = max(a, b); cout << "m is " << m; return 0; } |
m is 20
Time complexity: O(1)
Space complexity: O(1)