Author Topic: A question about global Vs Local Variables.  (Read 909 times)

Bazoukas

  • Member
  • **
  • Posts: 866
  • Kudos: 140
    • http://whitehouse.com
A question about global Vs Local Variables.
« on: 17 September 2002, 07:18 »
Here are few things that i havent straighten out in my head.
  Correct me if am wrong in any of my explainations.

int function (int , int);
 int function1 (int char1, int num1);

 and these are the callers.
 
  function  (char1, num1);
  function1 (char1, num1);

a) With the first function prototype I am avoiding globar variables but it expects two integers.
  b) on the 2nd function prototype char1 and num1 are prety much global variables. So if i use them in a block they get overwriten.
 


   Why not use voidfunction() and then later on call it? It doesnt lock you to a specific type of value, or there is a benefit that i cant see yet?
Yeah

flap

  • Member
  • **
  • Posts: 1,268
  • Kudos: 137
A question about global Vs Local Variables.
« Reply #1 on: 17 September 2002, 14:37 »
Your 2 function prototypes are effectively exactly the same.
I _think_ when you say "global variables" you're actually referring to variables passed by reference as opposed to by value.

That is, if you pass a variable to a function by reference, you are passing a reference to the original variable and so the function may change its value. If you're passing by value you're passing a copy of the variable so the function may modify it without changing the original.

In C++ (which I'm assuming you're using from an earlier thread of yours) you can use 'references' to accomplish passing by reference. Thus your function prototype would be
int function (int&, int&);
or
int function (int& num1, int& num2);
"While envisaging the destruction of imperialism, it is necessary to identify its head, which is none other than the United States of America." - Ernesto Che Guevara

http://counterpunch.org
http://globalresearch.ca