Tuesday 14 May 2019

Library Function or built in function

A function is a group of statements that together perform a task. Every C++ program has at least one function, which is main(), and all the most trivial programs can define additional functions.
You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually is such that each function performs a specific task.
In programming, function refers to a segment that groups code to perform a specific task.
Depending on whether a function is predefined or created by programmer; there are two types of function:
  1. Library Function
  2. User-defined Function

Library Function

Library functions are the built-in function in C++ programming.
Programmer can use library function by invoking function directly; they don't need to write it themselves.

User-defined Function

C++ allows programmer to define their own function.
A user-defined function groups code to perform a specific task and that group of code is given a name(identifier).
When the function is invoked from any part of program, it all executes the codes defined in the body of function.

How user-defined function works in C Programming?

Working of function in C++ programming
When a program begins running, the system calls the main() function, that is, the system starts executing codes from main() function.
When control of the program reaches to function_name() inside main(), it moves to void function_name() and all codes inside void function_name() is executed.
Then, control of the program moves back to the main function where the code after the call to the function_name() is executed as shown in figure above.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double number, squareRoot;
    cout << "Enter a number: ";
    cin >> number;

    // sqrt() is a library function to calculate square root
    squareRoot = sqrt(number);
    cout << "Square root of " << number << " = " << squareRoot;
    return 0;
}


#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;
}

No comments:

Post a Comment

Directory implementation

The selection of directory-allocation and directory-management algorithms significantly affects the efficiency, performance, and reliabil...