C++ function parameters and arguments

What are functions parameters and arguments? , how are they related? and what is the difference between them? and why defining a function without parameter name is valid? you will find the answers to these questions here.

While using functions we may want to pass some values to it because the function may require those values to execute it’s code,for instance if there is a function that can perform addition then the function will require some values to perform the addition.In such case the programmer has to pass some values,such values which is passed when the function is called is known as arguments.And the arguments when received by the function is known as parameters.

Parameter and arguments are the same element,they are given different names to simplify the identification of the error liable to be produced when values are passed to or received by the function.Say error produced due to the passing of unmatched data type.But the terms parameter and argument cannot be used interchangeably in C++.The program below shows the parameter and argument while calling a function.

#include <iostream>

using namespace std ;

void Circumference(float r) //’r’ is a parameter
{
cout << “Circumference of a circle with radius ” << r << ” is ” << 3.2425*r*r ;
}

int main()
{
float R=4.5 ;
Circumference(R) ; //R is an argument

cin.get() ;
return 0 ;
} 

A simple pictorial representation is given below to differentiate parameter and arguments.

So you see arguments are parameters when the function receives the values and and parameters are arguments in the function calling side.


why is declaring or defining a function without the parameters names acceptable?

When we declare a function mentioning only the parameter type without it’s name the compiler will accept it.For instance look at the code below.

void func(float , float , float ) ;   //Works fine 

And also when a function is defined mentioning only the parameter type without the identifier(name) it is also accepted by the compiler.Consider the function definition given below.

#include <iostream>

using namespace std ;

void func( float , float , float);   //work fine
{

}

int main( )
{
func(1.77 , 0.69 , 7.367 ) ;

cin.get( ) ;
return 0 ;
}

When a function call is made the Compiler scans only the type of the arguments passed to the function and accordingly figures out which function to call;if the function have the same name the compiler will differentiate them base on their parameters type.So the above function declaration or definition is valid.

However,defining a function without it’s parameters name holds a disadvantage for us.In the above function we could not make any use of the values passed to the function since we cannot access the values without it’s parameters names.Hence,it is always better to give an argument name while defining a function.




Leave a Reply

Your email address will not be published. Required fields are marked *