The RETURN Command in C++: Getting Information from a Function

Now that you have learned what functions are and what they are for, let's see how to communicate with functions by receiving data and information from them using the return command.

Exchanging Data with C++ Functions

In the previous tutorial, from our C++ Functions section, we looked at how to create and invoke a function.
Just call by the name: hello(), it came quickly and executed immediately.

We could call 1 million times, that it would execute 1 million times without having to repeat code, just call the function whenever we need to do what it intends to do.

But there is a problem ... when we call a function, the execution leaves the place where the call was made and goes to another memory location, where the function instructions are, executes everything, and comes back. In this previous case, there is no communication between the caller and the function.

Let's look at a C ++ code where we invoke a function that asks for the number and displays its double:
#include <iostream>
using namespace std;

void doub()
{
    float num, doubl;

    cout<<"Type a number: ";
    cin >> num;

    dobl = 2*num;

    cout<<"Double: "<<dobl<<endl;

}

int main()
{
    doub();
    return 0;
}
Cool huh? However, we do not have access to this information 'doubl', we can not get it to use in another calculation, for example, it is simply displayed within the function and at the end of it, this value is lost. Function internal variables are created locally, and then deleted.

If you try to use 'doubl' outside the function you will see that it gives a compilation error.

We need to find another way to make the function communicate with the outside, to send information out, do you agree that this is important?

The RETURN Command in Functions

Whenever we want to send information from within a function to where it was called, we use the return command.

First, in the function declaration header, we need to enter the data type the function will return. In the previous example is void because it returns nothing.

If the data type it will return is a float, for example, its header should be:
float doub() {...}

And within the scope of the function we should do: return info;
Where info must be a float.

Ready, the info 'info' will be returned, returned to the place where the function was invoked.

Let's create a function that asks for a number, doubles it, and returns its double:
#include <iostream>
using namespace std;

float doub()
{
    float num, doubl;

    cout<<"Type a number: ";
    cin >> num;

    doubl = 2*num;

    return doubl;
}

int main()
{
    float res;
    res = doub();

    cout<<"Double: "<<res<<endl;
    return 0;
}
Now when we call the function: doub()
A value is returned to the caller, back a float, in this case.

Then we store the result of this return in the res variable (obviously it must be float):
res = doub();

And that's it, this variable will be twice the value that the user enters inside the function.
We can even make the code leaner, see:
#include <iostream>
using namespace std;

float doub()
{
    float num;

    cout<<"Type a number: ";
    cin >> num;

    return (2*num);
}

int main()
{
    cout<<doub()<<endl;
    return 0;
}

Note that we can return an expression directly: return 2*num;

Example of Using RETURN in Functions

Let's now create an odd or even function.
If it is even, it should return 0, if it is odd it should return 1.

Here's how our C ++ code looks:
#include <iostream>
using namespace std;

int odd_even()
{
    int num;

    cout<<"Type a number: ";
    cin >> num;

    if(num%2==0)
        return 0;
    else
        return 1;
}

int main()
{
    if(odd_even()==0)
        cout<<"It's even!\n";
    else
        cout<<"It's odd!\n";
    return 0;
}
Note now that there are two return statements inside our odd_even() function, that's possible, but see that only one or the other can happen, never an IF and an ELSE is executed both, it's always one of them.

Let's clean this code?
#include <iostream>
using namespace std;

int odd_even()
{
    int num;

    cout<<"Type a number: ";
    cin >> num;

    return (num%2);
}

int main()
{
    if(!odd_even())
        cout<<"It's even!\n";
    else
        cout<<"It's odd!\n";
    return 0;
}
Got a good understanding?

Good programming practices

First, function names: There is no right or wrong way to use them, but we recommend being consistent with their names.

Preferably create named functions in the following style: c_progressive(), project_progressive(), van_der_graaf_generator() ... - all lowercase, separated by _

Or: cProgressive(), projectProgressive(), vanDerGraafGenerator() ...- first lowercase letter, and uppercase for beginning of next words, ok?

Second, use a lot of functions, and make them very communicative, whenever possible by returning and receiving (we'll see in the next tutorial) data in a very logical and clear manner.

Just as every part of your car or every organ in your body does its specific task, they also communicate and exchange information with each other, this is essential for the proper functioning of the system.

The functions of a system should do the same! This is good programming practice, don't forget it! Make your functions easily 'coupled' with other functions, it will be essential for you to be able to create large and robust systems such as business software, operating systems, games, etc.

No comments:

Post a Comment