I did the exercise with a boolean function instead. Good enough?

I started the challenge by going to do a google search on how you define an array-returning function in c++, and according to this link https://www.tutorialspoint.com/cplusplus/cpp_return_arrays_from_functions.htm
that wouldn’t be possible or easy with the knowledge I have.
I’m guessing the TArray is specific to Unreal and that’s why we can return arrays?

I end up using this code at the beginning of the exercise

  TArray<FString> ValidWords;

        for (int32 Index = 0; Index < 10; Index++)
    {
        if (GetValidWords(Words[Index]))
        {
        ValidWords.Emplace(Words[Index]);
        }
    }

    for (int32 Index = 0; Index < ValidWords.Num(); Index++)
    {
        PrintLine(TEXT("%s"),*ValidWords[Index]);
    }


and after, at the end I defined the GetValidWords function as a boolean like this

bool UBullCowCartridge::GetValidWords(FString WordToCheck)
    {
        if (IsIsogram(WordToCheck) && WordToCheck.Len() >= 4 && WordToCheck.Len() <= 8)
        {
            return true;
        }
        else
        {
            return false;
        };
        
    }

This actually worked in my project, I guess this might not be a nice way to do it though. Do you have any thoughts about why this would be not a good way of doing it compared to the way it’s done in the course?

That’s technically fine and arguably more readable but I just want to point out the fact that it’s essentially a single line

bool UBullCowCartridge::GetValidWords(const FString& WordToCheck)
{
    return IsIsogram(WordToCheck) && WordToCheck.Len() >= 4 && WordToCheck.Len() <= 8;
}
1 Like

So returning the conditions will return true or false as the function returns a boolean. Didn’t know you could structure the function like that. Thanks

It’s not really related to how functions work. That expression returns a value which can then return from the function directly. To perhaps make it clearer by introducing a variable.

bool bIsValid = IsIsogram(WordToCheck) && WordToCheck.Len() >= 4 && WordToCheck.Len() <= 8
return bIsValid;

The same thing is happening here but with a variable. Same with putting that directly in the if statement.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms