I was having a chit-chat with a friend discussing some C language shenanigans
when he sent me a code snippet, he wanted me fix the code and get it to work in
4 different ways.
Here is the snippet:
|
|
There are two main takeaways from the above snippet:
- The
calc
function is never called in main. - Even if we called it in main, the
sum
andmul
variables are locals to the
scope ofcalc
.
The obvious solution to me was to just make these variables globals then we can
just call calc
in main and we’re done, but he wanted me to do it in 4
different ways, that was clearly an exercise of passing values around in C.
Using Global Variables
|
|
Output
sum=60 mult=500
This is pretty simple, by making the sum
and mul
variables global, we just
expand their scope to the whole program.
Using Pointers
|
|
In this solution we declared these variables in the main
function and
passed them by pointers (i.e. we passed a reference to their location in
memory), by doing this we can freely modify a variable from another scope since
we directly access the variable’s location in memorey.
i.e. we dereference a memory address and modify the value that the address is
pointing to.
Bonus: Passing by Reference (C++)
|
|
C++ allows us to directly pass the address of sum
and mul
without using
pointers, we just pass them by reference.
That’s why I think people should be careful of using the terms “pass-by-pointer”
and “pass-by-reference” interchangeably as these are two different things.
Using Arrays
|
|
Passing values with arrays are useful when our values have the same type, It is
very similar to passing by pointer/reference since we actually pass the address
of the array to the calc
function, It’s values is then modified using the
array notation my_array[index] = value
which is really equivalent to*(my_array + index) = value
.
Using Structs
|
|
C structs are a decent way of passing multiple values around functions,
especially when these values are of different types, we can also get around
typing Struct Result
a bunch of times by adding a typedef that introduces a new
type in our code, It makes our code a bit cleaner and easier to read.
This is the same code but with introducing the typedef.
|
|
Note: All these solutions are ways to return multiple values from a function, unlike modern languages like python we can’t do this in C, another good solution is splitting the calc
function two two spearate functions where each one of them will return It’s result, sometimes this can be better than the above approaches and It’s all depending on taste.
|
|