# Lesson 4 of Tutorial

• 09-09-2004
polonyman
Lesson 4 of Tutorial
Hey guys. Im pretty new to the site and just tryin to work through the tutorials. I have found them difficult but haven't had a big problem understanding them until lesson 4. Ill just copy the bit I dont get.

<<int a;
a=random(5);>>

Do not think that 'a' will change at random, it will be set to the value returned when the function
is called, but it will not change again.

I dont c how a=random(5) works so could anyone explain it better plz? :confused: also the (5) has me confused
thankz cya
• 09-09-2004
bithub
The random function returns a random number. It is used when you want to set a variable to something random. Once the variable is set by the random function it will not change on its own.
• 09-09-2004
polonyman
also im havin sum throuble with another bit of lesson 4

<< #include <iostream.h>
int mult(int x, int y);
int main()
{
int x, y;
cout<<"Please input two numbers to be multiplied: ";
cin>>x>>y;
cout<<"The product of your two numbers is "<<mult(x, y);
return 0;
}
int mult(int x, int y)
{
return x*y;
} >>

basically i dont c y int mult(int x, int y); is at the top and bottom only the bottom 1 doesnt have semicolons. be nice if u could explain it, cya
• 09-09-2004
polonyman
more stupid probs
hey, i edited the program, have a look

<<#include <iostream.h>
int mult(int x, int y);
return x*y;
int main()
{
int x, y;
cout<<"Please input two numbers to be multiplied: ";
cin>>x>>y;
cout<<"The product of your two numbers is "<<mult(x, y);
return 0;
}
>>

it works exactly the same. and it still wont work with cin.get(); to stop it disapearing. where do i put it or what else do i use?
• 09-09-2004
hk_mp5kpdw
Quote:

Originally Posted by polonyman
basically i dont c y int mult(int x, int y); is at the top and bottom only the bottom 1 doesnt have semicolons. be nice if u could explain it, cya

When the compiler gets to a piece of code that calls a function it likes to know a little about the function being called so it can do proper type checking of the arguments at that moment. In order to do this it either needs to have already read through the whole definition of the function before it reaches that particular part of the code, or it at least needs to know how the function gets called and what type of arguments it takes and what type of value it may return. The second method is done by the use of a function prototype which is demonstrated in that section of code you had. It typically looks just like a function definition except it is missing the body of the function and has the semicolon at the end. This is just letting the compiler know that "hey I'm going to be calling a function called mult later on and it takes two int arguments and it returns an int value as its result.

So, you can either fully define the function prior to its being called like this:
Code:

```#include <iostream> // Function fully defined prior to its being called below, no prototype needed int mult(int x, int y) {     return x*y; } int main() {     int x, y;     std::cout<<"Please input two numbers to be multiplied: ";     std::cin>>x>>y;     std::cout<<"The product of your two numbers is "<< mult(x, y);     std::cin.get();     std::cin.get();     return 0; }```
Or you would need the function prototype:
Code:

``` #include <iostream> // Function prototype needed otherwise compiler won't know about function // when it gets to the calling of it in the code below int mult(int x, int y); int main() {     int x, y;     std::cout<<"Please input two numbers to be multiplied: ";     std::cin>>x>>y;     std::cout<<"The product of your two numbers is "<<mult(x, y);     std::cin.get();     std::cin.get();     return 0; } int mult(int x, int y) {     return x*y; }```
Quote:

Originally Posted by polonyman
it still wont work with cin.get(); to stop it disapearing. where do i put it or what else do i use?

See either of the above examples.
• 09-09-2004
Salem
Before you invent another creative way of highlighting code, read this