Jul 30, 2020

Functions in C++ | C++ Language | Coding Winds

Functions in C++

Hello People, today we are going to study about the main part of our code, We are going to read about Functions in this article. We can use functions when we want our code to perform particular tasks again and again, we can reduce the task of typing a part of code again and again, by making a function of that particular task and calling the function when required.

So, by now you must have got an idea that function is a part of code which executes only when we call them. We can also pass data in a function, known as parameters.

Creating a FUNCTION

To create a function, specifying a function name followed by parentheses’()’. A function declaration also must have a return type and parameter in it.

Return keyword is specified before the name of the function and parameters are specified after the name of function, inside the parentheses,  separated by commas (if there are more than one.)

Parameter is some information which might require for the execution of the function, this parameters are of some data-type. We will study more about this parameter further.

There can be more than one parameter in a single function.

For Example:

Calling a FUNCTION

Declared function does not execute immediately, they are saved for later use, to execute that particular function(when required) we need to call the function in our main code. For executing a particular function we need to call it by the function’s name followed by parentheses

Let us look at the below given example for the better understanding :


We can call a function multiple times with different parameter value everytime :


In C++ it is important to declare a user defined function[ sum() ]

However, it is possible if we the declaration the function before the “ main() “ function and give the definition of the code after the  “ main() “ function, this will increase the readability of the code. For Example :


C++ Function Parameters

Information can be passed to a function in the form of paramters. This parameter is passed to a function in the form of variable. Parameters are specified after the name of function inside the parentheses, separated by commas (if there are more than one).

SYNTAX :

void FunctionName ( parameter1 , parameter2 , parameter3)

{

            ---Body of function---

}

 DEFAULT PARAMETERS

We can set a default for a parameter value in a function. This can be done by using equals sign ( = ).

If we uses a function without an argument, it uses the default value for it.

Look at the below given example for a better understanding :


Return Values

At the time of declaration we specify the return type. If we want a function to return a value we can use the data-type ( e.g., int, string,etc.) of which type we want function to return value. Also, if we don’t want any value in return we can use “void” keyword.

This is all about the basics of functions in C++ hope you gained some knowledge after reading this.

Thanks for giving it a read.

Keep Coding!


No comments:

Post a Comment