Jump to content

[SOLVED] declaring a variable as global in a function or calling by reference?


HoTDaWg

Recommended Posts

hi there,

 

i'm a bit confused by the difference between creating a function and referring to its variables by reference, or by just declaring the variable as global.  What is the difference between both methods?

 

thanks,

 

HotDawg

Global should only be used for certain situations. It is better to pass regular variables as parameters.

 

One example of a global variable could be a database connection that you want to use in a function. Show the function you are working on and it can be explained a bit more.

oh thanks for the help

i didnt have a specific case

i am currently learning from a book and was wondering which would be best in which situation, or if i understood the concepts of calling by reference/globals correctly

thanks for the help

Functions should be passed any data they need as parameters and return any results. Passing a parameter by reference (a pointer to the actual data) so that the function operates on the original data should be avoided as well. If you have data and functions that are so closely related that you would end up considering using global, you should instead be using a class/OOP to embody that closely related data and function(s).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.