C++ function help please?

Questions about programming languages and debugging
Post Reply
User avatar
Vossler
Newbie
Newbie
Posts: 3
Joined: 18 Apr 2010, 16:00
14

C++ function help please?

Post by Vossler »

http://code.suck-o.com/38646

I have a few questions about this code

1)Why is the changeThem function executed before main? Is it because the function prototype was declared before int main, or is it something else?

2) I am still not sure as to the difference between a parameter and an argument, other than a parameter is contained in () with the function header, and the argument is contained in the () of the function call. Can someone give me a more detailed explanation please?

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

That doesn't seem to be the whole code?

EDIT: nvm what I said above. I'm tired and I didn't see all of the code ^^

Anyway ...

1: If you write a function before main, then you don't need a prototype to tell the system that the function exists.

2: An argument is a value you send to a function when calling it, and a parameter is the variable receiving the value.
"The best place to hide a tree, is in a forest"

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

changeThem() is not executed before main(). It's declared before main() so it can be used inside main(). The line before main() describing just the type signature of changeThem is called a function prototype. The implementation of changeThem is after main().

The entry point of a program is always main(), even if you write functions above it.

The functionality of the program can be explained by the concept of pass-by-value as opposed to pass-by-reference. Check Wikipedia.

User avatar
Vossler
Newbie
Newbie
Posts: 3
Joined: 18 Apr 2010, 16:00
14

Post by Vossler »

2: An argument is a value you send to a function when calling it, and a parameter is the variable receiving the value.
Sorry for being dense, but I am a still not 100%.....

Imagine its a restaurant.

Does the parameter choose the table that the argument will sit at, and the argument is the customer who sits down at the table....?

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

Vossler wrote: Imagine its a restaurant.

Does the parameter choose the table that the argument will sit at, and the argument is the customer who sits down at the table....?

hmm, I would more like to say that the Parameter is the table where the argument sits down ^^ depends on how you see it I guess... but I found something here that will explain it pretty well =)
The words argument and parameter are often used interchangeably in the literature, although the C++ Standard makes a clear distinction between the two. An argument is one of the following: an expression in the comma-separated list in a function call; a sequence of one or more preprocessor tokens in the comma-separated list in a macro call; the operand of a throw-statement or an expression, type, or template-name in the comma-separated list in the angle brackets of a template instantiation. A parameter is one of the following: an object or reference that is declared in a function declaration or definition (or in the catch clause of an exception handler); an identifier between the parentheses immediately following the macro name in a macro definition; or a template-parameter. This example demonstrates the difference between a parameter and an argument:


void func(int n, char * pc); //n and pc are parameters
template <class T> class A {}; //T is a a parameter

int main()
{
char c;
char *p = &c;
func(5, p); //5 and p are arguments
A<long> a; //'long' is an argument
A<char> another_a; //'char' is an argument
return 0;
}
"The best place to hide a tree, is in a forest"

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

http://en.wikipedia.org/wiki/Argument_% ... _arguments

On line 17 whole and real are arguments to the function changeThem.
The function changeThem has two parameters named i and d.

Post Reply