4

Why is the time function usually used like this:

time_t currentTime;
currentTime = time( NULL );

instead of this:

time_t currentTime;
time( &currentTime );

Is the first method used more just because it is arguably more readable? Or is there another reason?

Thanks.

Edit: Also, why was the time function even designed this way? Why have two ways to set the variable?

stacker
  • 68,052
  • 28
  • 140
  • 210

4 Answers4

6

It always seems to make more sense to return a value from a function than pass a paramter that is changed.

This might be because we learned on languages where functions and subroutines were different, don't know about these youngsters today.

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • @Matt what? could you elaborate? –  Jan 14 '11 at 04:55
  • @Jay - In e.g. Python where values (of numbers) are immutable, `def f(i): return i + 1` / `i = 0; i = f(i)` works, but `def g(i): i += 1` / `i = 0; f(i)` doesn't (in fact there's no really good way that I can think of to make it work). – Chris Lutz Jan 14 '11 at 05:08
1

Of course only K&R probably know the true answer, but my suspect is that is just an "incident" because of historical implementation reasons. For example may be the design of this function started as void time(time_t*) because simply wasn't possible in some form of pre-ansi C to return a value of type time_t and only later evolved in a value-returning function.

If this is the explanation then the reason for keeping the parameter anyway is of course backward compatibility with existing code.

6502
  • 112,025
  • 15
  • 165
  • 265
1

The most common format is actually time_t currentTime = time( NULL );

This is shorter, and doesn't leave the currentTime variable uninitialized. The parameter is a historic accident and has no use.

MSalters
  • 173,980
  • 10
  • 155
  • 350
-1

The timer parameter is a pointer to an object of type time_t, where the time value is stored. Alternativelly, this parameter can be a null pointer, in which case the parameter is not used, but a time_t object is still returned by the function.

So you don't need to create a time_t object.

Miles Rout
  • 1,204
  • 1
  • 13
  • 26
  • 1
    I know how the `time` function works, that is not the question. As far as your last sentence, do you mean that you don't have to create a `time_t` object on an earlier line? Because you still have to have create a `time_t` object... –  Jan 14 '11 at 04:51
  • you first code block has the unnecessary line `time_t currentTime;`. The `time( NULL )` function creates a time_t object anyway, I believe, so you don't need to create your own (you don't use it). EDIT: the timer parameter is the only parameter of the time function – Miles Rout Jan 14 '11 at 04:58
  • 1. you don't need the first line of your first example. 2. I said "The timer parameter is a pointer..." before. "timer parameter" refers to the parameter of the `time` function – Miles Rout Jan 14 '11 at 05:03
  • 1
    1. yes, on my first example I could, if I was creating the variable at that time, do it like this: `time_t currentTime = time ( NULL )`. but I don't believe that is why people use the first method, instead of the second. 2. yeah, I get that, but what is your point? –  Jan 14 '11 at 05:10