Post

Function return

Function return

Function Return


Prerequites

1
C++

What is Function Return

when we make functions, some developers do not deeply think about RETURN. But I think Return of function is very important part especially we don’t need to get back any variable. I think the return should be fixed bool or enum type if the function is not too much simple. The meaning of bool or enum is about whether this function is to be success or fail.

1. Rule of Return

I have simple two rules of deciding return type.

1
Return: bool or enum type

First, All or most of functions that i don’t know what is the best return or i know what is the return exactly should be bool or enum type.

both clients or developers want to know what function is sucess. When we debug the program or flow, we usually should check the status after functions are finished. So if we use return void, they are very confused where the process have problems.

1
Return: Any data type

Second, I want to say if we use a data type as return, it is SPECIAL CASE. There are some condition why we can use a data type as return.

  1. NEVER happen error when we call the function.
  2. TOO MUCH HARE to use as getting the output from parameters
  3. If we use the this case, we should make overload same operation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 1.
int GetResultCount()
{
    return m_i32Count;
}

# 2.
bool GetResultCount(int* pI32Count)
{
    bool bOK = false;

    do
    {
        if(!pI32Count)
            break;

        *pI32Count = m_i32Count;

        bOK = true;
    }
    while(false);

    return bOK;
}

# Too much complex to use
int main()
{
    ...
    int i32Count;
    CData.GetResultCount(&i32Count);

    ...

    return;
}

# 3.
int GetResultCount(); # commonly used functions
bool GetResultCount(int* pI32Count); # when clinets have problem, using function. 
This post is licensed under CC BY 4.0 by the author.