My guess is that you're looking for a simpler explanation than the one you just read on Quora.
Here it is for you.
In mathematics, a function f(X)
is a sequential set of transformations on X
which gives you a completely different output (usually called Y
)
A function is often mathematically described.
For example, let there be a function f(X) = (8*X + 5)
So for every input, there's gonna be a output.
Like, f(5)
will be 45
on computation.
In terms of programming,
This 45
is the returned value (Y
)
Whenever you are trying to use a function, you're calling that function for a specific input.
I could get into the details of why a program slows down when you call multiple functions but that was explained in the link you provided. So to keep things simple, imagine if I ask you to calculate something simple once. You'll do it right away. But if I ask you to do a thousand simple calculations, though you'll do it, it'll still take you quite a bit of time. It's not exactly the same with computers, but it's somewhere along the lines.
There are some functions which don't actually return anything at all. In Java they're referred to as void
functions.
To understand this, let's take another example.
Let f()
be your function, and let it print out 5 lines to your console.
Now whenever you try to call a void function, you can think of it like you've replaced those 5 print statements by one function call.
Say it takes an input now f(X)
and it prints out X+18
Since it doesn't return anything, you can't assign f(X)
to a variable. But it does print things, so whenever you call it in your code, it basically just executes the statement that prints out (X+18)
to your console.
In case this isn't clear enough, say you define a void function g(X)
to a block of code.
Now whenever you call it, you can think about replacing g(X)
with that block of code.
Hopefully this helps
EDIT:
It seems like you've edited your question, so here's an edited answer for you.
Now, a fair warning - the explanation I'm about to give you is not a technical one. My teacher and one of my colleagues used this explanation for me when I was questioning them about the stack and functions for the first time so it's gonna be in layman terms.
Imagine a huge container (or a box), and let's call it A
The container/box is open from the top.
Any item you put inside the box, goes down to the bottom due to gravity.
Suppose you put an item B
inside of the container A
. Then you add C
and then D
Since the box is only open from the top, the last item that you put in is the only one you can access first. And only when you take out this item, can you go on to access the item below it. So you'll first have access to D
, when you take out D
, you'll have access to C
, and then finally when you take out C
you'll have access to the lowest item B
The container A
is basically a stack.
The you can think of the container as a box of memory (not physically but figuratively) it's basically just free memory space.
Whenever you invoke the main function, it gets pushed in the box or as we call it now, stack.
The main( )
function is at the bottom of the stack now. You can relate this to the item B
from our above example.
Now suppose you have a function f(X)
And within your main( )
function you're calling f(5)
Calling f(5)
creates a new scope for the function where the value of the passed in variable (X
) is now set to 5
This entire thing - the function definition, any other variables you've created within the function, and the value of the passed in variable - is called a "stack frame". The stack frame will take up some space in our stack/container. This stack frame also corresponds to the next item in the container, and gets "stacked" over the main method. You can think of it as C
from the previous example.
Now comes in the part I said about removing items from the container/stack. Removing corresponds to computing and returning the value. Considering the presence of void functions, a better term would be that removing refers to execution of a function. The first item that needs to be removed is C
or f(5)
and then B
or main( )
Now coming to a simple answer to why things slow down when you're calling a lot of functions. There's one simple answer. Your container doesn't have an infinite capacity.
If you keep filling up container A
with items B, C
, the items will obviously occupy space. Eventually if you go on adding items D, E, F, G, H...
there'll be a point when your container is full, after that if you try to put in more items, your container will "overflow"
Basically when you call a lot of functions, your container/stack keeps filling up, eventually leading to a stack overflow.
So calling a function creates a scope of its own, takes all the information related to the function (definition, passed variables, other variables etc), puts it in a stack frame and then stacks the stack frame.
Hopefully it's all clear now... And since this question has been put on hold, if you still have doubts feel free to leave another comment and tag me in it or contact me somehow :)
PS: I've used a lot of layman language here to explain this, so use this as a general way to visualise what's going on, and refer to technical definitions if you're studying this for an exam
EDIT I've edited it to be an example with just one single function f(5)
so that it's easier to understand what I'm saying. The earlier one was slightly incorrect as well