Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good design to return value by parameter?

Tags:

bool is_something_ok(int param,SomeStruct* p) {     bool is_ok = false;      // check if is_ok      if(is_ok)        // set p to some valid value     else        // set p to NULL     return is_ok; } 

this function return true and set p to a valid value if "something is ok" otherwise return false and set p to NULL

Is that a good or bad design? personally, i feel uncomfortable when i use it. If there is no document and comment, i really don know how to use it.

BTW:Is there some authoritative book/article about API design?

like image 775
aztack Avatar asked Mar 16 '10 07:03

aztack


People also ask

Why are parameters and return values useful?

Goal: Parameters and return values allow students to write programs that are more organized and cleaner. Naming functions helps students write programs that read more like descriptions of what they do, and they also help students reuse code.

Do parameters return values?

The parameter is a string in which you will count the words. The argument is any string you pass to your function when you call it. The return value is the number of words.

What is the difference between parameters and returned values?

A return value is a result of the function's execution. It can be returned to the block of code that called the function, and then used as needed. Parameters are the necessary input for a function to be executed and produce a result. Parameters are variables defined by name.

Can a function have parameters and return values?

Introduction. A function can take parameters to use in its code, and it can return zero or more values (when more values are returned, one of the values often speaks of a tuple of values).


1 Answers

Since you have tagged the question as C++ and not C, I would suggest you to:

  • return the value directly
  • if you have more than one value, use output parameters
  • use non-const references as output parameter where possible (instead of pointers), and use const-references for the input parameters.
  • if something went wrong, raise a exception instead of returning false or -1.

But that are just some general hints. The best way to go always depends on the specific problem...

like image 121
tux21b Avatar answered Sep 27 '22 21:09

tux21b