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 purpose 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 an argument, and these parameters act as variables inside the function.
Basic syntax and working of functions with parameters in C++ programming language
Syntax of function with parameter
void FunctionName(int parameter1, int parameter2) { // code to be written here. }
Explanation
In the above function syntax we have created a function with type void and name of the function as FunctionName(int parameter1, int parameter2) along with parameters parameter1 and parameter2 as parameters of the function. And with in the opening and closing curly brackets we can write the coding part or logic as per our program requirement.
Syntax of function with parameters and arguments
void FunctionName(int parameter1, int parameter2) { // code to be written here. } void main() { int argument1, argument2; FunctionName(argument1, argument2); }
In the second syntax code we have created a function named as FunctionName(int parameter1, int 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.
int argument1, argument2;
FunctionName(argument1, argument2);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 like this,
FunctionName(argument1, argument2);
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(int parameter1, int parameter2);
This above line of code is referred as function declaration. In function declaration we only have function type, function name, and parameters if any.
Now lets talk about function definition. In the function 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
Coding example 1
#include<iostream> #include<conio.h> using namespace std; void add(int num1, int num2) { int sum; sum = num1 + num2; cout << "Sum of entered numbers = " << sum; } void main() { int number1, number2; cout << "Enter first number = "; cin >> number1; cout << "Enter second number = "; cin >> number2; add(number1, number2); _getch(); }
Explanation of coding example 1
In the first programming example we have an add() method or function which has two integer parameters num1 and num2.
void add(int num1, int num2) { int sum; sum = num1 + num2; cout << "Sum of entered numbers = " << sum; }
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 like this,
int number1, number2; cout << "Enter first number = "; cin >> number1; cout << "Enter second number = "; cin >> number2;
and after that we will simply call the add() function and pass these number variables as arguments.
add(number1, number2);
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.
Coding example 2
#include<iostream> #include<conio.h> using namespace std; int add(int num1, int num2) { int sum; sum = num1 + num2; return sum; } void main() { int number1, number2; cout << "Enter first number = "; cin >> number1; cout << "Enter second number = "; cin >> number2; cout << "Sum of entered numbers = " << add(number1, number2); _getch(); }
Explanation of coding example 2
int add(int num1, int num2) { int sum; sum = num1 + num2; return sum; }
And the reason for this function to return an integer value is because we declared this function with int data type.
int add(int num1, int num2)
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.
Coding example 3
#include<iostream> #include<conio.h> using namespace std; int add(int num1, int num2) { int sum; sum = num1 + num2; return sum; } void main() { int number1, number2, result; cout << "Enter first number = "; cin >> number1; cout << "Enter second number = "; cin >> number2; result = add(number1, number2); cout << "Sum of entered numbers = " << result; _getch(); }
Explanation of coding example 3
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.
cout << "Sum of entered numbers = " << result;
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