confusing with argument

Oct 17, 2020 at 6:01pm
I get the idea of how this work but I don't really understand how did it bring the information by using printDouble(num);

int num {getvaluefromuser()} would go to first int getvaluefromuser. Then back to printdouble(num);

printdouble(num); go to void printdouble and it'll get value with * 2.

so my question did the (num): bring the value from void printDouble(int value) because the (num) would refer to (int value) in void part?

Im confuse where exact thing go from there to there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #include <iostream>

int getValueFromUser()
{
 	std::cout << "Enter an integer: ";
	int input{};
	std::cin >> input;

	return input;
}

void printDouble(int value) // This function now has an integer parameter
{
	std::cout << value << " doubled is: " << value * 2 << '\n';
}

int main()
{
	int num { getValueFromUser() };

	printDouble(num);

	return 0;
}


I need to understand of where step to step it goes.

example

step 1... int num {getValueFromUser()};
step 2... cout << "etc" then get input/
step 3... return input to int main()
step 4... printDouble(num); .. that where I'm lost.

I suspect

step 4...(int value) would refer to int num then
step 5...printDouble(num)
step 6...then it sent to int getValueFromUser().
Last edited on Oct 17, 2020 at 6:11pm
Oct 17, 2020 at 6:26pm
Ignoring global variables, values are transferred between functions either via their argument list (can be one-way or two-way) or via a return value of the function (one-way: from function to calling procedure).

int num{.}
declares a variable of type int called num and initialises it with whatever is within the curly brackets {}

In this instance, what is within the curly brackets is a function call; so, num is initialised to the return value of the function (whatever is sent back by return input;)

This value of num is then submitted, via the argument list, to the function printDouble(.). It is passed by value (i.e. a one-way transfer) and is collected by that function in the local variable value.

Function printDouble prints out both value and 2 * value. It doesn't return anything because it is a void function.
Oct 17, 2020 at 9:47pm
Can I get more explaining, maybe with the step to step in order?
Oct 19, 2020 at 11:34am
I'd recommend taking a look at a tutorial about functions, for example:

http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.