Is it good practice to return different variables based on the parameter value?

Say I have a GetSomething(char type) function which returns two different variables based on what you pass it, like so:
1
2
3
4
5
6
7
8
9
10
11
float Flashlight::GetSomething(char type){
	if(type == 'x'){
		return velocityX;
	}
	else if(type == 'y'){
		return velocityY;
	}
	else{
		std::cout << "Debug: Programming error getting flashlight velocity\n";
	}
}

On a scale of null pointers to (insert something notably good), how good or bad is this practice? Just asking out of pure curiosity.
It is not absolutely terrible; except that you must return a value even if the argument is not 'x' or 'y'.
Or not return at all (throw an exception).

Why not have two functions?
1
2
float Flashlight::veclocity_x() const { return velocityX ; }
float Flashlight::veclocity_y() const { return velocityY ; }


Why is the type float (not double)?
Ok I'll add seperate functions then, I just like knowing of alternate routes. I'm using floats because sfml is only compatible with floats. What is wrong with a float versus a double?
> What is wrong with a float versus a double?

double is the natural floaing point type in C++; 5.78 is double, not float. A conversion from float to double is a promotion.

On modern processors, the difference in performance between float and double is very small, while the differences in range and precision are significant.

When several million numbers are involved, float's smaller memory footprint gives it a small edge; more numbers fit into a cache line; uses less memory bandwidth; SIMD instructions may be able to parallelise more operations.

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
41
42
#include <iostream>
#include <cmath>
#include <numeric>
#include <ctime>

template < typename T, std::size_t N > std::clock_t test( T (&a)[N], T& v )
{
    const auto start = std::clock() ;

    std::fill( a, a+N, 1 ) ;
    for( std::size_t i = 0 ; i < N ; ++i )
    {
        a[i] += std::sin( a[i] ) ;
        a[i] *= ( 1 + std::sqrt( a[i] ) ) ;
        a[i] -= std::cos( a[i] ) ;
        a[i] = std::abs(a[i]) ;
        a[i] += std::log( a[i] ) ;
        a[i] *= ( 1 + std::sqrt( a[i] ) ) ;
        a[i] += std::lgamma( a[i] ) ;
    }

    v = std::accumulate( a, a+N, 0 ) ;

    return std::clock() - start ;
}

int main()
{
    constexpr std::size_t N = 1024*1024*4 ;

    {
        static float f[N] ;
        float v ;
        std::cout << "float: " << test(f,v) * 1000 / CLOCKS_PER_SEC << " millisecs\n" ; 
    }

    {
        static double d[N] ;
        double v ;
        std::cout << "double: " << test(d,v) * 1000 / CLOCKS_PER_SEC << " millisecs\n" ; 
    }
}

clang++ -std=c++14 -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors main.cpp -lsupc++ && ./a.out
g++ -std=c++14 -O3 -Wall -Wextra -pedantic-errors main.cpp && ./a.out

float: 1110 millisecs
double: 1300 millisecs

float: 1050 millisecs
double: 1240 millisecs

http://coliru.stacked-crooked.com/a/34c2f0812c8baa5e
Topic archived. No new replies allowed.