SEARCH THIS BLOG

FORMS OF FUNCTION IN C++

Functions are no doubt one of the most important parts of any programming language. They help us to reuse a particular logic multiple number of times inside a program and are very easy to modify.

To know basics of functions read the article on Functions in C++.

Here we will be discussing the various forms of functions that are used in C++. Their use depends upon the task we need to perform.

We know functions contain a return type, function’s name and argument list. Out of these, the return type and argument list are optional which give rise to the different forms of functions.


Function forms in C++


Form 1. Function with no return type and no argument

When we do not need to feed any value into a function nor do we want the function to return some value, we use this form. When a function doesn’t return any value, we put its return type as void.

General syntax :

    void function_name ()

    {

            // body of function;

            // no return statement;

     }


Form 2. Function with return value but no argument

When we do not need to pass values to a function but we wish to return some value, we use this form. The return type should match the type of value being returned. If they do not match, the returned value is converted into the return type value which might cause loss in information.

General syntax :

    return_type function_name ()

    {

          // body of the function;

          // return statement;

     }

where return_type can be any C++ datatype (primitive, derived or user-defined) except void.


Form 3. Function with no return type but with argument list

This form of a function can be used when we want to pass certain values into the function for evaluation but we do not want the function to return a value. As the function returns no value, we use void as the return type.

General Syntax :

    void function_name (argument list)

    {

          // body of function;

          // no return statement;

    }


Form 4. Function with return type and argument list

This form is used if we want to pass the values to the function, perform the required operations and then return back the result value.

General Syntax :

    return_type function_name (argument list)

    {

         //body of the function;

         //return statement;

    }

 


For comparison, we will write the same program to find greater of the 2 integers entered by the user, separately using all four forms of a function named max.

 

Functions in C++


Functions in C++


 

  Functions in C++


   Functions in C++





If you find something wrong or if you wish to add some information in this article, feel free to comment on this post or contact us at coding.nkcoder@gmail.com.

Which topics you want an article on? Comment down on this post.

Do not stop here. Read all articles related to C++ here.

If you want to contribute any article for the website, feel free to share it on coding.nkcoder@gmail.com. We will publish it on the website with your name.

 


Post a Comment