Parameters are storage places (variables) in the function used to pass data from the caller into the function. Parameters in programming are located between brackets (and) after the function’s identity, and a function can have multiple parameters separated by commas “,.”
Functions usually need data to operate. We typically do to get data to call variables, but we have to keep in mind the rules for local variables in scope. We can not quickly get data from other scopes.
This is one reason why parameters exist in programming languages. If we can’t get data from another scope directly, we can pass that data when we call it with the parameters’ intermediary.
Suppose we have a variable “int an” in the primary function and a custom function with one parameter “int b,” when we pass the value of the variable “int an” into the custom function. We can use parameters “int b” for intermediates and places that will be local variables in the custom function. What happens is the data from “int a” will be copied to the variable “int b.” that way, our custom function will have data from the outer scope (the scope of the call). And also, the data will arrive at its destination safely.
Parameters in programming languages usually have two types, namely:
- Formal Parameter / Parameter Function
- Argument / Actual Parameters
Function Parameters
Function Parameters, also known as Formal Parameters, are local variables established in the function declaration (not definitions), which are the storage places for the values of the arguments that are passed when the function calls.
General Form of Writing
returnType identitas (Function Parameters){
//…definisition…
}
Function parameters are inside the function declaration, between the signs (and) after the function’s identity. Inside the brackets, you can define multiple parameters, each separated by a comma “,.”
Example
int myFunction(int a) { … }
double myFunction(double a, double b) { … }
void functionNoReturn(int a, char b, double c) { … }
Argument
Arguments are parameters that accompany function calls. It is a place where you can provide data for the function called and given when calling the function.
Passing data into custom functions can be in the form of direct data, data from a variable, data from constants, data from function returns, or the result of operations. Passing data into a function must follow the rules set by the function parameter.
Nama (Arguments)
If there is more than one argument, each argument will be separated by a comma. Provision of arguments must match the parameters.
Example:
myFunction(2) ;
myFuntion(a+b, a+4) ;
myFunction(“Arguments”, ‘A’, variabel) ;
Sample Program
#include <iostream>
using namespace std;
void myFunction(int a, int b, int c=20){ // Fuction Parameter atau Parameter Formal
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
}
int main(){
myFunction(1,2,3); //Arguments atau Parameter Actual
return 0;
Concluding Parameters in Programming
This is the tutorial on a parameter in programming, how to create it, and how to apply it to the program. We have discussed the meaning of parameters and function arguments and saw the difference. Lastly, make sure you write your code well. Otherwise, you’ll get a syntax error.