1

I'm not sure whether it's appropriate to ask this here. I'm concerned about the memory allocation and I don't know where to read up on that.

I'm debating with myself whether I should declare local variables per root-finding function, or just use global variables that each function can reuse per call. Please note that I don't plan on using recursion here. Just (do-)while (or for) loops only.

I'd really appreciate it if I can get sufficient explanation too.

// [1]
// global(?) variables
// functions.cpp

#include <cmath>
#include "functions.h"

using namespace std;

double guess1;     // upper
double guess2;     // lower
double root;
double prevGuess;
double sigFigs;
double minSigFigs;

double newt_rhap(params)
{
   // do stuff
}
double bisection(params)
{
   // do stuff
}

===============

// [2]
// local variables
// functions.cpp

#include <cmath>
#include "functions.h"

using namespace std;


double newt_rhap(x,y,z,f(),f_prime())
{
   double guess = x;
   double sigFigs = y;
   double minSigFigs = z;

   // do stuff
}
double false_position(w,x,y,z,a())
{
   double xr;
   double upper = w;
   double lower = x;
   double xprev;
   double fxr;
   double fxu;
   double fxl;
   double sigFigs = y;
   double minSigFigs = z;

   // do stuff
}
Nogurenn
  • 858
  • 3
  • 11
  • 28
  • 5
    Don't use globals... That's just silly. Declare variables within each function. – Charles Salvia Dec 03 '13 at 12:44
  • Is it going to be a big problem? – Nogurenn Dec 03 '13 at 12:45
  • 2
    No.... It would be more of a problem if you used globals – Charles Salvia Dec 03 '13 at 12:46
  • I would recommend explicitly making your function pointer parameters actual pointers. It's what the overwhelming majority of people are used to seeing (if only that happened with "arrays" as well). – chris Dec 03 '13 at 12:46
  • @chris You mean something like ``someFunction(returnType (*func)(paramType))``? Do correct me if I'm wrong. – Nogurenn Dec 03 '13 at 12:51
  • @chris I read the syntax while looking up on how to pass functions as parameters. My professor told me that it's fine to do it like ``returnType func(paramType)``, at least for our exercises. Are there complications if I didn't explicitly make it actual pointers? – Nogurenn Dec 03 '13 at 12:54
  • What exactly are your concerns about memory allocation? – JeffRSon Dec 03 '13 at 12:55
  • 1
    @solitude, No, they are interpreted by the compiler as function pointers when used as a parameter, but as I said, almost everybody explicitly states them as such. – chris Dec 03 '13 at 12:58
  • @JeffRSon I'm just not sure if declaring and defining variables every time a function is called within one runtime is a good idea. – Nogurenn Dec 03 '13 at 12:59
  • @solitude, it's a GREAT idea. Unless you're talking about some object that's really expensive to initialize – Charles Salvia Dec 03 '13 at 12:59

3 Answers3

1

You should use globals when you want to share the value of variables between several functions and want to avoid passing the values inbetween the functions.

In other cases it is better to use local variables. With globals you need to be sure the value wasn't modified accidently somewhere.

Additionally, you might have to frequently reintialize the values before usage.

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
0

Don't use global unless you know that the variables value is going to be same for all the functions that you use are defining. But it is better to avoid globals.

tamilps2
  • 46
  • 1
  • 5
  • I was planning on giving it a different value for each function call. Are global variables supposed to be unchanging/static? – Nogurenn Dec 03 '13 at 12:49
  • It is reusable if it is value that dosen't change. You don't need to declare it again and again if you are having (say 20 functions using that value). Use constants for values that don't change, that would be even better so that you don't change it's value by accident. – tamilps2 Dec 03 '13 at 12:51
  • 2
    In general, it's best to avoid using global variables at all, unless you have some really good reason – Charles Salvia Dec 03 '13 at 12:52
  • 1
    See http://stackoverflow.com/a/485020/168288 – Charles Salvia Dec 03 '13 at 12:54
0

Always try to avoid using Global variables unless you need some particular variable/values common or to be accessed in other functions also. Because by declaring Global variable as when your program get bigger and bigger then you will face difficulty in debugging,that is at which particular function you global variables are getting wrong values. Like you were expecting 10 but it giving 20 then you have sit and debug which function does this. That is why Java like language does not allow Global variable concept itself.

Vinod
  • 61
  • 3