in , ,

C++ Functions

C++ Functions
C++ Functions

C++ Functions: In this tutorial, we will find out about the C++ function and function expressions with the help of examples.

A function is a square of code that plays out a particular errand.

Assume we have to make a program to make a circle and shading it. We can make two capacities to tackle this issue:

• a function to draw the circle

• a function to color the circle

Partitioning a perplexing issue into littler pieces makes our program straightforward and reusable.

There are two sorts of function:

  1. Standard Library Functions: Predefined in C++
  2. User-characterized Function: Created by users

In this tutorial, we will concentrate generally on user-defined functions.


C++ User-defined Function

C++ allows the programmer to characterize their own function.

A user-defined function bunches code to play out a particular undertaking and that gathering of code is given a name (identifier).

At the point when the capacity is summoned from any piece of the program, everything executes the codes characterized in the body of the function.


C++ Function Declaration

The syntax to declare a function is:

returnType functionName (parameter1, parameter2,...) {
    // function body   
}

Here’s an example of a function declaration.

// function declaration
void greet() {
    cout << "Hello World";
}

Here,

the name of the function is greet()
the return type of the function is void
the empty parentheses mean it doesn’t have any parameters
the function body is written inside {}
Note: We will learn about returnType and parameters later in this tutorial.


Calling a Function

In the above program, we have declared a function named greet(). To use the greet() function, we need to call it.

Here’s how we can call the above greet() function.

int main() {
     
    // calling a function   
    greet(); 

}

Example 1: Display a Text

#include <iostream>
using namespace std;

// declaring a function
void greet() {
    cout << "Hello there!";
}

int main() {

    // calling the function
    greet();

    return 0;
}

Output

Hello there!

Function Parameters

As referenced above, function can be pronounced with boundaries (contentions). A parameter is a value that is passed while proclaiming a capacity.

For instance, let us consider the capacity underneath:

void printNum(int num) {
    cout << num;
}

Here, the int variable num is the function parameter.

We pass a value to the function parameter while calling the function.

int main() {
    int n = 7;
    
    // calling the function
    // n is passed to the function as argument
    printNum(n);
    
    return 0;
}

Example 2: Function with Parameters

// program to print a text

#include <iostream>
using namespace std;

// display a number
void displayNum(int n1, float n2) {
    cout << "The int number is " << n1;
    cout << "The double number is " << n2;
}

int main() {
     
     int num1 = 5;
     double num2 = 5.5;

    // calling the function
    displayNum(num1, num2);

    return 0;
}

Output

The int number is 5
The double number is 5.5

In the above program, we have used a function that has one int parameter and one double parameter.

We then pass num1 and num2 as arguments. These values are stored by the function parameters n1 and n2 respectively.

Note: The type of the contentions passed while calling the function must match with the comparing boundaries characterized in the function declaration.


Return Statement

In the above programs, we have used void in the function declaration. For example,

void displayNumber() {
    // code
}

This means the function is not returning any value.

It’s also possible to return a value from a function. For this, we need to specify the returnType of the function during function declaration.

Then, the return statement can be used to return a value from a function.

For example,

int add (int a, int b) {
   return (a + b);
}

Here, we have the data type int instead of void. This means that the function returns an int value.

The code return (a + b); returns the sum of the two parameters as the function value.

The return statement denotes that the function has ended. Any code after return inside the function is not executed.


Example 3: Add Two Numbers

// program to add two numbers using a function

#include <iostream>

using namespace std;

// declaring a function
int add(int a, int b) {
    return (a + b);
}

int main() {

    int sum;
    
    // calling the function and storing
    // the returned value in sum
    sum = add(100, 78);

    cout << "100 + 78 = " << sum << endl;

    return 0;
}

Output

100 + 78 = 178

In the above program, the include() function is used to discover the total of two numbers.

We pass two int literals 100 and 78 while calling the function.

We store the returned value of the function in the variable sum, and afterward, we print it.

Notice that sum is a variable of int type. This is because the return value of add() is of int type.


Function Prototype

In C++, the code of capacity assertion ought to be before the function call. In any case, on the off chance that we need to characterize a capacity after the capacity call, we have to use the function prototype. For instance,

// function prototype
void add(int, int);

int main() {
    // calling the function before declaration.
    add(5, 3);
    return 0;
}

// function definition
void add(int a, int b) {
    cout << (a + b);
}

In the above code, the function prototype is:

void add(int, int);

This provides the compiler with data about the capacity name and its boundaries. That is the reason we can utilize the code to call a capacity before the capacity has been characterized.

The syntax of a function prototype is:

returnType functionName(dataType1, dataType2, ...);

Example 4: C++ Function Prototype

// using function definition after main() function
// function prototype is declared before main()

#include <iostream>

using namespace std;

// function prototype
int add(int, int);

int main() {
    int sum;

    // calling the function and storing
    // the returned value in sum
    sum = add(100, 78);

    cout << "100 + 78 = " << sum << endl;

    return 0;
}

// function definition
int add(int a, int b) {
    return (a + b);
}

Output

100 + 78 = 178

The above program is nearly identical to Example 3. The only difference is that here, the function is defined after the function call.

That’s why we have used a function prototype in this example.


Advantages of Using User-Defined Functions

Functions make the code reusable. We can pronounce them once and use them on multiple times.

Functions make the program simpler as every little task is divided into a function.

Functions increase readability.


C++ Library Functions

Library functions are the implicit capacities in C++ programming.

Programmers can use library functions by conjuring the capacities straightforwardly; they don’t have to compose the functions themselves.

Some regular library works in C++ are sqrt(), abs(), isdigit(), and so on.

So as to use library functions, we ordinarily need to incorporate the header record in which these library capacities are characterized.

For example, so as to use mathematical functions, for example, sqrt() and abs(), we have to incorporate the header record cmath.


Example 5: C++ Program to Find the Square Root of a Number

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

int main() {
    double number, squareRoot;
    
    number = 25.0;

    // sqrt() is a library function to calculate the square root
    squareRoot = sqrt(number);

    cout << "Square root of " << number << " = " << squareRoot;

    return 0;
}

Output

Square root of 25 = 5

In this program, the sqrt() library function is used to calculate the square root of a number.

The function declaration of sqrt() is defined in the cmath header file. That’s why we need to use the code #include to use the sqrt() function.


Please feel free to give your comment if you face any difficulty here.

salman khan

Written by worldofitech

Leave a Reply

How to Use Your Android Phone as a Mouse on Mac

How to Use Your Android Phone as a Mouse on Mac

How to Create the Perfect GIF in Photoshop

How to Create the Perfect GIF in Photoshop