User defined Functions
User defined Functions are used to perform a small, well-defined task with a given input and produce an (optional) output. Functions have two parts, the function header (consisting of return type, the function name, and one or more input parameters) and a function body (that specifies exactly what a function is doing). Functions are also called methods, procedures or subroutines.
Defining Functions:
The format for a function definition is as follows:
returnType fName(parameter list)
{
// function body (specific code)
return <expression>;
}
where
a. returnType is one of the basic types (e.g., int, double) or the special type void which indicates that no value is returned by this function.
b. inputList is a comma-separated list of zero or more declared variables whose values must be provided to the function when it is called.
c. If a function’s return type is not void, the keyword return must be present and return the type specified in the function header.
d. To call a function that returns void, simply type its name followed by an input list of expressions in parenthesis.
Example function:
int multiply(int x, int y, int z)
{
return x * y * z;
}
Functions should be reasonably short and be well-described by their name. Precede each function with a comment block describing it and to use it.
Calling Functions:
You call a function by specifying its name followed by a list of values to initialize its input values.
a. If you call a function that does not return void, you can assign the returned value to a variable or other expression using the = operator.
b. When calling a function you must provide as many input values as are required by the function header, and they must be of the same (or compatible) type.
c. When calling a function with no input parameters, use “empty” parenthesis.
Example function call:
int result = multiply(1, 3, 5);
Reference Parameters:
Input parameters (variables) to a function can be either value parameters or reference parameters
a. Value parameters (the default) receive a copy of the value of the variable used to call the function. The (value) parameter inside the function is unrelated to the variable linked during the function call. Changes to the value parameter inside the function have no effect outside the function.
b. Reference parameters point to the same memory location as the variable used to call the function. The (reference) parameter inside the function is just another name for the variable linked during the function call, they refer to the same data. Whenever a reference parameter changes inside the function, the linked variable outside the function also changes.
c. If you call a function that expects a value parameter, you can pass either a variable (whose value will be copied) of a compatible type or a number or constant (which is then copied into the variable).
d. If you call a function that expects a reference parameter, you can only pass variables (which will be linked) of the same type.
You can recognize reference parameter by the ampersand & symbol between the type and the parameter’s name. Example function with reference parameters:
void add_pi(float &n)
{
float pi = 3.14;
*n += pi;
}
Thanks
Mukesh Rajput
Post A Comment:
0 comments: