# Thread: Need help understanding Functions (Lesson 4)

1. ## Need help understanding Functions (Lesson 4)

So i need some help understanding the concept about Functions. I've read the whole lesson but there're some things that im not quite understanding.

 1. Which is not a proper prototype? A. int funct(char x, char y); B. double funct(char x) C. void funct(); D. char x(); I took the quiz and i got this problem wrong, I thought the answer would be C, i didn't think void would be a proper prototype, well I haven't even learned about void so i dont even know what it is. I'm barely on the lesson about functions. Since 'int' and 'double' are both variable types and they have their function next to them i thought why would that be wrong.....wait im barely thinking of this right now, is double not a proper prototype because it doesn't have a syntax?! I think that's why cause apparently all other prototypes have syntax's. And another question i got wrong is: 3. Which of the following is a valid function call (assuming the function exists)? A. funct; B. funct x, y; C. funct(); D. int funct();

I guess i got this one wrong because all I've seen is
int mult ( int x, int y );so I thought D would be right, I'm not sure if i have seen any other function like D, most likely it's because i didn't know they were function, can anyone tell me functions that have been shown to me from the lessons before functions??? Also why is D not a valid function call???

Im very specific with things, If someone tells me to do something I would need details on how to do things cause i would be scared im not doing them right. So I need to be explained things in details.

Also im very new to programming, i want to be able to master C++ and a lot of other languages as well Please help me make this possible.

2. For the first question, the answer is B because it is missing the semicolon at the end, thus it is not a proper function prototype.

For the second question, D is not a valid function call because the "int". D is actually an example of a function prototype (a declaration of a function).

Also im very new to programming, i want to be able to master C++ and a lot of other languages as well Please help me make this possible.
Keep practicing. The best way to learn how to program is to spend a lot of time doing it. Any time you have questions, forums like this one are a good place to get answers.

3. Thanks so much One more thing, i want to test myself for i can know what are functions when i see them. In this whole code im going to name what are the functions in comments, if you can correct me when it is needed that would be great.

Code:
```#include <iostream>

using namespace std;

int mult(int x, int y)        // 'mult' a function
{
return x * y;     // 'return' is a function
}
int main()          // 'main' is a function
{
int x;
int y;

cout << "Please input two numbers to be multiplied: ";  // 'cout' is a function??
cin >> x >> y;        // cin is a function??
cin.ignore();       // cin.ignore() is a function
cout << "The product of your two numbers is " << mult(x, y) << "\n";
cin.get();        // cin.get() is a function
}```
Thanks for the help!

4. There are only 2 functions defined in that code block: main, and mult.

cin and cout are both objects of type ostream. The get() and ignore() calls are invocations of member functions. If you have not yet learned about classes, then ignore this for now.

5. My god thank you so much! I have been stuck on trying to figure this stuff out for house :/

And i will ignore that until i get to classes, but finally i can move on thank you and if i need more help i help be back to the forums.

6. OK, here's the thing. A function prototype looks like:

ReturnType FunctionName(ArgumentType ArgumentName, ...);

int, double, void and char are all valid types, so they could go into ReturnType, so based on that, all answers are right.
Now we have FunctionName, what the function is supposed to be called. In the question, all the answers have a function name, so they're still all right.
Then comes a number of arguments. Each argument needs a type and a name. A function can also not take any arguments in which it will be an empty paranthesis list (). So "char x" is a valid argument. "()" is an empty argument list (valid). "char x, char y" is a valid argument list because it takes two arguments x and y with appropriate types. Based on this, all answer are correct.
Each function prototype must end with a semicolon. Answer B does not end with a semicolon, therefore it is not a function prototype.

1. Which is not a proper prototype?
A. int funct(char x, char y);
B. double funct(char x)
C. void funct();
D. char x();

Continuing to question 2...

3. Which of the following is a valid function call (assuming the function exists)?
A. funct;
B. funct x, y;
C. funct();
D. int funct();

A function call looks like:

FunctionName(ArgumentName, ...);

So basically you type your function prototype, strip the return type and the type of each argument and replace the names with the names of actual variables you want to pass. If you pass no parameters, the list should be empty ().
So A is not valid because of missing paranthesis list.
B is not valid because of missing paranthesis list.
C is valid.
D is not valid because of return type.

7. There's generally three things you will see when it comes to functions:

1) Declaration

This isn't the actual code for the function, it is a prototype (blueprint) of what the function looks like. It lets the compiler check that you are using the function correctly, without the need to look at the definition (the actual code). Declarations end in a semicolon ';', and there is no body afterwards
Code:
`{ /** Lines to execute in the body */ }`
By looking at a declaration, you can tell the return type, and also the variable types the function takes. Names for variables are not necessary in a declaration, but are generally a good idea. It's also a good idea to have a descriptive name, that tells a little about what the function will be doing:

Code:
```int Subtract( int left, int right ); // A function that probably returns ( left - right )

// Could also be:
int Subtract( int, int );```

2) Definition

This is the actual code that executes when you call the function. You write the code just like you would in the main() function. A function definition has it's own scope, which means that you may declare variables within the function body, and those variables are only available within the fuction (unless you return in some way). Also, the variables passed into the function will be separate copies from the variables elsewhere, unless specified otherwise (If this is the case you will see symbols like '*', or '&', which you will learn more about later):

Code:
```int Subtract( int left, int right )
{ // Open the function body

return left - right; // right is subtracted from left, and the result is returned to the caller

} // Close the function body```
3) Calling

Calling is when you use the function in other code. Functions are usually written to do specific things. Writing the function lets you neatly wrap a specific behavior that you can then execute on command. This is useful when you will need to call upon this behavior several times while writing your code, but don't want write it out every time. So instead you write the function declaration, and definition, and then call the function like this:

Code:
```int main()
{
int A= 10;
int B = 4;

// We call Subtract(), which returns A - B
int Result = Subtract( A, B );

std::cout << Result << "\n";
}```