PF Lecture 5b
PF Lecture 5b
Fundamentals
Lecture 05a
Calling a Function
• The function gets a copy of the data to use.
• It can change its copy and, of course, change any variables declared within it.
• Next step is to define the function.
Defining a Function
• Each function is defined once (that is, written once) in a program and can then
be used by any other function in the program that declares it suitably.
• Like the main() function, every C++ function consists of two parts, a function
header and a function body, as shown in Figure.
• The function header is always the first line of a function and contains the
function’s returned value type, its name, and the names and data types of its
parameters.
Defining a Function
• It is also called function declarator.
• Because findMax() doesn’t formally return any value and receives two integer
values, the following function header can be used:
• The names in parentheses in the header are called the function’s formal
parameters (or parameters, for short).
• Therefore, the parameter x is used to store the first value passed to findMax(),
and the parameter y is used to store the second value passed at the time of the
function call.
• The function doesn’t know where the values come from when the call is made
from main().
Defining a Function
• The first part of the call procedure the computer performs involves going to the
variables firstnum and secnum and retrieving the stored values.
• These values are then passed to findMax() and stored in the parameters x and
y.
• When you write a function, you’re creating a function definition.
• Each definition begins with a header line that includes a parameter list, if any,
enclosed in parentheses and ends with the closing brace that terminates the
function’s body.
• Parentheses are required whether the function uses any parameters or not.
• The following is a commonly used syntax for a function definition:
• A function prototype declares a function.
• The syntax for a function prototype, which provides the function’s return data
type, the function’s name, and the function’s parameter list, is as follows:
• Generally, all function prototypes are placed at the top of the program, and all
definitions are placed after the main() function.
• However, this placement can be changed.
• The only requirement in C++ is that a function can’t be called before it has been
declared or defined
Components of Function
Findmax function
void findMax(int x, int y)
{ // start of function
body
int maxnum; // variable declaration
if (x >= y) // find the maximum number
maxnum = x;
else
maxnum = y;
cout << "\nThe maximum of the two
numbers is "
<< maxnum << endl;
return;
} // end of function body and end of function
Function definition
• Notice that the parameter declarations are made in the function header, and
the variable declaration is made immediately after the function body’s opening
brace.
• This placement is in keeping with the concept that parameter values are passed
to a function from outside the function, and variables are declared and
assigned values from within the function body.
• The placement of the findMax() function after the main() program is matter of
choice.
• Usually, main() is listed first because it’s the driver function that gives anyone
reading the program an idea of what the complete program is about before
encountering the details of each function.
• However, findmax can be defined before main and prototype is not required
then.
Function definition
• Function body can not be used to define another function.
• Each C++ function is a separate and independent entity with its own
parameters and variables; nesting functions is never permitted.
• Although useful functions having an empty parameter list are extremely
limited, they can occur.
• The function prototype for this type of function requires writing the keyword
void or nothing at all between the parentheses following the function’s name.
• For example, both these prototypes
int display();
int display(void);
• Indicate that the display() function takes no parameters and returns an integer.
Return Statement
• In C++, return is a reserved word.
• When a return statement executes in a function, the function immediately
terminates and the control goes back to the caller.
• Moreover, the function call statement is replaced by the value returned by the
return statement.
• When a return statement executes in the function main, the program
terminates.
• Thus, return statement can sometime be used inside if/else structure to
terminate the program.
• Another statement used for such termination is exit.
Default Arguments
• C++ provides default arguments in a function call for added flexibility.
• The primary use of default arguments is to extend the parameter list of existing
functions without requiring any change in the calling parameter lists already
used in a program.
• Default argument values are listed in the function prototype and transmitted
automatically to the called function when the corresponding arguments are
omitted from the function call.
• For example, the function prototype
void example(int, int = 5, double = 6.78);
provides default values for the last two arguments.
• If any of these arguments are omitted when the function is called, the C++
compiler supplies the default values.
Default Arguments
• Following function calls are valid:
example(7, 2, 9.3) // no defaults used
example(7, 2) // same as example(7, 2, 6.78)
example(7) // same as example(7, 5, 6.78)
Write a function which can add 3 integer numbers and return result to main program.