answersLogoWhite

0


Best Answer

Parameters are the variables you pass to functions. Return values are the variables that are returned by those functions. Values can also be returned by the parameters themselves (such parameters are said to be output parameters).

The variables you pass and return from functions will either be by value or by reference. Only references can be output parameters, since passing by value creates an automatic copy of the value which falls from scope when the function returns. Pointer variables are always passed by value. To pass a reference to a pointer you must pass a pointer to the pointer, which is itself passed by value, but refers to the pointer.

Constant references are the preferred method of passing variables to functions, since there is no need to copy the variable you pass and because it is constant, the original value is left unaltered. Non-constant reference is the next best method, but only if you expect changes to be reflected back in your value (an output parameter). If you do not expect changes, then you must pass by value or, when that isn't an option, create a copy of your value and pass by non-constant reference. The only difference is that passing by value automatically destroys the copy when the function returns.

Ignoring output parameters, a function can only return, at most, one value. A function that returns void has no return value, however it is good practice to always return something, even if only an integer to indicate whether the function was successful (zero) or not (non-zero). If the function is a logical function, such as Add( X, Y ), or an accessor, such as GetName() then it makes more sense to return the actual result of the function via the return value. If you need more than one return value, then you must use output parameters, passed by reference, which allows the return value to be used as an error indicator.

Generally you will want to store the return value of a function, especially if the return value is a memory allocation. If you don't store the return value, the value is lost forever, unless you re-call the function with the same parameters, which is highly inefficient. However, if you only need to know the result of a function once, you don't need to store it, so long as you act upon it immediately, usually as part of a conditional expression. E.g.,

int x, y; // Uninitialised, values unknown.

if( x + y == 12 ) // call the int::operator+ function.

// x+y is definitely 12.

else

// x+y is definitely not 12, but the actual sum is no longer known at this point.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is Parameter Passing and Returning values.. in Programming.. preferbly Visual Basic?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Different parameter passing methods with examples?

explain parameter passing methods c program


Why is parameter passing useful to programmers?

Because all software is written using functions and classes. If you do not know how to pass data from class to class, to function you are not a programmer.


What is a parameter passing?

Passing parameters probably means passing a parameter into a function.Basically, this happens when you call a function. You put the parameter into a function, then call it, and the function does something to the parameters you put into it. Here's an example: #include <iostream> using namespace std; int sum(int a, int b); //Declare the function, so the program knows that it exists int main() { int first, second, total; cout<<"Please insert two numbers."<<endl; cin>>first>>second; total=sum(first, second); /*Here we are passing the variables (parameters) "first" and "second" into the function "sum". This function then does something to them and outputs the value. In this case, it is stored in the variable "total"*/ cout<<"Their sum is "<<total<<"."<<endl; return 0; } //Here we define the function (tell the program what to do) int sum(int a, int b) { //We pass two integers into the function. Their values are stored in a and b. a and b can then be used by the function. sum=a+b; return sum;//Adds them, then returns the sum }


What is an actual parameter?

A formal perimeter refers to an identifier that is used in a method to stand for the value that is passed into the method by a caller. An actual perimeter on the other hand refers to the actual value that is passed into the method by a caller.


What is a variable that is used within a function?

If the variable is declared within the function body, it is a local variable, one that is local to the function. Local variables fall from scope when the function returns, they are only accessible within the function. However, local variables can be returned by value, which creates an automatic variable that is returned to the caller. If the caller does not store the return value, the automatic variable falls from scope when the expression containing the function call ends. However, the expression may evaluate the return value without storing it. Note that functions cannot return local variables by reference since the local variable falls from scope when the function returns. If the variable is passed as an argument to the function, then the variable is a parameter of the function. Arguments may be passed by value or by reference, depending upon the function signature. Passing by value means the function parameter is a copy of the argument (if the argument is an object, the object's copy constructor is invoked automatically). Thus any changes made to the parameter within the function are not reflected in the argument that was originally passed, and the parameter will fall from scope when the function returns. However, the value of the parameter can be returned as previously explained. Passing by reference means the function parameter refers directly to the argument that was passed. Thus any changes made to the parameter are reflected in the argument. Parameters that are declared as constant references assure the caller that the reference's immutable members will not be altered by the function. If the parameter is a non-const reference but the caller does not wish changes to be reflected in the argument, the caller should pass a copy of the argument instead.

Related questions

What does parameter passing in c means?

parameter passing in c, what does it do?


Different parameter passing methods with examples?

explain parameter passing methods c program


What is call by refrence?

When calling a function, passing a variable's address as function parameter.


Definition for call by value in c?

This is the only possible way of parameter-passing in C language.


Which is the default parameter passing technique in c plus plus language?

The default is to pass by value.


Is it possible to use two parameter passing methods?

The number of parameters is zero or more, so two is perfectly okay.


Why is parameter passing useful to programmers?

Because all software is written using functions and classes. If you do not know how to pass data from class to class, to function you are not a programmer.


What is a parameter passing?

Passing parameters probably means passing a parameter into a function.Basically, this happens when you call a function. You put the parameter into a function, then call it, and the function does something to the parameters you put into it. Here's an example: #include <iostream> using namespace std; int sum(int a, int b); //Declare the function, so the program knows that it exists int main() { int first, second, total; cout<<"Please insert two numbers."<<endl; cin>>first>>second; total=sum(first, second); /*Here we are passing the variables (parameters) "first" and "second" into the function "sum". This function then does something to them and outputs the value. In this case, it is stored in the variable "total"*/ cout<<"Their sum is "<<total<<"."<<endl; return 0; } //Here we define the function (tell the program what to do) int sum(int a, int b) { //We pass two integers into the function. Their values are stored in a and b. a and b can then be used by the function. sum=a+b; return sum;//Adds them, then returns the sum }


What is an actual parameter?

A formal perimeter refers to an identifier that is used in a method to stand for the value that is passed into the method by a caller. An actual perimeter on the other hand refers to the actual value that is passed into the method by a caller.


Why printf is known as call by value?

C language uses only one method for parameter-passing: call by value.


What do you call the process of testing flow of control between a main function and its subordinate function?

function-call and returning (actually, it is 'passing', not 'testing')


The ls command doesn't show everything in directory what's wrong?

Nothing. By default, ls will not show configuration files or directories that begin with a . These can be displayed by passing the -a parameter to ls. Ex. ls -a