Jump to content

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


HoTDaWg

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.