Sei sulla pagina 1di 3

function name in conjunction with the parameter

list would establish the function signature.


Functions • Parameters
A group of statements that collaboratively This portion of a function could be thought
performs a task is called a Function. Every C++ of as a placeholder. When the program calls a
program contains at least one function, which is function, a value is passed on to the parameter.
the main() function. Additional functions could also The value that would be passed on is referred to as
be defined in most of all the trivial programs. the actual parameter or the argument. The type,
A code of a program could be divided into order, and number of parameters in a function are
multiple functions. The way the code would be what the parameter list refers to. A function can
divided among different functions is up to the contain no parameters. Therefore, parameters are
programmer. However, logically, the way it would optional.
usually be divided is that each function would • Function Body
perform a specific task. This portion of a function is where the
There is a difference between a function collection of statements is specified. The collection
declaration and a function definition, where the of statements would then define what the function
declaration of a function informs the compiler the specifically does.
name of the function, the return type, and the
parameter(s); while the definition of the function Example:
provides the actual body of the function. int max(int num1, int num2)
There are numerous built-in functions {
which are provided by the C++ Standard Library int result;
that could be called by the program. if(num1 > num2)
{
2 Examples of built-in functions: result=num1;
• strcat() - This built-in function is used to }
concatenate two strings. else
• memcpy() - This built-in function is used to copy {
one memory location to another memory location. result=num2;
Defining a Function }
General Form return result;
return_type function_name(parameter_list) }
{ Function Declaration
body of the function; A function declaration informs the compiler
} regarding information about the function,
A function definition would consist of a specifically its name and how the program may
function header and a function body. invoke the function.
Parts of a Function General form:
• Return Type
This portion of a function is where the data int max(num1, num2);
type of the value to be returned would be
specified. It is possible for a function to return no In function declaration, the name of the parameter
value and just perform the desired operations. The isn’t as essential, only the data type of the
keyword void would then be specified as the return parameter is.
type if that were the case.
• Function Name Example:
This portion of a function is where the
actual name of the function is specified. The int max(int, int);
NOTE: Function declaration is required when Function Arguments
defining a function in one source file in order for it A function must declare variables which
to be called in another file. The declaration of the would accept the values of the arguments. These
function should then be located at the top of the variables are referred to as the formal parameters
file that is accessing the function. of the function.
Formal parameters are similar to other local
Calling a Function variables in a function. These are created upon
In order for a function to be used, it must entry and would be destroyed upon exit.
first be called or invoked. When a function is called There are different ways argument could be
by a program, the program control would then be passed onto a function when calling a function.
transferred to the called function, which would
then perform its defined task. The program control 3 ways to pass arguments to a function:
would then return back to the main program once
the function executes its return statement or when • Call by value
it reaches its function ending close brace. This method is use to copy the actual value
Example: of an argument into the formal parameter of the
function. Thus, the said changes to the parameter
within function would not bear any effect on the
argument.

• Call by pointer
This method is use to copy the address of
an argument into the formal parameter of the
function. The address will be used in order to
access the actual argument used in the call. Thus,
changes made to the parameter would affect the
argument.

• Call by reference
This method is use to copy the reference of
an argument into the formal parameter of the
function. The reference will be used to access the
actual argument used in the call. Thus, the changes
made to the parameter would affect the argument.

NOTE: C++ uses call by value as the default method


to pass arguments.

Output: Max value is: 200


Output:
The maximum value is: 200
The maximum value is: 100

Default Values for Parameters


A default value for each of the last
parameter could be specified when defining a
function. If the corresponding argument is left
blank when calling to the function, the default
value would be used.
This can be done through the use of the
assignment operator and assigning values for the
arguments within the function definition. The given
default value is used if a value for the given
parameter is not specified. Otherwise, the default
value will be ignored and the specified value will be
used.

Potrebbero piacerti anche