parameter variable c language

tshuck

Joined Oct 18, 2012
3,534
I will assume by parameter variable you mean the object passed to a function...

They all differ in scope, meaning they only exist as long as they are within scope. globals, for instance, have....well, global scope, and as such last the entire length of the program. You can modify them and use them anywhere within your program. locals, on the other hand, only last until the end of the section that they were created in, until '}'. Like the local scoped variable, the parameter of a function exists until the end of the function*.

*there is, however the possibility you passed a variable by reference, that was defined outside the function call. In this case, you will be operating on the variable whose scope is the same as the function call and will last until the section that called the function has ended.


This might help you....
Rich (BB code):
#include <stdio.h>

void PassByValue(int);
void PassByReference(int*);

//this is a global variable
int global1 = 1;

void main()
{
    printf("global1 is: %i\r\n",global1);

    //this variable has local scope to main()
    //it will cease to exist after main() has ended
    int x = 2;

    printf("x is: %i\r\n",x);

    PassByValue(x);

    printf("x is: %i\r\n",x); 

    PassByReference(&x);

    printf("x is: %i\r\n",x); 
}
void PassByValue(int value)
{
    //this will modify the value of the
    //variable passed in
    //value will only contain the value
    //until the function returns
    value = value * 2;
}
void PassByReference(int* reference)
{
    //this will modify the contents of the
    //location pointed to by reference
    //reference will contain the value
    //until the variable it points to
    //no longer exists
    *reference = (*reference) * 2;
}
 
Top