the ( ) and no integer

I did this, following the tutorial, but I want to 100% understand why

int getNumber() doesnt need any integer inside the ( ).

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

int doubleNumber(int a)
{
    return a * 2;
}

int getNumber()
{
    int a{0};
    std::cin >> a;

    return a;
}

void print(int c)
{
    std::cout << "Your number is: " << c;
}

int main()
{
    int number{doubleNumber(getNumber())};
    print(number);

    return 0;
}
I want to 100% understand why int getNumber() doesnt need any integer inside the ( ).

You have created a function int getNumber(),
* whose name is "getNumber"
* it returns an integer value
* It does not take any parameters

Why should call of function, that does take 0 parameters, have any parameters?
I see, cuz there nothing to add so it should be normal (). Thanks

an question

main() and int main(). What's the different? Do I need to use int main if I'm to use any of int doubleNumber and void of such?

Also

I tried different way, without extra int for cin >>

Is this working properly? It does compiler fine tho.

I tried to leave line 25 without anything in the (). It turn out too few to argument error. So I added a into it. Were it correct?

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

int doubleNumber(int a)
{
    return a * 2;
}

/*int getNumber()
{
    int a{0};
    std::cin >> a;

    return a;
}
*/
void print(int c)
{
    std::cout << "Your number is: " << c;
}

int main()
{
    int a{0};
    std::cin >> a;
    int number{doubleNumber(a)};
    print(number);

    return 0;
}
Last edited on
The standard says main shall return int. And all functions in C++ need to have an explicit return type (could be void). That's just how it is. A "return 0;" is implicitly added if it doesn't already exist in this particular case in main, though.

Your code looks fine to me. You are passing an int to a function that expects an int (doubleNumber), and then using the return value of that function (also an int) to initialize your number variable in main.
Last edited on
Thanks for explaining.

What you mean is because I want int so it allow me to use int main(). So if i desire to use some char, I will have to use char main(). Wont that mean its better if I leave it normally as main().

Unless I'm wrong?
char main() is not legal C++. int main ... is the one and only way to get a start

A program shall contain a global function named main, which is the designated start of the program. It shall have one of the following forms:
int main () { body } (1)
int main (int argc, char *argv[]) { body } (2)

https://en.cppreference.com/w/cpp/language/main_function


What you mean is because I want int so it allow me to use int main(). So if i desire to use some char, I will have to use char main(). Wont that mean its better if I leave it normally as main().
As per the standard, the return type of main() must be int, so using any other type is invalid.
Unlike in C, in C++ all functions must have their return types explicitly specified, so this:
 
main(){}
is an invalid program. This isn't:
 
int main(){}
Thank! that what I was looking for.
1
2
3
4
5
6
7
8
9
int main()
{
    int a{0};
    std::cin >> a;
    int number{doubleNumber(a)};
    print(number);

    return 0;
}


Note that you don't need the variable number. You can do this:

1
2
3
4
5
6
7
int main()
{
    int a{0};
    std::cin >> a;

    print(doubleNumber(a));
}

oh shorter, thanks didnt think of that.

on other side, could somebody give more example on this? I can't just move to next chapter without 100% understanding of this.

one more Question I'm curious.

void print(int x , int y) // it is void because it doesnt return anything. what if I put int print(int x, int y).
what would change?

I compiler it, nothing changed?

std::cout << x + y;

int print(int x , int y) // it is int because it carry x and y over.

return x, y;

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.
Last edited on
The int num {getValueFromUser()}; is a declaration of a variable, whose name is "num" and type is int.

The declaration contains initializer; the num is created with the value of expression getValueFromUser().

What is the value of getValueFromUser()?
That is a function, so its value is whatever that function returns, once its execution completes.
We know from declaration of the function, int getValueFromUser(), that the returned value is one int.

Okay, the function is called. It does:
1
2
3
4
 	std::cout << "Enter an integer: ";
	int input{};
	std::cin >> input;
	return input;

In other words, it reads one integer value from the user and returns that integer.
The value returned from the function is copied into variable num.


As far as the main() is concerned, there are four steps:
1. The function getValueFromUser() is called
2. The num is created with some value that we got in step 1
3. Function printDouble(int) is called with num
4. Value 0 is returned to the operating system that did start this program
Thank you! Appreciate your time of writing this down.
int print(int x , int y) // it is int because it carry x and y over.

To be precise:

This declaration is doing several things:

1) int print(int x , int y)

-the function is called print

2) int print(int x , int y)

- the function takes two arguments, x and y. Both of them are integers. These two values are copied into the function from the code that calls it. Those values are used inside the function.

3) int print(int x , int y)

The funtion returns an integer value to the code that calls it.

Note that the type that the function returns, doesn't have to be connected to the type(s) of the argument(s) that get passed in.


Also, you said:

return x, y;

This almost certainly does not do what you think it does. It returns a single integer, that is the same as the value of y:

https://en.cppreference.com/w/cpp/language/operator_other


Last edited on
Topic archived. No new replies allowed.