0% found this document useful (0 votes)
9 views20 pages

PF Lecture 5b

The document provides an overview of function definitions in C++, including how to declare and define functions, the role of parameters, and the use of return statements. It also discusses default arguments, their syntax, and rules for implementation, as well as examples demonstrating their usage. Additionally, it emphasizes the importance of function prototypes and the independent nature of functions in C++.

Uploaded by

mashnabsafdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views20 pages

PF Lecture 5b

The document provides an overview of function definitions in C++, including how to declare and define functions, the role of parameters, and the use of return statements. It also discusses default arguments, their syntax, and rules for implementation, as well as examples demonstrating their usage. Additionally, it emphasizes the importance of function prototypes and the independent nature of functions in C++.

Uploaded by

mashnabsafdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Programming

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)

• Four rules must be followed when using default arguments.


• First, default values should be assigned in the function prototype
• Second, if any parameter is given a default value in the function prototype, all
parameters following it must also be supplied with default values.
• Third, if one argument is omitted in the actual function call, all arguments to its
right must also be omitted.
• Fourth, the default value used in the function prototype can be an expression
consisting of both constants and previously declared variables.
Default Arguments
• If this kind of expression is used, it must pass the compiler’s check for validly
declared variables, even though the expression’s actual value is evaluated and
assigned at runtime.
• Default arguments are extremely useful when extending an existing function to
include more features that require additional arguments.
• Adding new arguments to the right of the existing arguments and giving each
new argument a default value permit all existing function calls to remain as
they are.
• C++ provides the capability of using the same function name for more than one
function, referred to as function overloading.
Default Arguments Example
#include <iostream>
using namespace std;
// defining the default arguments
void display(char = '*', int = 3);
int main() {
int count = 5;
cout << "No argument passed: ";
// *, 3 will be parameters
display(); void display(char c, int count)
cout << "First argument passed: "; {
for(int i = 1; i <= count; ++i)
// #, 3 will be parameters
{
display('#'); cout << c;
cout << "Both arguments passed: "; }
// $, 5 will be parameters cout << endl;
}
display('$', count);
return 0;
}
Default Arguments Example 2
#include <iostream>
using namespace std;
int main() {
int count = 5;
// defining the default arguments cout << "No argument passed: ";
void display(char c = '*', int count = 3) { // *, 3 will be parameters
for(int i = 1; i <= count; ++i) { display();
cout << "First argument passed: ";
cout << c;
// #, 3 will be parameters
} display('#');
cout << endl; cout << "Both argument passed: ";
} // $, 5 will be parameters
display('$', count);
return 0;
}
Default Arguments Example 2
If we are defining the default arguments in the function definition instead of the function prototype, then the
function must be defined before the function call.
// Invalid code

int main() { Either define prototype of


// function call display function or define
display(); this function before main().
}

void display(char c = '*', int count = 5) {


// code
}
Practice

Write a function which can add 3 integer numbers and return result to main program.

You might also like