confirming

Hi again, I want to make sure I get it right.

For this line 3 the one with nothing in the parameter. Is not necessary because we already used return input;.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream>

int get_input_from_user()
{
    int input{0};
    std::cin >> input;

    return input;
}

int main()
{
    int print_input{get_input_from_user()};
    std::cout << "your input is: " << print_input;

    return 0;
}


for this one, line 3, it is nesscary because we want to copy this to line 10's (x, y). Is this what different between them? Is there other way to use those integer inside the parameter.?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int add(int x, y)
{
    return x + y;
}

int main()
{
    int print_input{add(x, y)};

    return 0;
}
You need to make the 2nd one at least compile...
1
2
3
4
5
6
7
8
9
3:16: error: 'y' has not been declared
 In function 'int add(int, int)':
5:16: error: 'y' was not declared in this scope
 In function 'int main()':
10:25: error: 'x' was not declared in this scope
10:28: error: 'y' was not declared in this scope
10:9: warning: unused variable 'print_input' [-Wunused-variable]
 In function 'int add(int, int)':
6:1: warning: control reaches end of non-void function [-Wreturn-type]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int get_input_from_user()
{
	int input {0};
	std::cin >> input;

	return input;
}

int add(int x, int y)
{
	return x + y;
}

int main()
{
	int print_input {add(get_input_from_user(), get_input_from_user())};
	std::cout << "The sum is: " << print_input << '\n';

}


Now try this!

1
2
3
4
int main()
{
	std::cout << "The sum is: " << add(get_input_from_user(), get_input_from_user()) << '\n';
}

Is not necessary because we already used return input
There is no direct connection between parameter and the return value. E.g.:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>

int get_input_from_user(const char *prompt) // Note: parameter
{
    std::cout << prompt; // Note
    int input{0};
    std::cin >> input;

    return input;
}

int main()
{
    int print_input{get_input_from_user("input: ")};  // Note: Additional promt
    std::cout << "your input is: " << print_input;

    return 0;
}
For this line 3 the one with nothing in the parameter. Is not necessary because we already used return input

As coder777 says, there is no relationship at all between those things. You can have:

- functions with a return value and arguments
- functions with a return value and no arguments
- functions with arguments and no return value
- functions with no arguments and no return value

What does "return input" even mean? Return values aren't imput to the function, they're output from the function. Can you explain what you mean by that phrase?

It seems to me that you haven't really understood what arguments are for, and what return values are for. You probably need to spend more time with your textbook, reading and understanding what it says about functions.

Or maybe with a better textbook, if the one you're using doesn't explain this properly.
i dont know why i totally skipped it but yeah i fixed it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int add(int a, int b)
{
    return a + b;
}

int main()
{
    int a = 5;
    int b = 4;
    std::cout << add(a, b);

    return 0;
}
Then so I follow some idea from seeplus and made this

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
#include <iostream>

int get_input_from_user()
{
    int input{};
    std::cin >> input;

    return input;
}

int add(int a, int b)
{
    return a + b;
}

void print_total(int total)
{
    std::cout << "Your total is " << total;
}

int main()
{
    int a {get_input_from_user()};
    int b {get_input_from_user()};
    int sum{add(a,b)};
    print_total(sum);
    return 0;
}


as for int ab what is good name for it? Idk what to name it so I made up ab.. maybe result?

one of tutorial said I should make many function as I can to avoid many lines in one function.
Last edited on
as for int ab what is good name for it?
What about sum?
Thanks!
Topic archived. No new replies allowed.