What Is Functions In C++ ? || Explained By Examples || Function Types
👉WHAT IS FUNCTION👈
Function – Function is a block of a code that is executed only when it is called in the code. In C++ if you want to make a function then the syntax is
Here return type is the type of data
which function is going to be returned. It is not mandatory that the function should
have return something always, it is optional. But now you have a question in
your mind that if we don’t return something then what we will write in the place
of return_type when declaring a function?. I will tell you the answer you have
to write void which means nothing. Void is a datatype in c++ that means nothing,
so there is no example of a void data type because void means nothing.
Now here in the syntax, I wrote a word parameter, so what does parameter mean?.
Basically, parameters are a need of that function to be run.
For ex. If you want to make tea then what you need let’s suppose you need tea
masala, milk & sugar. So tea making is a function here and these three
requirements are parameters without these you can’t make tea or you can’t
execute tea making function.
So is it mandatory that every function
needs parameter?
No, as in the case of a return, the parameter is
also optional. Let’s understand it by an example – Let’s suppose you want to
talk or you have to say something then here saying or talking is a function and
what you need to say is nothing. So here in our example, there is no parameter
to execute a talking function. So it completely depends on your function
work whether it needs a parameter or not.
Now let’s take some examples of a function in C++ -
Output –
You Are Very Smart
Dhruv Is The Best Person In The World
What is your Birth Year?
2003
What is the Current Year?
2021
Your Age Is 18
Your Age Is (With Param) 18
Comments
Post a Comment