r/C_Programming • u/Stickhtot • Sep 07 '26
Question How do functions that "initialize" some values and memory work only returning an irrelevant value?
For example, SDL's SDL_INIT() takes an integer parameter and returns a bool, how can returning a bool "initialize" a library and allowing me to do stuff with that library?
2
Upvotes
11
u/AlexTaradov Sep 07 '26
Libraries have global variables. They are initialized by that function.
Following APIs use that initialized state.
41
u/ir_dan Sep 07 '26
It doesn't just return a bool. Functions can do lots of things before returning a value. The value indicates whether or not the function succeeded.
73
u/stevevdvkpe Sep 07 '26
Side effects. Lots and lots of side effects.
33
u/etaithespeedcuber Sep 07 '26
Really funny how half the posts in this sub are papers about conditional switch device execution based on an addr CPU flag as a POSIX-only solution to integer overflows and the other half are "what is a function"
9
u/jaynabonne Sep 07 '26
The same way that main can print "hello world" and then return an int. A lot of things can happen before the function returns that have nothing to do specially with the return value, which can be just communicating back a result status.
13
u/nderflow Sep 07 '26
Programming functions are (often) not like mathematical functions. They can have _side effects _ such as changing the state of the program. In this case by modifying the state of the program as, probably, represented in static or global variables.
2
u/sciencekm Sep 07 '26
Libraries normally have internal states that gets setup when you call the initialization function.
2
u/Ok_Mission_3025 Sep 07 '26
The return value of true is js to indicate the function has successfully finished it's task, it's task maybe whatever internally.
2
u/thurizas123 Sep 10 '26
With SDL being open source, you can look at the code and see how SDL_INIT() works, and what it does.
A few things to consider,
For example consider this snippet 'SDL_INIT(SDL_INIT_VIDEO)'. SDL_INIT_VIDEO has the value of 0x00000020 (or in binary 0b00000000000000000000000000100000). Several other values are,
SDL_INIT_AUDIO 0x00000010 0b00000000000000000000000000010000SDL_INIT_VIDEO 0x00000020 0b00000000000000000000000000100000SDL_INIT_JOYSTICK 0x00000200 0b00000000000000000000001000000000SDL_INIT_HAPTIC 0x00001000 0b00000000000000000001000000000000Notice how in the binary representation in each of these values there is only a single one. Now suppose that I want to initialize say the audio and video subsystems. I could do that with code like this:
uint32_t systems = SDL_INIT_AUDIO | SDL_INIT_VIDEO;bool good = false;good = SDL_INIT(systems)If you are unsure what is going on here, do the bit-wise 'or' of those two values. Now, a hypothetical implementation of SDL_INIT might be:
bool SDL_INIT(uint32_t flags)
{
bool res = true;
if(res & ((flags & SDL_INIT_VIDEO) = SDL_INIT_VIDEO))
{
res &= InitializeVideo();
}
if(res & ((flags & SDL_INIT_AUDIO) = SDL_INIT_AUDIO))
{
res &= InitializeAudio();
}
..... and so on
return res;
}
Again, there is alot going on in this function (again, this is a hypothetical implementation...look at the source to see how the SDL folks did it).
If the first subsystem is initialize correctly the code will then proceed to initialize the other subsystems as needed. If any subsystem fails to initialize then 'res' will become false and none of the other subsystems will be attempted.
I hope this helps a bit.
1
u/SmokeMuch7356 Sep 07 '26
Most libraries maintain some internal state via
staticvariables, environment variables, system calls, etc. They may allocate memory for internal use, create temporary files, or allocate other system resources. You cannot modify this state directly; that's all hidden from you, and the only way it can be affected is through API calls (sort of like how you can't manipulate the contents of aFILEobject directly, you have to usestdiofunctions to do that).The init function kicks all of this off and may call other helper functions that aren't exposed to you. The Boolean return simply indicates whether all those setup operations succeeded.
1
u/catladywitch Sep 08 '26
Functions in C can have side effects and, in particular, access and mutate state that was allocated outside of them, but in the scope that contains said functions, even if it isn't passed to them as an argument.
I think it might be clarifying to look up the difference between functional and imperative programming, the concept of global state, and the difference between pure and impure functions, because introductory essays about those topics are a condensed summary of what you describe (and its potential downsides).
-1
u/ChickenSpaceProgram Sep 07 '26 edited Sep 07 '26
In this case the library is probably initializing global variables. This is usually considered bad practice in your own code, but it's sometimes useful/convenient.
0
u/elonboring1 Sep 07 '26
a function has to return something unlike constructors they hav a destructor in stead give or take a function must return a value in the case of init() is a bool thatt is equal to a flag up