C ++ variable and function

therefore, in my main function, I have a function called with arguments stored in a variable. I run my program and the variable containing this function is executed. I thought that when I store functions or anything in a variable, this should not be done until I say.

eg:

int cycle1 = cycleList(argument1, argument2);

this statement above is now being executed on my screen. Is this the right way to write code? I wanted to store the function in a variable and then use the variable somewhere in my code.

+3
source share
7 answers

If you want to save the result of a function at this point in time during program execution, then yes, you are doing it right.

+1
source

, , , . :

#include <functional>

std::function<int (int, int)> cycle1 = cycleList;

, ++ 11, :

int (*cycle1)(int, int) = cycleList;

:

 cycle1(argument1, argument2);
0

++ , ; , , . , int, .

, :

// Store a lambda function object, capturing arguments to call it with.
// This doesn't call the function.
auto cycle1 = [=]{cycleList(argument1, argument2);};

// Call the function later. This calles 'cycleList' with the captured arguments.
int result = cycle1();

, , , .

0

, ( ) , , :

#include <iostream>

int cycleList(int arg1, int arg2) { return arg1 + arg2; }

struct cycleListObj
{
    int arg1, arg2;

    // constructor stores arguments for later use
    cycleListObj(int a1, int a2): arg1(a1), arg2(a2) {}

    // overload function call operator()
    int operator()() { return arg1 + arg2; }
};

int main()
{
    int result1 = cycleList(1, 1);   // stores 2 into result1
    cycleListObj fun(1, 1);          // defines a function object fun with arguments 1, 1
    int result2 = fun();             // calls the function object, and stores the result into result2
    std::cout << result1 << result2; // outputs 22
}

Live

, ++ std::function, .

, . .

0

. , , .

:

int Add(int num1, int num2)
    {
        return num1 + num2;
    }

    int main()
    {
        int result, input1, input2;

        cout << "Give a integer number:";
        cin >> input1;
        cout << "Give another integer number:";
        cin >> input2;

        result = Add(input1,input2);

        cout << input1 << " + " << input2 << " = " << answer;
        return 0;
    }

Add() main(), , Add() . , main(), add() , num1 + num2. .

0

, , . , . , . , .

What can I offer you in this case, this code is able to. There must be a suitable time or a satisfactory condition when you want this method to execute and calculate the result for you. For instance:

public class BaseCondition {
    public int compute(int a, int b) {
        return (a + b);
    }

    public boolean set(boolean flag) {
        flag = true;
        return flag;
    }

    public int subtract(int a, int b) {
        return (a - b);
    }

    public int callCompute(int a, int b) {
        boolean flag = false;
        int computedVal = 0;
        if (a < b || a == b) {
            flag = set(flag);
        }
        if (flag) {
            computedVal = compute(a, b);
        } else {
            computedVal = subtract(a, b);
        }

        return computedVal;
    }

    public static void main(String[] args) {
        BaseCondition obj = new BaseCondition();
        int a = 11;
        int b = 51;
        System.out.println("Result=" + obj.callCompute(a, b));

    }

}

Here you can find that the calculation will be called only on the basis of a flag that is set only when the condition is met. Hope this helps :)

0
source

You can also do the following using auto

#include <iostream>
using namespace std;

int Foo()
{
    return 0;
}

int main() 
{
    // your code goes here
    auto bar = Foo;
    return 0;
}
0
source

All Articles