What Is Functions In C++ ? || Explained By Examples || Function Types

 

👉WHAT IS FUNCTION👈



By - Dhruv Arne


 

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

 

Return_type function_name(parameter_datatype parameters(if it have))

{

Code

Return something;

}

 

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++ -

 #include <iostream>

using namespace std;

//Function Without Parameter & Return.
void tarrif_func()
{
    cout<<"You Are Very Smart"<<endl;
}

//Function With Pamrameter & Without Return
void naam_sung_tarrif(string name)
{
    cout<<name << " Is The Best Person In The World"<<endl;
}

//Function Without Parameter & With Return
int age()
{
    int birth_year;
    int current_year;
    cout<<"What is your Birth Year?"<<endl;
    cin>>birth_year;
    cout<<"What is the Current Year?"<<endl;
    cin>>current_year
    int user_age = current_year - birth_year;
    return user_age;
}

//Function With Parameter & Return
int age_with_param(int birth_yearint current_year)
{
    int user_age = current_year - birth_year;
    return user_age;
}


int main()
{
    //Calling A Function
    tarrif_func();

    //Calling A Funtion And Passing Argument In It
    naam_sung_tarrif("Dhruv");

    //Calling A Funtion 
    //But it will return something
    // so that printing that 
    //returned value here
    cout<<"Your Age Is " <<age()<<endl;

    //Calling A Funtion & Passing Argument In It
    //But it will return something
    // so that printing that 
    //returned value here
    cout<<"Your Age Is (With Param) " <<age_with_param(2003,2021)<<endl;

    return 0;



}

 

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

Popular posts from this blog

Basics Of Operators||Basics of operators in C++||Basics of operators in cpp||Types of operators in C++||Arithmetic Operators||Comparision Operators||Binary Operators||Logical Operators||Bitwise Operators||Assignment Operators||Miscellaneous Operators||Bitwise Explained||Operators Precedence||

Reference and Pointers in C++