Jump to content

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


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).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.