As you may have noticed, C++ doesn’t have a direct way to pass function arguments as an argument list. To work around this, some developers will wrap their function calls in parentheses to indicate that the function is getting argument(s) as an argument list. This isn’t something that you need to worry about when writing your own functions — the compiler abstracts this information from you — but if you’re writing code for another programmer, it can sometimes be a pain. Fortunately, there are ways to get past this without having to change much of your code! Passing Function Arguments As An Argument List in C++ In the regular execution of a program, most statements don’t take arguments by value. Rather, they take them as parameterized expressions. These are also known as function arguments . The standard library provides several functions that take variable number of parameters and return one value. You can also create your own custom functions by extending the basic_ostream class with new operator() overloads. These functions aren’t called operator() because they all return one value — they just return another variable number of parameters as arguments (or a single parameter). If multiple arguments are required for an operation, you can use the & symbols as wild cards instead of comma-separated values (or values).

Argument in C++

What is an argument in C++?

An argument is a piece of information that a function receives as an input. The argument can be any data type, as long as the data is convertible to that type. Because functions take arguments as an input, you can’t pass argument by reference. You can, however, pass argument by value, which means that the caller doesn’t need to know how the argument gets to the function.

Function Arguments in C++

When a function call includes a variable number of positional or variable number of positional or named arguments, it’s called a function argument list. This argument list is made up of zero or more arguments. Function arguments that aren’t passed by value or reference are referred to as function arguments . The variable that contains the argument list is known as the argument variable .

Also Read : Program Using Swing Components to Add Two Numbers 

Passing Function Arguments by Value

To pass function arguments by value, put the value(s) you want to pass as the argument(s) in the parentheses. You can do this either before the parentheses or after the comma. Here are a couple of things to keep in mind: The value should be surrounded by parentheses to protect the data type from damage. If you put the value expression directly on the line without parentheses, the value could get accidentally deleted. The value(s) should be the same data type as the argument(s) — or a subtype of the data type if the value is a subtype of a certain object. If you need to pass a value of a different data type, use a subtype. This won’t cause a conflict with the rest of your code.

// function definition to swap the values.
void swap(int x, int y) {
   int temp;

   temp = x; /* save the value of x */
   x = y;    /* put y into x */
   y = temp; /* put x into y */
  
   return;
}

Passing Function Arguments by Reference

To pass function arguments by reference, put the address of the function to which you want to give the arguments in the parentheses. Here are a couple of things to keep in mind: You should enclose the address in parentheses to protect it from damage. If you put it on the line by itself, it could get accidentally overwritten. The address should be the same data type as the argument(s). If the address contains spaces, you should use the commas as character literals. If you need to pass a value of a different data type, use a subtype. This won’t cause a conflict with the rest of your code.

#include <stdio.h>

void swapnum(int &i, int &j) {
  int temp = i;
  i = j;
  j = temp;
}

int main(void) {
  int a = 10;
  int b = 20;

  swapnum(a, b);
  printf("A is %d and B is %d\n", a, b);
  return 0;
}

Wrapping Function Calls with parentheses

To pass function arguments as an argument list, you can use parentheses to group the arguments. Wrap the function calls in parentheses to indicate that the function is getting argument(s) as an argument list. You can use the parentheses to indicate the order in which the arguments should be passed. You can also use parentheses to group values that are explicitly intended to be passed as parameters. One way to do this is to enclose the values in square brackets, like this: [[foo]][bar] . This is known as the parameterization approach. The parameterization approach is the most common way to pass function arguments.

Leave Your Comment