Function Part 1

Estimated reading: 4 minutes 132 Views

A function in C++ is a block of code that performs a specific task. Functions can be used to encapsulate common operations, making your code more reusable and easier to understand. Functions can also be used to improve the performance of your code by avoiding duplicate code.

To create a function in C++, you use the following syntax:

return_type function_name(parameter_list) {
  // Function body
}

The return_type specifies the type of data that the function will return. The function_name is the name of the function. The parameter_list is a list of parameters that the function will accept. The function_body is the code that the function will execute.

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

int sum(int x, int y)
{
    int s = 0;
    s = x + y;
    return s;
}

This function takes two integer parameters, x and y, and returns the sum of those two parameters.

To call a function, you simply use its name followed by a pair of parentheses. For example, to call the sum() function, you would write:

int s = sum(10, 20);

This would assign the value 30 to the variable s.

The code could be written this way:

#include <iostream>

using namespace std;

int sum(int x, int y)
{
    int s = 0;
    s = x + y;
    return s;
}
int main()
{
    int s = 0;
    s = sum(10, 20);
    cout << "sum = " << s << endl;
    
    return 0;
}

The following example takes two float parameters and return the sum of them:

#include <iostream>

using namespace std;

float sum(float x, float y)
{
    float s = 0;
    s = x + y;
    return s;
}
int main()
{
    float s = 0;
    s = sum(20.5, 30.6);
    cout << "sum = " << s << endl;
    
    return 0;
}

Output:

sum = 51.1

The importance of using return:

We need to write a return in a function in C++ to specify the value that the function will return to the calling function. This is important because it allows us to use the function to perform calculations and then use the result of those calculations in our code.

Without the return statement, the function would not be able to return any value to the calling function. This would mean that we would not be able to use the function to perform calculations and then use the result of those calculations in our code.

By using the return statement, we can terminate the execution of the function early and avoid the error. This makes our code more robust and reliable.

Overall, the return statement is an important part of C++ programming. It allows us to return values from functions and to terminate the execution of functions early. This makes our code more reusable, efficient, and reliable.

But can we write a function without using return?

See this example:

#include <iostream>

using namespace std;

void print()
{
    cout << "myName" << endl;
}

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

This code defines a function called print() and a main function. The print() function simply prints the string “myName” to the console. The main function calls the print() function and then returns 0.

The print() function is declared with the void return type. This means that the function does not return any value. We use the void return type when we want a function to perform an action but not return any value.

When you run this code, the following output will be printed to the console:

myName

We use the void return type for the print() function because we only want it to print the string “myName” to the console. We do not need the function to return any value.

 

Functions can also be nested, meaning that one function can call another function. This can be useful for breaking down complex tasks into smaller, more manageable functions.

Functions are a powerful tool in C++ and can be used to improve the quality and performance of your code.

Here are some tips for using functions in C++:

  • Give your functions meaningful names. This will make your code more readable and easier to maintain.
  • Use functions to encapsulate common operations. This will make your code more reusable and easier to understand.
  • Use functions to improve the performance of your code by avoiding duplicate code.
  • Use functions to break down complex tasks into smaller, more manageable functions.
Share this

Function Part 1

Or copy link

CONTENTS
English