Question on how I can use and obtain just the value of return after function is executed

how to use return value in another function python
function with return value in c
python function return variable
python function return multiple values
function return python
how many return statements are allowed in a python function
return function
javascript function return value to variable

I am making a finite state machine for a coding class. How can I use the return value to change the value of int HP in my main so I will not have any other problems with my code. I just want to make it able to manipulate the value of HP, and use the new value of HP for more functions.

Sorry if the fix to this problem is really simple. I am struggling to understand how functions work in C++ and cannot seem to find a solution any other place online or reading tutorials.

#include <iostream>
#include <time.h>
using namespace std;

int forage(int HP) {
    cout << "The ant is in foraging state."<<endl;
    if (rand() % 100 < 60) {
            cout << "The ant found something!"<<endl;
        if (rand() % 100 < 10) {
            cout << "The ant found poison!" << endl;
            HP -= 1;
        }
        else {
            cout << "The ant found food!" << endl;
            HP += 1;
        }
    }
    int mHP = HP;
    return mHP;
}


 int main() {
        srand(time(NULL));
        int mHP = 0;
        cout << "Welcome to Ant Simulator"<<endl;

        forage(10);
        cout << mHP;
        system("pause");
        return 0;
    }

You have a couple of choices. One possibility is to pass HP by reference, and have forage modify what was passed in:

void forage(int &HP) {
    cout << "The ant is in foraging state."<<endl;
    if (rand() % 100 < 60) {
            cout << "The ant found something!"<<endl;
        if (rand() % 100 < 10) {
            cout << "The ant found poison!" << endl;
            HP -= 1;
        }
        else {
            cout << "The ant found food!" << endl;
            HP += 1;
        }
    }
}

Another possibility is to just use the result returned from forage:

mHP = forage(10);

If you're going to do this, you can add an annotation so that a recent compiler will tell you about the problem if you accidentally ignore the value it returned:

[[nodiscard]] int forage(int HP) {
// ...

The [[nodiscard]] tells the compiler you want to be sure the value returned from this function isn't discarded like your code in the question did.

As an aside, I'd also prefer that forage be separated into a couple of separate pieces. I'd prefer to have one piece that deal strictly with the UI (displaying the strings about what happened) and another that deals strictly with the logic of the game itself. As a starting point, you might consider passing a stream as a parameter, and having forage display to that stream:

void forage(int &HP, std::ostream &s) {
    s << "The ant is in foraging state.";
    if (rand() % 100 < 60) {
            s << "The ant found something!\n";
        if (rand() % 100 < 10) {
            s << "The ant found poison!\n";
            HP -= 1;
        }
        else {
            s << "The ant found food!\n";
            HP += 1;
        }
    }
}

This can help with things like porting the game to work under a windowing system, if you ever decide to do something like that.

Computer Programming: 4 Books in 1, print("This is what I received from return_me_ten():", number) answer If you're still having some trouble grasping parameters, you should just look at them as When the statement “return ten” is executed, the function will send the value to The third function aptly named “ask_yes_or_no” will receive a question and return  The best way to do this would be to use the promise returning function as it is, like this. lookupValue(file).then(function(res) { // Write the code which depends on the `res.val`, here }); The function which invokes an asynchronous function cannot wait till the async function returns a value.

Change forage(10); to mHP = forage(10);

Your function forage is of return type int. If you want to get this return value into your variable mHP, you need to assign the return value of the function to the variable as described above.

FREQUENTLY ASKED INTERVIEW QUESTION'S: SELENIUM USING PYTHON, submit() just submits the form without any UI actions, and executes the url that is Calling click() , the JavaScript functions will be executed as it simulate actual click url Ans: Use driver.get(link) How to read the javascript function's return value in We can use following code the get the value of global variable country from  Bash functions, unlike functions in most programming languages do not allow you to return a value to the caller. When a bash function ends its return value is its status: zero for success, non-zero for failure.

Just to add to the previous answers... For a basic understanding how a function works with your defined function as an example:

int forage(int HP){...}

The int prior to the function name defines the return type, so basically what your function is giving back at the end of execution. Then comes the name of your function, in this case forage, followed by the input parameters. In your case there is only one single input parameter which is an integer value int HP. All the code inside of the curly brackets is executed at function call.

Now all functions that do not have the return type void have a return statement somewhere (most of the times at the end) in their code. The actual return value is assigned to a variable like this:

int returnedValue; 
receivedValue = forage(10); 

6.2. Functions that Return Values, Furthermore, functions like range , int , abs all return values that can be used to build Try modifying the square function above to see that this works just the same. The call to a function terminates after the execution of a return statement. The problem with this function is that even though it prints the value of the square,  I think here you may be trying to use the finally block in an odd way. The Framework documentation typifies finally as follows: The finally block is useful for cleaning up any resources allocated in the try block. If this were valid code, what would the expected result be? The finally block gets executed after the first return.

12.5. Returning a value from a function, Not only can you pass a parameter value into a function, a function can also produce a Try modifying the square function above to see that this works just the same. Since line 6 uses the return value as the right hand side of an assignment A return statement, once executed, immediately terminates execution of a  Return Type: A function can send a value to the part of the program that executed it. The return type is the data type of the value that is sent from the function. Name: You should give each function a descriptive name. In general, the same rules that apply to variable names also apply to function names.

CBSE Chapterwise & Topicwise Question Bank, Compter Science (for , Questions-II Find the error(s) in the following code and correct them: 1. def describe And here, no variable is defined to obtain value being input. The globals( ) and locals( ) functions can be used to return the names in the global A docstring is just a regular python triple- quoted string that is the first thing in a function  The SELECTEDVALUE function simplifies the syntax of a common pattern involving two functions (HASONEVALUE and VALUES) to retrieve a value from the filter context. Just remember that this technique is not required when you have a row context, because you can simply use a column reference and the RELATED function when a lookup table is involved.

2.2, When you write a user-defined function, you get to determine whether your Second, inside the function that will return a value, we use a return When the return statement is executed, the return value is copied from the Question #1 Since you've probably skipped more than just the lesson about  Its return value (another commentary function) should be called at the end of the second turn. Each turn, a new commentary function should be called that is the return value of the previous call to a commentary function. Also implement both, a function that takes two commentary functions (f and g) and returns a new commentary function.

Comments
  • Something like mHP = forage(10); perhaps?
  • Hi, welcome to Stack Overflow. Given that this is a rather basic question, it might be more beneficial for you to grab and follow a good C++ book; that will give you a more solid, integrated understanding than piecemeal questions on SO can.
  • WARNING: Using rand() is considered harmful and you’re strongly encouraged to use an appropriate random number generator facility in the Standard Library that produces high-quality random values. Your use of time(NULL) as a random number seed means that this will produce identical results if run in the same second, and on many platforms rand() is barely random at all.
  • Please elaborate, give some explanation of what the change does.
  • @Abyx The person asking the question has obviously little knowledge about C++ just throwing code at him will change nothing about that. If you add context to your answer he may learn from it.
  • @Abyx "How about you give an explanation to your comment?" In addition to that, all information, relevant to an answer, must be present in the answer itself, not in the comments.