Lab-07 Functions: Contents
Lab-07 Functions: Contents
Lab-07 Functions: Contents
Contents:
Functions Prototype
Function and Parameter Declarations
Calling a Function
Returning a Value.
Tasks
Prerequisites: Before they come to this laboratory session, students should study the syntax
and semantics of function definition, function call and function prototype.
Introduction:
Professional programs are designed, coded, and tested much like hardware: as a set of
modules integrated to perform a completed whole. A good analogy is an automobile; one
major module is the engine, another is the transmission, a third the braking system, a fourth
the body, and so on. All these modules are linked together and placed under the driver’s
control, which can be compared to a main () program module. The whole now operates as a
complete unit, able to do useful work, such as driving to the store. During the assembly
process, each module is constructed, tested, and found to be free of defects (bugs) before
it’s installed in the final product.
In this analogy, each major car component can be compared to a function. For example, the
driver calls on the engine when the gas pedal is pressed. The engine accepts inputs of fuel,
air, and electricity to turn the driver’s request into a useful product—power—and then
sends this output to the transmission for further processing. The transmission receives the
engine’s output and converts it to a form the wheels can use. An additional input to the
transmission is the driver’s selection of gears (drive, reverse, neutral, and so on). The
engine, transmission, and other modules “know” only the universe bounded by their inputs
and outputs. The driver doesn’t need to know the internal operation of the modules being
controlled. All that’s required is knowing what each module does and how to “call” on it
when the module’s output is needed. Communication between modules is restricted to
passing inputs to Each module as it’s called on to perform its task, and each module
operates in a fairly independent manner. Programmers use this same modular approach to
create and maintain reliable C++ programs by using functions. As you have seen, each C++
program must contain a main () function. In addition to this required function, C++
programs can also contain any number of other functions. In this session, you learn how to
write these functions, pass data to them, process the past data, and return a result.
Function Prototype:
A function is a block of code that performs a specific task. C++ allows you to define
functions according to your need. These functions are known as user-defined functions. A
function prototype is simply the declaration of a function that specifies function's
name, parameters and return type. It doesn't contain function body. A function prototype
gives information to the compiler that the function may later be
used in the program.
Syntax:
Return_Type function_Name (type1 argument1, type2 argument2,...);
Lets have a example to add two integers. To perform this task, a user-defined function
addNumbers() is defined.
#include <iostream>
using namespace std;
{
int num1,nu2,sum;
cout<<"Enters two numbers: ";
cin>>num1>>num2;
sum = add_Numbers(num1, num2); // function call
cout<<"sum = "<<sum;
return 0;
}
In the above example, int add_Numbers(int a, int b); is the function prototype which
provides following information to the compiler:
Calling a function
Control of the program is transferred to the user-defined function by calling it.
Syntax
Function_Name(argument1, argument2, ...);
In the above example, function call is made using add_Numbers(num1,num2) by typing
statement inside the main().
Function definition
Function definition contains the block of code to perform a specific task i.e. in this case,
adding two numbers and returning it.
Syntax
returnType function_Name(type1 argument1, type2 argument2, ...)
{
//body of the function
}
When a function is called, the control of the program is transferred to the function
definition. And, the compiler starts executing the codes inside the body of a function.
The parameters a and b accepts the passed arguments in the function definition. These
arguments are called formal parameters of the function. The type of arguments passed to a
function and the formal parameters must match, otherwise the compiler throws error.
If num1 is of char type, a also should be of char type. If num2 is of float type, variable b also
float type.
Placement of Statements: -
C++ doesn’t impose a rigid statement-ordering structure on programmers. The general rule for
placing statements in a C++ program is simply that all preprocessor directives, symbolic
constants, variables, and functions must be declared or defined before they can be used. As
noted previously, although this rule permits placing both preprocessor directives and
declaration statements throughout a program, doing so results in poor program structure. As a
matter of good programming form, the following statement ordering should form the basic
structure around which all C++ programs are constructed:
Return Statement
The return statement terminates the execution of a function and returns a value to the calling
function. In the above example, the value of variable result is returned to the variable sum in
the main () function.
The program control is transferred to the calling function after return statement. For example,
return a;
return (a+b);
The type of value returned from the function and the return type specified in function
prototype and function definition must match.
Pass by Value
The call/pass by value method of passing arguments to a function copies the value of an
argument into the formal parameter. Inside the function, the local variable of the function
which holds the passed value is manipulated to obtain the result. The changes made to the
parameter do not affect the passed argument.
Pass by Reference
The call/pass by reference method of passing arguments to a function copies the address of an
argument into the formal parameter. Inside the function, the address is used to access the
actual argument used in the call. It means the changes made to the parameter affect the
passed argument
Key points: