Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,216,015 members, 8,028,166 topics. Date: Friday, 13 December 2024 at 05:30 PM

A Simple Explanation Of Functions And Stacks - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / A Simple Explanation Of Functions And Stacks (2103 Views)

PHP Functions To Clean Database Inputs / Most Used Mysql Database Functions / Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD (2) (3) (4)

(1) (Reply) (Go Down)

A Simple Explanation Of Functions And Stacks by WhiZTiM(m): 11:57am On Apr 02, 2015
Sequel to a request on this post, https://www.nairaland.com/2216760/simple-c-codes-still-confuse-me#32101262
I thought it good to reply with a new post, so that a wider community of people can learn.
#####
Hopefully, this post/reply is brief, concise and not too technical. (experienced programmers too will gain something)
#####
#####
===== FUNCTIONS and STACK======

----------- A small picture --------
Consider a man named Musa who has to execute some tasks; those tasks are fully wrapped in a box. (Think of each box like a portable version of your local Grinding Machines/Blender where you grind Tomatoes, maize, etc; You know there are different kinds, sizes, and types of grinding Machines, so are there different type of boxes smiley ).

Musa is in a warehose full of Boxes; Now There is long enclosed rail with only one end accessible to Musa where he can push Boxes to, if he wishes; Each box cannot work on its own without a power supply; And the only power supply is at the end of the rail, where Musa sits.

-----Relating the above.....------

Your program starts from the function "main"; in this case, You have given Musa a box to process;
Musa, places the Box on the end of the rail, plugs it and starts processing it. At some point, He reaches a point where he cannot proceed without the result of another function(Box); He then stops there; but He is given some data (lets say Tomatoes) to call the other function with;

Musa unplugs "main" and pushes it further into the rail... then gets up and walks through the warehouse in search of the Box he needs to proceed. He retrieves it and brings it to his desk; Lays it on the end of the rail. remember, "main" has been pushed further into the rail. He plugs the new function(box) into the power supply, pours the Tomatoes into it and starts processing....

If He reaches a point where he needs, another function to proceed, ...then the entire process is repeated until the functions starts returning (i.e a function executed to completion)...
When a function is returning... Musa collects the results(lets say result of grinding tomatoes and onions); then He cleans up that machine(Box). Unplugs it and returns it to the warehouse floor. He then rolls the rail forward until the last Box He was using pops out. He connects it, and continues from where He stopped, this time with the results of the previous function. Hence, he can proceed. :-)

Now, that my friend, is a function and a stack. smiley!
The rail, is your stack, the boxes are your functions!

When we are returning from a function(cleaning up the machines), Destructors of objects are called. (In C++ and D).
We can simply view a stack as part of your RAM that is grows and shrinks with respect to function calls;

======= Extras ========
- There is something called stack unwinding. This simply means the minimum work you must do to remove boxes from the rail.
- There is something called stack overflow. This simply means having too much items on your rail to extents that it's full, therefore, preventing you to push more boxes into it.
- When a program is multi-threaded, it means, you have multiple Threads(box rails) and Executors(Musa)
- Functions may be inlined, meaning boxes embedded in boxes, thus Musa, doesn't have the burden of going to look for it.
- Every program starts with a function. (some may be in a global disguise, like scripting languages)

======= Technical =======
- Functions are fast because, every variable inside is addressed in a very simple fashion; relative offsets;
The CPU simply reads from direct offsets in order to retrieve the value you want to read;
Interestingly, (most at times) the data doesn't need to be re-read from RAM, because while the function is fetched, the entire function block will (almost always) be fetched from RAM into the CPU's memory(cache).

- One key difference that makes C++ faster than Java(not always); and Java faster than Python(not always) is in the way they resolve their functions;

- C++ knows exactly where the function is at every call site. and calls them directly (non virtual functions);
- Java looks the function up in a Virtual table (jumps to an offset, to find the exact position of the function);
- Python looks up a dictionary, does some calculations, and jumps to the function's call site

Note: These are traditional ways these languages work. But on good days (most of the time), they are able to make direct calls, hence as fast as C++;

If You are REALLY interested in the ADVANCED stuff, head to http://en.wikipedia.org/wiki/Call_stack ...start from there and dig in deeper.

==============
From a Machine's point of view, a function is basically any well defined cluster of (instruction and data).
What I mean is, A function is an executable region in memory.
==============

Congrats to our dear Nation Nigeria, on her Presidential Elections.

Regards,
Timothy

3 Likes

Re: A Simple Explanation Of Functions And Stacks by Nobody: 2:14pm On Apr 02, 2015
thanks for the write up! wink

it is fully mapped into the layman's domain (abastraction wink), concise and easy to understand, thanks once more.

1 Like

Re: A Simple Explanation Of Functions And Stacks by prinzfunchi(m): 8:16am On Aug 31, 2015
Lovely write up Whiztim .. Learned something new today

Need your advise pls...

we've been taught only Pascal in school but I've picked up C++, I wanna be a software developer , what are the other essential things I need to learn apart from C++?
Re: A Simple Explanation Of Functions And Stacks by WhiZTiM(m): 12:26am On Jun 22, 2017
prinzfunchi:
Lovely write up Whiztim .. Learned something new today

Need your advise pls...

we've been taught only Pascal in school but I've picked up C++, I wanna be a software developer , what are the other essential things I need to learn apart from C++?

This is over two years now... I am sorry for not responding since ....#facepalm
How's the C++ coming? Its two years now, you should prolly be a don by now wink cool
Re: A Simple Explanation Of Functions And Stacks by WhiZTiM(m): 12:26am On Jun 22, 2017
Re: A Simple Explanation Of Functions And Stacks by prinzfunchi(m): 10:46am On Jun 22, 2017
WhiZTiM:


This is over two years now... I am sorry for not responding since ....#facepalm
How's the C++ coming? Its two years now, you should prolly be a don by now wink cool

lol. i moved from C++ to Java and I'm an Android developer now

thanks for replying
Re: A Simple Explanation Of Functions And Stacks by KazukiIto(m): 3:28pm On Jun 22, 2017
Very wonderful. Thanks...
Re: A Simple Explanation Of Functions And Stacks by lomjsvj: 1:13pm On Oct 28, 2019
Simple function and explanation are filed for automatic means and all offers for designated personalities. The room for the success of https://www.topdissertations.org/edubirdie-review/ is improved for all chips in life. The design is divided with the help of the pure and all incentives based terms for members. the struggle is respected and instilled in the advancement of the concepts for all factors of life and society.
Re: A Simple Explanation Of Functions And Stacks by Nobody: 9:52am On Oct 29, 2019
prinzfunchi:
Lovely write up Whiztim .. Learned something new today

Need your advise pls...

we've been taught only Pascal in school but I've picked up C++, I wanna be a software developer , what are the other essential things I need to learn apart from C++?

FPC is also a good language.Why don't u wanna go with it?
Re: A Simple Explanation Of Functions And Stacks by Nobody: 9:54am On Oct 29, 2019
prinzfunchi:


lol. i moved from C++ to Java and I'm an Android developer now

thanks for replying

Cool u.
So what apps or projects have u done in Android?

(1) (Reply)

Latex Program For My Project Work In Mathematics / Cost Of An App Like Instagram / A Python Program To Check The Risk Of Corona Virus.

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 44
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.