-1

I can routinely read parameters (e.g a and b ) as input to a c++ code by command line argument.

Q: how can I use these 2 parameters in several other functions (not the main), so they act similar to "global" variables.

here is an example, the scope a and b is just the main and not for func1 and func2. how to make a and b visible also for func1 and func2? Thanks for help!

int main (int argc , char* argv[]) 
{ 
double a=atof(argv[1]);
double b=atof(argv[2]);

return 0;
}

double func1(double x)
{
return x+a+b;
}

double func2(double x)
{
return x*x+a*b;
}
physiker
  • 889
  • 3
  • 16
  • 30
  • 5
    Use a global variable – Ryan J Nov 06 '14 at 18:12
  • there should be more elegant way, i dont want to use global variables in the conventional way. – physiker Nov 06 '14 at 18:13
  • if you are using c++11, you can bind the arguements to the function call – smac89 Nov 06 '14 at 18:14
  • 4
    You either use some form of global variable or pass them around as function arguments. – n0rd Nov 06 '14 at 18:14
  • @Snac98: how? I use g++ compiler on ubuntu – physiker Nov 06 '14 at 18:16
  • @Reza: Why don't you want to use globals? Also, why don't your functions take `a` and `b` as parameters? – Mooing Duck Nov 06 '14 at 18:17
  • @Reza _"how?"_ `double func2(double x, double a, double b)` – πάντα ῥεῖ Nov 06 '14 at 18:18
  • 3
    You should explain your purpose. There are a dozen ways to do what you ask, including global variables, singletons, wrapping your functions as methods of a class with internal state, simply passing them as arguments, using [currying](http://stackoverflow.com/questions/152005/how-can-currying-be-done-in-c)... As is, this question is way too broad to give a meaningful answer. – hyde Nov 06 '14 at 18:19
  • @Smac89 Why does it need to be c++11? – Daniel Nov 06 '14 at 18:21
  • 1
    @Daniel [std::bind](http://en.cppreference.com/w/cpp/utility/functional/bind) was introduced in c++11. This is starting to look like the [X Y Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). OP probably wants to pass as many numbers as possible to the program and have these 2 functions use those numbers – smac89 Nov 06 '14 at 18:23
  • why not global? my code has 10-15 input parameters..so not a good way. why not in function definition? of course i know how that one works!! but again, this is not practical when you have more than 10 functions. purpose (for someone who put -1): it is clear, input to be used in many functions! in real code, the main calls many other functions. – physiker Nov 06 '14 at 18:29
  • No matter how you do it, you either need to have the functions take an array of arguments or define a global array which all the functions can access. Apart from the main function, there is no other way I know of to access command line arguments directly – smac89 Nov 06 '14 at 18:34
  • @Reza: If you have additional requirements that would affect answers, [edit] your questions and state those requirements there instead of burying them in comment. If you don't include that information where people can see it, it's no one's fault but yours if they're not aware of those requirements. – Ken White Nov 06 '14 at 18:40
  • We can't read your mind... from your post, it is _not_ clear you understand what you're doing. From your last comment, having a lot of input parameters is going to complicate things anyway, so "is not a good way" is really not true. It _is_ a good way, it's just more complicated to manage all of your input arguments. Period. – Ryan J Nov 06 '14 at 18:40

6 Answers6

3

Use global variables:

double a;
double b;

int main (int argc , char* argv[]) 
{ 
    a=atof(argv[1]);
    b=atof(argv[2]);

    return 0;
}

double func1(double x)
{
    return x+a+b;
}

double func2(double x)
{
    return x*x+a*b;
}

Or function parameters:

int main (int argc , char* argv[]) 
{ 
    double a=atof(argv[1]);
    double b=atof(argv[2]);

    double x = 13.1;
    double someVal = func1(x, a, b);
    double someOtherVal = func2(x, a, b);

    return 0;
}

double func1(double x, double a, double b)
{
    return x+a+b;
}

double func2(double x, double a, double b)
{
    return x*x+a*b;
}

If you want your data to persist outside of a function, it either needs to be a global variable, or you need to use function parameters and return values to maintain scope of your data. Not to mention other forms of OO design as others have mentioned that could accomplish whatever you're doing... This is about as elegant as you can get.

Ryan J
  • 8,275
  • 3
  • 25
  • 28
0

You could copy them to actual global variables.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

Like this:

double a, b;

int main()
{
    a=atof(argv[1]);
    b=atof(argv[2]);

    return 0;
}

double func1(double x)
{
    return x+a+b;
}

double func2(double x)
{
    return x*x+a*b;
}
Lawrence Aiello
  • 4,560
  • 5
  • 21
  • 35
0

The same way you make any data available to other parts of your program: either assign them to global variables or pass them around as function arguments.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

I like to store arguments in an object to keep things tidy and avoid passing lots of loose parameters.

class Arguments
{
public:
    Arguments(double a, double b) : A(a), B(b)
    {
    }

    double A;
    double B;
};

double func1(const Arguments& args, double x)
{
    return x + args.A + args.B;
}

int main(int argc, char* argv[])
{
    const Arguments args(atof(argv[1]), atof(argv[2]));

    const double x = 22;
    const double result = func1(args, x);

    return 0;
}

If you need to pass many named arguments, like program --align 2 --method shortest --color orange, boost::program_options can save you a world of trouble.

piedar
  • 2,599
  • 1
  • 25
  • 37
0

Perhaps you want something like this:

#include <numeric>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstdlib>
#include <iostream>

int main (int argc , char* argv[]) 
{ 
    std::vector<double> values;
    std::transform(&argv[1], &argv[argc], std::back_inserter(values), atof);
    std::cout << func1(3.4, values) << std::endl;

    return 0;
}

double func1(double x, const std::vector<double>& values)
{
    return std::accumulate(values.begin(), values.end(), x);
}

double func2(double x, const std::vector<double>& values)
{
    // Do something similar here
}

Note you need to have a c++11 compatible compiler to use this code. GCC 4.7+ should be able to compile it

smac89
  • 39,374
  • 15
  • 132
  • 179