Can variables be created in function prototypes?

I'm reviewing before my next class in C++ this fall. I have a quick question about functions.

Can variables be created in function prototypes, or do they have to be created separately and referred to in prototypes?

THX

Wayne
What do you mean? A function prototype merely declares the type of a function so it can be used without its definition. For example:
1
2
3
4
5
6
7
8
9
10
11
12
//Prototype:
int f(int a,double b);

//...

//Call:
int x=f(1,3.14);

//...

//Definition:
int f(int a,double b){ /*...*/ }
As it is a prototype nothing is actually created.
However, you can certainly declare argument types and names in a prototype.
Sorry for the unclear question.
Here's the code I'm trying to make sure I understand.

 
void compareCode(ifstream& infile, ofstream& outfile, const int list[], int length)


Normally when you create a variable you give the type, then the name, and maybe an initialization value- like this :

int length = 51;

In this program, as an example, the parameter int length is not created as a variable anywhere else in the code, either in the main function or in the compare function. So, was that variable created IN the list of parameters for thecompareCode function? Or do parameter lists refer to variables that are created elsewhere?

Hopefully that's more clear.
Thanks for the help.
I am not sure if this answers your question but since
length
is a value parameter, memory will be allocated for it.
Last edited on
A function prototype, or a function definition tells the compiler what variables to create as parameters when the function is called. Those variables are created immediately before the function code is executed.

So when the compiler encounters a function call, it will look at the function definition or the function prototype and create the variables specified in the parameter list.

So the variables specified in the parameter list are created every time the function is called and they are destroyed when the function exits.
When you declare a variable as a function parameter, you're saying two things:
1. The function takes this as a parameter.
2. Some stack space should be reserved to hold the value of the parameter.
So, to answer your question,
In this program, as an example, the parameter int length is not created as a variable anywhere else in the code, either in the main function or in the compare function. So, was that variable created IN the list of parameters for thecompareCode function?
Yes. The fact that you included length in the parameter list also reserved memory for it.
Wow THANKS. Now I have it. Galik, you nailed it, just what I was asking. Helios, you added to it in a way that makes me understand the mechanics of it.


Thanks gentlemen.
Topic archived. No new replies allowed.