What is a variable that receives an argument?

Learn about the difference between arguments and parameters in C.

Difference between Arguments and Parameters in C

Overview

Arguments and parameters are often used in different programming languages. But there exists a difference between them. Arguments are some actual value(s) supplied during the function call. Parameters are like the placeholders to which the argument's values are passed, or you can say they are used to receive the arguments passed during a function call. The term argument is used excessively during the function call, and it acts as a medium to pass the value from one function to other.

Scope

  • This article eradicates the uncertainty between arguments and parameters. It focuses mainly on the structure, syntax, and examples of arguments vs parameters.
  • It also describes the fundamentals of arguments vs parameters and gives a clear picture at the end of the article.
  • The concept of argument vs parameters is similar to all the other programming languages, but syntax may vary. Here in this article, the examples and syntax are in C language.

Introduction

Have you wondered what exactly a parameter is and what argument is while writing code?

Let us discuss an example of a simple addition function throughout this article. It will be helpful to illustrate the concept of argument vs. parameter. This user-defined function takes two inputs a and b, and produces the output c.

Note: It is always a good practice to declare the function prototype globally before the main function

What is Argument in C?

Arguments in C are the variables that are used to pass certain values. We use functions to reduce code complexity and improve readability in the C program. We create some user-defined functions which allow us to reuse the code.

When we create a function, we can pass the data in the form of an argument to the calling function. The passing data can be an integer value, floating-point value, etc. Arrays or strings can also be passed as a data to other functions.

What is a variable that receives an argument?

Example Arguments

void main() { int first_number,second_number; first_number = 4; second_number = 6; int sum = add(first_number,second_number); //passing arguments printf("Sum of %d and %d is %d" ,first_number, second_number,sum); }

The above code is an example of argument passing in C. There are mainly two kinds of arguments.

  1. Argument with return value
  2. Argument without return value

This is an example of the first kind of argument, an Argument with the return value.

  • There is a main() function. We declare two variables first_number and second_number inside the main().
  • We also initialized with 4 and 6 values, respectively.
  • We declare one more variable sum to store the sum of two numbers. Assume that we have a user-defined function add(), which takes two numbers and produces the sum as its output.
  • We call the function add() and assign it to the variable sum.

Now the program's control is given to the first line of add(). After executing the statements of add(), the value is stored in the variable sum.

The vaiable sum holds the result and gets printed in the main().

int sum = add(first_number,second_number);

In the above line, we call the function add() by passing two values, i.e., 4 and 6. Here the variables firstnumber and secondnumber act as the arguments. They pass the value to the called function through them.

What is a variable that receives an argument?

The above image represents the "Argument with return value".

  • There is a main() that passes values to function1() when called.
  • function1() performs the operations, produces the required result, and sends it back to main().
  • Then, further operations like printing the result can be done inside the main().

An example for Argument Without Return Value

void main() { int a,b; a = 4; b = 6; add(a,b); } void add(int x,int y) { int sum = x + y; printf("%d",sum); }

Here the main function calls the add() by passing the arguments. The result is printed in the add() function rather than returning it.

What is a variable that receives an argument?

What is Parameter

The parameter in C refers to any declaration of variables within the parenthesis during the function declaration. These are listed in the function's definition, separated by commas.

What is a variable that receives an argument?

Example of Parameter

int add (int a, int b) { int c = a + b; return c; }

In the above lines of code, we can see the function definition. The actual addition function is performed here in this add() function. In this case, add() receives the value from the main function and performs the addition operation using the "+" operator. The new variable c is declared to store the value of the result. It has int as a return type, hence it returns integer c to the main() function.

Difference Between Argument and Parameter in C

These are the some concluded difference points on arguments vs parameters from the above discussion.

ArgumentParameter
Also known as actual parameters Also known as formal parameters
Arguments are used while calling the function Parameters are used during the declaration of the function
Argument is the actual value of this variable that gets passed to function. Parameter is variable in the declaration of the function.

Example This is the combined example of argument vs parameter. These first_number and second_number are the arguments, whereas the a and b are parameters.

#include int add(int first_number,int second_number); //function prototype void main() { int first_number=4; int second_number=6; int sum = add(first_number,second_number);//passing arguements printf("Sum of %d and %d is %d" ,first_number, second_number,sum); } int add(int a, int b) { int c = a+b; return c; }

Conclusion

  • We saw the difference between argument and parameter is that parameter is a variable defined during function declaration while arguments are the actual values.
  • While passing the arguments from one function to another, one must ensure that the number of arguments passed must be equal to that of parameters received.
  • If the number of parameters and arguments mismatches in calling and called function error may arise.
  • Meaning for Parameter and Argument may change according to different programming languages.
  • The one-liner takeaway from this discussion of argument vs parameter is that arguments are passed in the function call, and parameters are written inside the function declaration.

What is an argument variable?

Variable Arguments (Varargs) in Java is a method that takes a variable number of arguments. Variable Arguments in Java simplifies the creation of methods that need to take a variable number of arguments.

What is the variable that is used by function to receive an argument?

A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions.

Is a parameter an argument?

A parameter is a variable in a function definition. It is a placeholder and hence does not have a concrete value. An argument is a value passed during function invocation.

What is variable number of argument?

To call a function with a variable number of arguments, simply specify any number of arguments in the function call. An example is the printf function from the C run-time library. The function call must include one argument for each type name declared in the parameter list or the list of argument types.