Class 10 Exam  >  Class 10 Notes  >  C++ Programming for Beginners  >  C++ Function Parameters

C++ Function Parameters | C++ Programming for Beginners - Class 10 PDF Download

Parameters and Arguments

  • Information can be passed to functions as a parameter. Parameters act as variables inside the function.
  • Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma:

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

void myFunction(string fname) {

  cout << fname << " Refsnes\n";

}


int main() {

  myFunction("Liam");

  myFunction("Jenny");

  myFunction("Anja");

  return 0;

}


// Liam Refsnes

// Jenny Refsnes

// Anja Refsnes

When a parameter is passed to the function, it is called an argument. So, from the example above: fname is a parameter, while Liam, Jenny and Anja are arguments.

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 ("Norway"):

Example

void myFunction(string country = "Norway") {

  cout << country << "\n";

}


int main() {

  myFunction("Sweden");

  myFunction("India");

  myFunction();

  myFunction("USA");

  return 0;

}


// Sweden

// India

// Norway

// USA

A parameter with a default value, is often known as an "optional parameter". From the example above, country is an optional parameter and "Norway" is the default value.

Multiple Parameters

Inside the function, you can add as many parameters as you want:
Example

void myFunction(string fname, int age) {

  cout << fname << " Refsnes. " << age << " years old. \n";

}


int main() {

  myFunction("Liam", 3);

  myFunction("Jenny", 14);

  myFunction("Anja", 30);

  return 0;

}


// Liam Refsnes. 3 years old.

// Jenny Refsnes. 14 years old.

// Anja Refsnes. 30 years old.

Note that when you are working with multiple parameters, the function call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.

Return Values

The void keyword, used in the previous examples, indicates that the function should not return a value. If you want the function to return a value, you can use a data type (such as int, string, etc.) instead of void, and use the return keyword inside the function:

Example

int myFunction(int x) {

  return 5 + x;

}


int main() {

  cout << myFunction(3);

  return 0;

}


// Outputs 8 (5 + 3)

This example returns the sum of a function with two parameters:

Example

int myFunction(int x, int y) {

  return x + y;

}


int main() {

  cout << myFunction(5, 3);

  return 0;

}


// Outputs 8 (5 + 3)

You can also store the result in a variable:
Example

int myFunction(int x, int y) {

  return x + y;

}


int main() {

  int z = myFunction(5, 3);

  cout << z;

  return 0;

}

// Outputs 8 (5 + 3)

Pass By Reference

In the examples from the previous page, we used normal variables when we passed parameters to a function. You can also pass a reference to the function. This can be useful when you need to change the value of the arguments:

Example

void swapNums(int &x, int &y) {

  int z = x;

  x = y;

  y = z;

}


int main() {

  int firstNum = 10;

  int secondNum = 20;


  cout << "Before swap: " << "\n";

  cout << firstNum << secondNum << "\n";


  // Call the function, which will change the values of firstNum and secondNum

  swapNums(firstNum, secondNum);


  cout << "After swap: " << "\n";

  cout << firstNum << secondNum << "\n";


  return 0;

}

Pass Arrays as Function Parameters

You can also pass arrays to a function:

Example

void myFunction(int myNumbers[5]) {

  for (int i = 0; i < 5; i++) {

    cout << myNumbers[i] << "\n";

  }

}


int main() {

  int myNumbers[5] = {10, 20, 30, 40, 50};

  myFunction(myNumbers);

  return 0;

}

Example Explained

  • The function (myFunction) takes an array as its parameter (int myNumbers[5]), and loops through the array elements with the for loop.
  • When the function is called inside main(), we pass along the myNumbers array, which outputs the array elements.
  • Note that when you call the function, you only need to use the name of the array when passing it as an argument myFunction(myNumbers). However, the full declaration of the array is needed in the function parameter (int myNumbers[5]).
The document C++ Function Parameters | C++ Programming for Beginners - Class 10 is a part of the Class 10 Course C++ Programming for Beginners.
All you need of Class 10 at this link: Class 10
15 videos|20 docs|13 tests

Top Courses for Class 10

15 videos|20 docs|13 tests
Download as PDF
Explore Courses for Class 10 exam

Top Courses for Class 10

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

C++ Function Parameters | C++ Programming for Beginners - Class 10

,

Viva Questions

,

Summary

,

Sample Paper

,

pdf

,

Objective type Questions

,

C++ Function Parameters | C++ Programming for Beginners - Class 10

,

mock tests for examination

,

Extra Questions

,

Important questions

,

ppt

,

Exam

,

video lectures

,

shortcuts and tricks

,

Free

,

study material

,

C++ Function Parameters | C++ Programming for Beginners - Class 10

,

Previous Year Questions with Solutions

,

past year papers

,

MCQs

,

practice quizzes

,

Semester Notes

;