That still leaves void as a valid keyword for a return-type doesn't it?
Printable View
That still leaves void as a valid keyword for a return-type doesn't it?
Of course.Quote:
That still leaves void as a valid keyword for a return-type doesn't it?
You do not really nead temp pointer p - string passed as parameter could do the sameQuote:
Is this better:
You will need it only to return the original pointer (it is suitable for chaining function calls like)
Code:printf("%s",firstLetUpper (test_string));
Void is still a valid return type, yes.
And the new-and-improved first letter uppercasing looks good to me.
Huh? A void function is simply one that doesn't return anything. The word "void" means "without value" - so a function that has a void return type returns "nothing of value" [in this case, specifically the compiler refuses to let you assign the return value, so--Code:
void func()
{
... // some code
}
int main()
{
int a;
a = func(); // This will be an error.
...
Mats
So a void function is used for an effect on code and any other would be used to actually calculate the value of something, right?
This comes to "side effects" and "results".
A side-effect is something that happens inside the function that affects the state of things outside of the function in some way that affects the programs operation.
A result is
Yes, a void function has only "side-effects", no actual result [or possibly, no effect at all] [1]. A "pure" function would not have any side-effects, only a result.
Many functions have side-effects and a result - getc() for example takes a character from the stdin buffer - so it modifies the state of stdin, as well as giving your the current character. We could do this with two different functions : One to get the current character (which returns a char), and another to move to the next character (which would be a void function). But it would be pretty stupid to do that most of the time [if not ALL of the time].
Examples of "pure" functions is:
sin()
cos()
feof()
These functions do not change anything that can be noticed by the rest of the system - they just return some result representing what you asked for (e.g. if the file is at End of file, or the calculation of sine or cosine).
[1] I'm assuming for the sake of argument here that functions that "do nothing and return nothing" have no effect at all. Of course, in a real system, calling any function has at the very least the side effect that some time is passed - but for this discussion, I'm ignoring "time" as a side effect.
--
Mats
An empty void functionwould have no result and no effect. A function like MS Windows Sleep() only has effect when it comes to "time", so according to my principles above, it has no effect either (it doesn't change in any way the result of your program [1], for example).Code:void func()
{
// No code here
}
[1] Unless of course your program has some dependency on time - either by using time to perform calculations, or because time affects how other functionality in your program works - multithreaded programs that are badly written will fail if the timing is right, so adding a sleep will potentially affect how such a program behaves.
--
Mats
Okay, thanks.