Parameters and Arguments - Sending Data to C++ Functions

In this tutorial, we will learn how to send data to functions in C++ programming language.

Sending data to functions

In the last tutorial of our C++ course, we learned how functions return information to those who invoked it through the return command.

Functions can receive information through the use of parameters, which is a special variable, declared in the function header, which will be responsible for storing the values ​​that will be passed to these functions. These values ​​are the arguments.

To use the arguments (ie, pass some value for functions to work), we must enter the parameter list within the parentheses of the function declaration.

For example, a function that returns an integer, receives an integer and is called func, states:

  • int func (int var) {...}


To invoke by passing the number 2112, for example, we do:

  • func (2112);


var is the parameter, 2112 is an argument.

Now a function that returns nothing is called func2 and receives an integer, a float, and a boolean:

  • void func2 (int var, float num, bool status) {...}


If we want to pass integer 1, float 21.12 and boolean true, into this function, we do:

  • func2 (1, 21.12, true);


Parameter list: var, num and status
Arguments (values): 1, 21.12 and true

See the order: an integer, a float and a boolean! Respect the order of the function header parameter list!

C++ Parameters and Arguments Example

Let's create a function that takes any number, and returns it squared:
#include <iostream>
using namespace std;

float square(float num)
{
    return num*num;
}

int main()
{
    float var;

    cout<<"Number to square: ";
    cin >> var;

    cout<<var<<"*"<<var<<" = " << sqaure(var)<<endl;
}
Note that we pass the var variable, which is a float, to the square() function, which takes a float as its argument.

Although we passed the var variable, what actually happens behind the scenes is that we pass a number, a value, that was entered by the user.

If we pass var = 5, what goes to the function is 5.
It is as if we had done: square(5);

Within the function, this value is stored in the num parameter, ok?
The function does not 'see' the var variable, nor does it know of its existence, it just takes its value and copies it to the float num variable.

C++ Parameters and Arguments

Let's now create a function that takes a number, raises it to the cube, and returns that value.
#include <iostream>
using namespace std;

float cube(float num)
{
    num = num*num*num;

    return num;
}

int main()
{
    float num;

    cout<<"Number to cube: ";
    cin >> num;

    cout<<"Number typed: "<<num<<endl;
    cout<<"Cube: "<< cube(num)<<endl;
    cout<<"num value: "<<num<<endl;
}
We tested this example.
The parameter is num within the function.

When calling the function, also pass a name variable num.
Within the function, we change the value of num.
It used to be num, then it becomes (num * num * num), and we return this new num value.

In main(), we will display the number that the user types (num), the cube (by invoking the cube() function), and then display the value of num again.

For num = 4, the result is:
C++ free tutorial


Note that the value of num has been changed ONLY inside the function!

When we pass an argument to a function, the function makes a copy of it and assigns it to the specific parameter. Inside the function, you changed the parameter value, but the variable original is not changed!

Ah ... now you can understand a little more about main ():
int main () {
...
    return 0;
}


That is, it is a function that takes no argument (since it has no parameter) and returns an integer, in which case it returns 0.

Two things

  1. Universally, when a function returns 0, it's all right, all ok. When your code works ok, it should return 0
  2. In this specific case, main() has no parameters, but yes, there are versions of it getting arguments, we will see later in our course

No comments:

Post a Comment