Functions And Parameters In C++ Programming Language
Functions And Parameters In C++ Programming Language
We have studied about functions in our previous blogs, but now we will discuss about parameters and arguments in functions in C++ programming language. We know that functions are reusable blocks of code that executes specific tasks. Now we will look at the use of functions along with the parameters.
The first questions should be that what is a parameter ? In simple words we can define paramteres as placeholder variables inside the function round brackets to accept external data inputs. And the main function of parameters is to hold the data passed to the functions, by functions calling.
Before going deep into functions remember that functions are one of the most important building blocks in C++ programming. So in simple words we can say that information or data can be passed to functions as a parameter and these parameters act as variables inside the function.
Basic syntax of functions with parameters in C++ programming language
In the below function syntax image we have created a function with type void and name of the function as FunctionName(parameter1, parameter2) along with parameters parameter1 and parameter2 as parameters of the function.
In the second syntax code we have created a function named as FunctionName(parameter1, parameter2) along with parameters as discussed in the previous paragraph. Now in the main() method we are calling this function named as FunctionName and we have also passed arguments.
So what are arguments ? Remember when we create a function with parameters, in the function round brackets these variables are called parameters, but when we call a function in the main() method and give some values to the function, these new given values are called arguments.
Basic working of funtions and parameters in C++ programming language
Now we will look at working of functions and parameters in little bit depth. First of all there are two phases for function creation
1) function declaration
2) function definition
during the function declaration phase we simply mention type of function like void or int followed by function name and open and close round brackets and put semi-colon at the end like this
void FunctionName(para1, para2);
This above line of code is referred as function declaration. In function declaration we only have funtion type, function name, and parameters if any.
Now lets talk about function definition. In the funtion definition we write each and every thing along with function coding or logic part of the function. So in simple terms we can also say that function definition is a complete function, with type, name, and coding part or logic.
Simple programming examples of functions and parameters in C++ programming language
In the first programming example we have an add() method or function which has two integer parameters num1 and num2. In this function when we pass the value to the int parameters, it simply add those values and put their result in the sum variable of type int and then show the result of their addition on the console screen.
In the main() method we will take input from the user and put them into number1 and number2 variables and after that we will simply call the add() function and pass these number variables as arguments. The values from number1 and number2 variables will be passed to the function parameters. And from there, values will be passed to num1 and num2 parameters.
In the second coding example we have the same add() method or function and it will again add two numbers, but this time this function will return the addition of two numbers using the return statement i.e,
return sum;
What this return statement will do is, it simply returns the added value in the sum variable to the function calling area in the main method like this add(number1, number2);. So the returned value will be passed to the function call.
Now we will look at the third coding example with respect to the functions and parameters. In this piece of code we again have an add() method or function which is performing same functionality as mentioned earlier in the previous examples. The only change in this code is that, the point of function call i.e,
result = add(number1, number2);
In this line of code we are simply assigning the returned value of this add() method or function to the integer variable named as result. And printing the sum of entered numbers using this result variable. In this way we have implemented the three coding examples of functions and parameters.
Congratulations like always we have successfully implemented functions and parameters in C++ programming language.



Comments
Post a Comment