Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Function Parameters 
2.1.
Syntax of Parameter Passing 
2.2.
Example
2.3.
Output
2.4.
Explanation
3.
Types of Parameters
3.1.
Default Parameter
3.1.1.
Example
3.1.2.
Output
3.2.
Multiple Parameters
3.2.1.
Example
3.2.2.
Output
4.
Types of Parameter Passing
4.1.
Pass By Value
4.2.
Pass By Pointer
4.3.
Pass By Reference
5.
Frequently asked questions:
6.
Key Takeaways
Last Updated: Mar 27, 2024

Function Parameters in C++

Author Rhythm Jain
1 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

A function is a collection of statements that work together to complete a task. The name, return type, and function parameters are all specified in a function declaration. A function definition defines the body of the function.

Also See,Fibonacci Series in C++

Function Parameters 

A parameter is similar to a placeholder. When you call a function, you send a value to the argument. This value is known as the actual parameter or argument. As a parameter, information can be supplied to functions.

Parameters are optional, which means that a function may or may not have parameters.

Parameters are provided inside the parentheses following the function name. We can enter as many parameters as you wish, separated by a comma.

We also need to specify the parameter’s data type while defining the function itself; otherwise, it could provide confusing results.

Syntax of Parameter Passing
 

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

Example

void foo(string name) {
  cout <<”Hello ”<<name<<”\n";
}


int main() {
  foo(“ninja”);
  return 0;
}

Output

Hello ninja

Explanation

In the above example, the “name” is the parameter of the function “foo,” and “ninja” is an argument.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Types of Parameters

Default Parameter

A default parameter is an argument to a function that is not needed to be specified by the programmer. If we call the function without an argument, the default value, also known as the optional value, is used.

The default parameter is assigned using an equals sign (=).

Example

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


void foo(int num = 10) {
    cout << num << "\n";
}


int main() {
    foo(1);
    foo();
    foo(20);
    return 0;
}

Output

1
10
20

You can try and compile with the help of online c++ compiler.

Multiple Parameters

We can add as many parameters as we want in the function declaration separated by a comma. When working with multiple variables, the function call must have the same number of arguments as parameters, and the arguments must be supplied in the same sequence.

Example

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


void foo(string first_name,string last_name, int age) {
  cout << first_name <<" "<< last_name << " is "<<age << " years old. \n";
}


int main() {
  foo("Tom", "Holland",25);
  return 0;
}

Output

Tom Holland is 25 years old. 

Types of Parameter Passing

Pass By Value

Here the actual value of an argument is copied into the function parameter. Inside the function, changes to the parameter do not affect the argument.

Pass By Pointer

Here, the address of an argument is copied into the formal parameter. The address is used within the function to access the actual parameter used in the call. This signifies that changes to the parameter have an effect on the argument.

Pass By Reference

Here the reference of an argument is copied into the formal parameter. The reference is used within the function to access the actual parameter used in the call. This signifies that changes to the parameter affect the argument.

Before the argument, we use an “&” symbol to pass by reference.

Check out this article - Balanced Parentheses

Must Read Lower Bound in C++, Difference between argument and parameter

Frequently asked questions:

  1. What is the difference between parameter and argument?
    A parameter is a variable in a function definition. The data you pass into the function parameters are called arguments when a function is called.
     
  2. How can we assign a default parameter?
    We can assign a default parameter using equals sign (=). For example 
    void foo( int mynum=5){
        //code
    }
     
  3. How can we ensure that changes made to parameter also affect the argument in a function?
    We can use the pass by reference method to make sure that changes made to parameter also affects the argument in a function.

Key Takeaways

Today we learned about types of function parameters in C++. We also came upon different parameter passing techniques. We observe various examples to make a clear understanding.
That’s all for today, folks. If you wish to learn from more curated articles, look at our library.

Recommended Readings: 

Keep Coding!

Live masterclass