Functions in c++

Functions in c++

Functions in C++ are self-contained blocks of code that perform a specific task. They are used to break a program into smaller, more manageable pieces, making it easier to read, write, and maintain. Functions can be called from anywhere in the program, and they can return a value or perform an action.

Here is the syntax for a function in C++:

return_type function_name(parameter1, parameter2, ...) {
    // code to be executed
    return value; // optional
}

The return_type is the type of value that the function will return (e.g. int, double, void). The function_name is a unique name for the function, and the parameters are values that can be passed to the function.

Here is an example of a function in C++:

#include <iostream>
using namespace std;

int square(int x) {
    int result = x * x;
    return result;
}

int main() {
    int num = 5;
    int squared = square(num);
    cout << "The square of " << num << " is " << squared << endl;
    return 0;
}

n this example, the function "square" takes an integer parameter "x" and returns the square of that integer. The function is called within the main function and the result is stored in a variable "squared". The output will be:

The square of 5 is 25

Functions can also be defined to return no value, which is denoted by the "void" keyword. Here is an example of a void function:

#include <iostream>
using namespace std;

void hello() {
    cout << "Hello, world!" << endl;
}

int main() {
    hello();
    return 0;
}

In this example, the function "hello" prints a message to the console, but does not return a value. The function is called within the main function. The output will be:

Hello, world!

Function Types

1) Functions with no arguments and no return value

void functionName() {
    // code to be executed
}

This type of function does not take any arguments and does not return a value. It is used when the function needs to perform some action, such as printing a message to the console.

Here is an example:

#include <iostream>
using namespace std;

void printHello() {
    cout << "Hello, world!" << endl;
}

int main() {
    printHello();
    return 0;
}

Output:

Hello, world!

2) Functions with arguments and no return value:

void functionName(argument1, argument2, ...) {
    // code to be executed
}

This type of function takes one or more arguments but does not return a value. It is used when the function needs to perform some action with the arguments.

Here is an example:

#include <iostream>
using namespace std;

void printSum(int a, int b) {
    int sum = a + b;
    cout << "The sum of " << a << " and " << b << " is " << sum << endl;
}

int main() {
    printSum(5, 3);
    return 0;
}

Output:

The sum of 5 and 3 is 8

3) Functions with arguments and a return value:

returnType functionName(argument1, argument2, ...) {
    // code to be executed
    return value;
}

This type of function takes one or more arguments and returns a value. It is used when the function needs to perform some computation and return the result.

Here is an example:

#include <iostream>
using namespace std;

int calculateSum(int a, int b) {
    int sum = a + b;
    return sum;
}

int main() {
    int result = calculateSum(5, 3);
    cout << "The result is " << result << endl;
    return 0;
}

Output:

The result is 8

4) Functions with no arguments and a return value:

returnType functionName() {
    // code to be executed
    return value;
}

This type of function does not take any arguments but returns a value. It is used when the function needs to perform some computation without any external input.

Here is an example:

#include <iostream>
using namespace std;

int getRandomNumber() {
    return rand() % 100;
}

int main() {
    int number = getRandomNumber();
    cout << "The random number is " << number << endl;
    return 0;
}

Output:

The random number is [random number between 0 and 99]

Built in Functions in C++

C++ provides several built-in functions that are part of the standard library. These functions perform common tasks and can be used to simplify code and improve its readability. Some of the most commonly used built-in functions are:

1) cout and cin: These are used for input and output of data.

#include <iostream>
using namespace std;

int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;
    cout << "The number you entered is " << num << endl;
    return 0;
}

2) sqrt(): This function is used to calculate the square root of a number.

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double num = 25;
    double squareRoot = sqrt(num);
    cout << "The square root of " << num << " is " << squareRoot << endl;
    return 0;
}

3) rand(): This function is used to generate random numbers.

#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
    srand(time(0)); // seed the random number generator
    int num = rand() % 100; // generate a random number between 0 and 99
    cout << "The random number is " << num << endl;
    return 0;
}

4) strlen(): This function is used to get the length of a string.

#include <iostream>
#include <cstring>
using namespace std;

int main() {
    char str[] = "Hello, world!";
    int length = strlen(str);
    cout << "The length of the string is " << length << endl;
    return 0;
}

5) tolower() and toupper(): These functions are used to convert characters to lowercase or uppercase.

#include <iostream>
#include <cctype>
using namespace std;

int main() {
    char letter = 'A';
    char lowerCase = tolower(letter);
    char upperCase = toupper(letter);
    cout << "The lowercase version of " << letter << " is " << lowerCase << endl;
    cout << "The uppercase version of " << letter << " is " << upperCase << endl;
    return 0;
}

These are just a few examples of the many built-in functions available in C++. By using these functions, you can save time and improve the efficiency of your code.