Functions Parameters

Parameter and Arguments

A function parameter is a variable declared in the function declaration or definition. It represents a value that the function expects to receive when it is called. Function parameters allow you to pass data to the function so that it can perform its task using that data.

Syntax :

void functionName(parameter1, parameter2, parameter3) {
  // code to be executed
}

The following example has a function that takes a string called fname as parameter. When the function is called, we pass along a first name, which is used inside the function to print the full name:

Example

#include <iostream>
#include <string>
using namespace std;

void myFunction(string fname) {
  cout << fname << " Marcos\n";
}

int main() {
  myFunction("John");
  myFunction("Chris");
  myFunction("Tony");
  return 0;
}

Output:

John Marcos
Chris Marcos
Tony Marcos

Default Parameter Value

You can also use a default parameter value, by using the equals sign (=).

If we call the function without an argument, it uses the default value (“BMW”):

#include <iostream>
#include <string>
using namespace std;

void myFunction(string car = "BMW") {
  cout << car << "\n";
}

int main() {
  myFunction("AUDI");
  myFunction("Mustang");
  myFunction();
  myFunction("Ford");
  return 0;
}

Output:

AUDI
Mustang
BMW
Ford

A parameter with a default value, is often known as an “optional parameter“.

Multiple Parameters

Functions can have multiple parameters. These parameters allow you to pass more than one piece of data to the function for processing. Here’s an example:

Example:

#include <iostream>
#include <string>
using namespace std;

void myFunction(string fname, int age) {
  cout << fname << " Marcos. " << age << " years old. \n";
}

int main() {
  myFunction("John", 31);
  myFunction("Chris", 21);
  myFunction("Tony", 30);
  return 0;
}

Output:

John Marcos. 31 years old. 
Chris Marcos. 21 years old. 
Tony Marcos. 30 years old. 

Return Keyword

The return keyword is used to exit a function and return a value to the caller. It can also be used to return control to the caller without returning a value if the function return type is void.

Example:

#include <iostream>
using namespace std;

int myFunction(int x) {
  return 100 + x;
}

int main() {
  cout << myFunction(50);
  return 0;
}

Output:

150

Pass by reference


Pass by reference in C++ allows you to pass arguments to functions such that the function can modify the original values of the arguments directly, rather than working with copies of the values. This can be useful when you want to avoid the overhead of copying large objects or when you want a function to modify multiple variables.

To pass arguments by reference in C++, you use the reference (&) operator in both the function declaration and the function call. Here’s an example:

Example:

#include <iostream>
using namespace std;

void swapNums(int &x, int &y) {
  int temp = x;
  x = y;
  y = temp;
}

int main() {
  int firstNum = 5;
  int secondNum = 10;

  cout << "Before swap: " << "\n";
  cout << firstNum <<"&"<< secondNum << "\n";

  swapNums(firstNum, secondNum);

  cout << "After swap: " << "\n";
  cout << firstNum <<"&"<< secondNum << "\n";

  return 0;
}

Output:

Before swap: 
5 & 10
After swap: 
10 & 5

Pass Arrays as Function Parameters

You can also pass arrays to a function:

#include <iostream>  
using namespace std;  

void myFunction(int myNumbers[5]) {
  for (int i = 0; i < 5; i++) {  
    cout << myNumbers[i] << "\n";    
  } 
}

int main() {  
  int myNumbers[5] = {1, 2, 3, 4, 5};  
  myFunction(myNumbers);
  return 0;
}  

Output:

1
2
3
4
5

    Leave a Reply

    Your email address will not be published.

    Need Help?