Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Passing parameters

Passing Methods

Values can be passed to a function through two methods.

By default, values are passed to a function through a method called:

  • pass by value

This means that:

  • value of the variable is passed, not the variable itself.

This would be like giving a person a copy of your driver’s license. They can read all of your information, ​and they can change whatever they want on their copy, but the original is not altered in any way.

If desired, a value can be passed through the other method called:

  • pass by reference

This means that:

  • Function is actually given the address of the variable, allowing it direct access to the information.
  • Placing a & after the data type in the function definition allows direct access (this must also be present in any forward declaration).

Let’s look at an example to better understand the concept of passing values by reference.

void fctn(int& arg1, int arg2){ //passing argument 1 by reference using &
arg1 = arg2; //we equate arg1 to arg2
//there is no return as void function
}

int main() {
int num1 = 4; //intializing and decalring num1
cout << "num1 before passing to fctn is: "<<num1<<endl;
fctn(num1,23); //passing num1 and 23 as arguments to fctn function
cout << "Value of num1 (arg1 in function) is: "<<num1<<endl;
return 0;
}

In the code above, we pass the argument arg1 by reference. This means that whatever changes we make to the arg1 value in fctn function will automatically be made to the argument num1 passed in main function.

Default Parameters

It’s possible to assign default values to parameters that are omitted from the function call. The default values are usually defined in the function declaration​ like this example:

int addTwoInts(int arg1 = 4, int arg2 = 5);
//In this case, if the parameters aren’t provided, they will be assigned 4 and 5 respectively.

Scope

Scope refers to the level of access an object has.

A function can access only global variables and those that are passed to it through arguments.

Note: Any variables declared inside a function are only available to that function.

Try the following example in the editor below.

Create a function named “compute” which takes three arguments A, B, C passed by reference and has a return type void.
Update the value of each integer to its respective cube i.e  A = A3, B = B3, C = C3.

Start solving Passing parameters on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test