Table of contents
1.
Introduction
2.
Function Parameters 
2.1.
Syntax of Parameter Passing
2.2.
Example
2.3.
Output
2.4.
Explanation
3.
Why Use Functions in C++?
4.
User-defined Functions
5.
Types of Parameters
5.1.
Default Parameter
5.1.1.
Example
5.1.2.
Output
5.2.
Multiple Parameters
5.2.1.
Example
5.2.2.
Output
6.
Types of Parameter Passing
6.1.
Pass By Value
6.2.
Pass By Pointer
6.3.
Pass By Reference
7.
Function Call Methods
8.
Frequently asked questions:
8.1.
What is called function in C++?
8.2.
What are main functions in C++?
8.3.
What is the function of this in C++?
9.
Conclusion
Last Updated: Nov 10, 2024
Easy

Function Parameters in C++

Author Rhythm Jain
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In C++, function parameters are used to pass information to functions, making code more flexible and reusable. Understanding how to use these parameters is important for writing efficient programs. 

Function Parameters in C++

In this article we will understand different types of function parameters in C++, such as pass-by-value, pass-by-reference, and default parameters, and how they help improve code quality.

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
}
You can also try this code with Online C++ Compiler
Run Code

Example

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


int main() {
  foo(“ninja”);
  return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

Hello ninja
You can also try this code with Online C++ Compiler
Run Code

Explanation

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

Why Use Functions in C++?

Functions in C++ are essential for making code more modular, reusable, and manageable. They allow you to break down complex tasks into smaller, manageable pieces of code, improving readability and reducing errors. By using functions, you can avoid repeating code, which helps in writing cleaner and more efficient programs. Functions also allow for better debugging and maintenance of your code, as each function can be tested independently.

User-defined Functions

User-defined functions in C++ are functions created by the programmer to perform specific tasks. These functions allow you to define operations that are not part of the standard C++ library. By creating your own functions, you can make your code more organized and reusable. You can pass data to functions using parameters and receive results through return values. User-defined functions also help in reducing redundancy, enhancing program structure, and improving overall code clarity.

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;
}
You can also try this code with Online C++ Compiler
Run Code

Output

1
10
20
You can also try this code with Online C++ Compiler
Run Code

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;
}
You can also try this code with Online C++ Compiler
Run Code

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

Function Call Methods

In C++, there are two primary methods to call a function: call by value and call by reference.

  • Call by Value: In this method, the actual value of the argument is passed to the function. The function works on a copy of the data, meaning any changes made inside the function do not affect the original value. This method is useful when you don't want the function to alter the original data.
     
  • Call by Reference: In this method, instead of passing a copy of the argument, a reference (or memory address) of the original variable is passed to the function. This allows the function to modify the original data directly. It is more efficient when large data needs to be passed or when you want to update the value of the arguments.
     

Both methods have their use cases depending on whether you need to modify the original data or simply work with a copy.

Frequently asked questions:

What is called function in C++?

A function in C++ is a block of code that performs a specific task. It can take input parameters, process them, and return a result. Functions help in reusability and organizing code into smaller, manageable pieces.

What are main functions in C++?

The main function in C++ is the entry point of a program. It is where execution starts. Every C++ program must have a main() function, and it typically returns an integer value to indicate the program's success or failure.

What is the function of this in C++?

In C++, the this keyword refers to the current object instance within a class. It is used to access member variables and functions of the class. It helps to differentiate between member variables and parameters with the same name.

Conclusion

In this article, we learned about function parameters in C++. We saw how parameters help functions receive input, process data, and return results. We also discussed different types of parameters, like value, reference, and pointer parameters, and how they improve code flexibility and reusability.

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