0

I'm looking at the following function which is supposed to return (uint8 id, uint8 nextId). I see that in the body of the function, the return statement is within an "else" statement - see toward the end of the function. Will this function still return id, nextId every time it is run even though the return statement is within an else statement that won't always be executed?

Another way to ask this is do you have to explicitly state within the body of a function that you're expecting it to return something or is it enough to just state it when you define the function?

 function setShortRecordIds(address asset, address shorter)
    private
    returns (uint8 id, uint8 nextId)
{
    AppStorage storage s = appStorage();

    STypes.ShortRecord storage guard = s.shortRecords[asset][shorter][Constants.HEAD];
    STypes.AssetUser storage AssetUser = s.assetUser[asset][shorter];
    // Initialize HEAD in case of first short createShortRecord
    if (AssetUser.shortRecordId == 0) {
        AssetUser.shortRecordId = Constants.SHORT_STARTING_ID;
        guard.prevId = Constants.HEAD;
        guard.nextId = Constants.HEAD;
    }
    
    nextId = guard.nextId;
    uint8 canceledId = guard.prevId;
    
    if (canceledId > Constants.HEAD) {
        
        uint8 prevCanceledId = s.shortRecords[asset][shorter][canceledId].prevId;
        if (prevCanceledId > Constants.HEAD) {
            guard.prevId = prevCanceledId;
        } else {
            
            guard.prevId = Constants.HEAD;
        }
        
    } else {
        
        id = AssetUser.shortRecordId;
        
        if (id < type(uint8).max) {
            AssetUser.shortRecordId += 1;
        } else {
            // If max id reached, match into max shortRecordId
            return (id, nextId);
        }
    }

    if (nextId > Constants.HEAD) {
        s.shortRecords[asset][shorter][nextId].prevId = id;
    }
    guard.nextId = id;
}

1 Answer 1

0

Another way to ask this is do you have to explicitly state within the body of a function that you're expecting it to return something or is it enough to just state it when you define the function?

You have 2 options to declare that your function returns something when declaring it :

function unassignedReturn() public returns(uint) {
  ...
}
function assignedReturn() public returns(uint retVal) {
  ...
}

In the first case, if you don't have return something; in your function body, the compiler is going to complain and i believe (might wanna double check) it would lead to undefined behavior for the return value ( = it's gonna return whatever is in memory. Again, I could be wrong). In the second case, the variable is initialized in the function declaration and if it's not set in the function body, it's gonna return the default value (so 0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.