Please review my code.

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

//This will create a function that prints 'You just called a function.'
void function()
{
    std::cout << "You just called a function.\n";
}

//This will use a function allong side 'std::cout' to print text on the screen.
int main()
{
    function();
    std::cout << "Really? :O";
    << Add(10+13);
}

// This will add two specified numbers.
int Add (int x, int y)
{
    return (x+y);
}


Please could someone explain why this is not working and how to fix it?
Thanks.
1
2
3
4
5

Shouldn't line 14 be something like:

    std::cout << Add( 10 + 13 ) << endl;
 


What seems to be the problem?
1) Add should be declared before it is used (used on line 14 and declared on 18). You can either move the whole function definition before the main or keep it in its place and add a prototype before main (int Add(int x, int y);).

2) On line 14, you probably forgot something before the << operator. Perhaps you wanted to print the output? It should be something like std::cout << Add(10, 13);.

Edit: ninja'd and corrected ;)
Last edited on
Thanks, it works now. :)
<< is a binary operator. What is on the left side of << on line 14? ';' ?
Add is a function that takes two arguments, while you only pass one. write Add(10, 13) instead.
The compiler reads your code from top to bottom. On line 14 it has no knowledge of a function you define on line 18. You should declare the function Add before you call it.
The add function he made takes two parameters and then adds them together. Do you [benewen] understand how function parameters work?

EDIT: Lets all not post at once =P
Last edited on
Topic archived. No new replies allowed.