r/cprogramming • u/Due_Butterscotch1154 • Jul 23 '26
How to move around multiple values from functions to functions?
I'm tryna mess around in C and make a pkmdmg calc but I don't know how to return the values from the function
I dont know how to display it without screenshots
1
Upvotes
11
u/Defiant_Direction_54 Jul 23 '26
Have a look at structs, you can pass one in as an argument and edit it's values within the scope of said function
3
u/Rich-Engineer2670 Jul 23 '26 edited Jul 23 '26
If you are asking how to return multiple values from a C funch such as:
x, y, z := Funcname()
You really can't do so directly. Go and a few other languages can do it, but it's all syntactic sugar. Under the hood, these languages either do some "stack" work, or create structures. You can do the same thing. For example:
type PlotResult {
int x;
int y;
int z;
}
func DoSomething() *PlotResult {}
Now, in C++ etc. you can create tuples, but it is again, syntactic sugar under the hood. Always remember, C is just an extremely powerful assembler. It's just pushing things onto the stack and popping the off most likely. If you run Linux, do a gcc -s and see the assembly code that actually does it.
I've even, under duress, borrow other items from language like Lisp. If I have a large block of return data, one can always create a list and return a reference to it. Not pretty, but it works.
Also remember pushing "larger objects" around like this, while it works, you've pushing a lot of stuff on to, and off the stack -- not efficient and you have only so much stack space. Pointers are your friend....
func DoThingy() *PlotResult
In this case, you're pushing a POINTER to some static data in the function on to the stack. ONE POINTER. So long as you don't make another call to wipe that out, you can then just dereference the point and get what you need. This optimization doesn't really matter if you have a machine with 32GB of RAM perhaps, but if you're writing embedded code or you're writing for a machine with 32K of RAM (K, not GB), every bit counts.
6
u/zhivago Jul 23 '26
1
u/sciencekm Jul 23 '26
If the values are of the same type, you can use arrays. If the values are of different types, you can use structures.
1
u/Paul_Pedant Jul 24 '26
Display without screenshots: Write any debug to a file, and view it in an editor. The method depends on what environment you are working in. The really depends on what IDE you are working in.
If you are actually trying to add your results directly into PacMan, life is going to become really complicated.