C++ Function

Learn about function declaration.

* In C++, functions are blocks of code that perform a specific task, and a function must be defined before it is used anywhere in the program.

Concept of Function

A function is a block of code that performs a specific task and can be called from other parts of the program. Functions provide a way to modularize code, making it more organized, readable, and reusable.

In C++ language, User-defined functions, and built-in functions are the main two types of functions in programming, and they serve different purposes:

Built-in Functions:

Built-in functions, also known as standard library functions or pre-defined functions, are functions that come as part of a programming language's standard library. These functions are provided by the language developers to perform common tasks, and they are readily available for use without requiring the programmer to implement them from scratch. Built-in functions cover a wide range of functionalities, from basic mathematical operations to more complex tasks.

Here's is an examples of some in C++ built-in functions are, 'sqrt' (square root), 'sin' (sine), 'cos' (cosine), etc.

cpp Copy Code
#include<iostream>
#include<cmath>

int main() {
    double number;

    // Get input from the user
    std::cout << "Enter a number: ";
    std::cin >> number;

    // Check if the entered number is non-negative
    if (number >= 0) {
        // Calculate and display the square root
        double sr = sqrt(number);
        std::cout << "Square root of " << number << " is: " << sr << std::endl;
    } else {
        std::cout << "Error: Negative number." << std::endl;
    }

    return 0;
}

#include<cmath> : Include the cmath header for the sqrt function.

Output:
Enter a number: 4
Square root of 4 is: 2

* This program prompts the user to enter a number, calculates the square root using the sqrt function from the '<cmath>' header, and then displays the result.

User-defined Functions:

User-defined functions are functions created by the programmer to perform specific tasks within their program. These functions are defined by the user to encapsulate a set of instructions, making the code modular and more readable. User-defined functions allow for code reuse and abstraction, making the overall program structure more organized.

you can create a user-defined function by specifying its name, return type, parameters, and a block of code that defines the function's behavior. The function can then be called from other parts of the program. Here's a simple example of a user-defined function in C++:

cpp Copy Code
#include<iostream>

// Function to add two numbers
int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(5, 7); // Calling the function
    std::cout << "The sum is: " << result << std::endl;

    return 0;
}
Output:
The sum is: 12

Note: In this example, 'add' is a user-defined function that takes two parameters ('a' and 'b') and returns their sum.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.