You are on page 1of 24

CHAPTER 6

FUNCTION
MODULAR PROGRAM
• In a modular program, codes are divided into some logical group
called modules.
• Each module will perform a specific task. To enable the module to
perform its task, the module must be called.
• A module is also known as a function. By having functions in our
program, we can easily control the flow of data and control,
especially for a complex software system.
• There are 2 types of functions:

 Predefined function
 User defined function
PREDEFINED FUNCTIONS
• A collection of functions and other program elements obtained
from C++ Standard Library function, accessible through header
files.

• To use these functions in our program, we must include the header


file name using the preprocessor directive #include.

• Examples :

 iostream.h = cin, cout

 conio.h = getch( )

 math.h = pow, sqrt


PREDEFINED FUNCTIONS

FUNCTION PURPOSE EXAMPLE


abs (x) Return the absolute value of its abs(-7) = 7
argument

ceil (x) Return the smallest whole ceil (56.34)


number that is not less than x =57.0
floor (x) Return the largest whole number floor (45.67) =
that is not greater than x 45.00
pow (x,y) Return the power of x depend on pow (2,3) =8
y
sqrt (x) Return the square root of the x sqrt (16) = 4
Examples
#include<iostream.h>
#include<math.h>
main( )
{
int Number, X,Y, Z;

cout<<”Enter one number”; Assume Number = 4


cin>>Number; X=pow (4,2);
4² = 16
X= pow(Number,2); //calculate square Y=pow(4,3);
Y= pow(Number,3); //calculate cube 4³ = 64
Z=sqrt (4);
Z= sqrt(Number); //calculate square
√4 = 2
root
Output:
cout<<”The square of the number is The square of the number is 16
“<<X; The cube of the number is 64
The square root of the number is 2
cout<<”The cube of the number is “<<Y;
cout<<”The square root of the number
is ”<<Z;
}
Let’s Try !
Determine the value of each of the following expressions:

1. abs(12)
2. pow(3,3)
3. pow(4,2)
4. sqrt(49)
5. sqrt(25)
6. floor(36.27)
7. ceil(18.3)
Let’s Try !
Write each of the following as a C++ expression:

 9²

 √81

 -b + √ b² - 4ac
2a
USER DEFINED FUNCTION
● User defined function is the function written by programmer to
perform certain task which is not available in C++ Standard
Library Functions.
● User defines function are classified into 2 categories:
 Value-returning function – function that have a return type.
These function return a value of a specific data type using the
return statement.
 Void function – function that do not have a return type. These
function do not use return statement to return a value.
USER DEFINED FUNCTION
Value-returning function

● Items to be considered:

i. The name of the function


ii. The number of parameter
iii. The type of each parameter
iv. The data type of the value computed by the function, called the
type of the function
v. The code required to accomplish the task

 i until iv - are called the function header


 v - is called the body of the function
USER DEFINED FUNCTION
Function Headern : How to declare function header
function square takes an integer parameter and returns an integer value
int square (int x)
function mult takes 3 parameters of type float and returns a floating point
value
float mult (float a, float b, float c)
function Total takes 2 integer parameter and does not return any value
void Total (int x, int y)
function DisplayValue does not accept any parameter and does not return any
value
void DisplayValue (void)
*Notice that there is no semicolon at the end of function header
USER DEFINED FUNCTION

Syntax: Value-returning function


functionType functionName (formal parameter list)
{
Statement
}

Syntax: Function call


functionName (actual parameter list)

Syntax: return statement


return expr; functionType data type
expr variable, constant value or expression
USER DEFINED FUNCTION
function function
return type name

int larger (int x, int y)


function heading
{
int max; formal parameter list

if(x >= y)
max = x;

else
max = y;

return max;
} function return value
USER DEFINED FUNCTION
• Function definition are divided into two definition:

Formal parameter
A variable declared in the function heading.
Syntax: Formal parameter list
dataType identifier, dataType identifier, ……….

Actual parameter
A variable or expression listed in a call to a function.
Syntax: Actual parameter list
expression or variable, expression or variable, ……….
USER DEFINED FUNCTION
Function Prototype

• The function heading without the body of the function.

• Place function prototype before any function definition


(including the definition of main function)

• Syntax: Function prototype


functionType functionName (parameter list);

*Notice that the function prototype ends with a


semicolon
USER DEFINED FUNCTION
Void Function

• Void function and value returning function have similar


structure.

• Both have heading and body.

• Void function do not have a data type.

• Can use return statement without any value.


USER DEFINED FUNCTION
Syntax: void function
void functionName (formal parameter list)
{ statement }

Syntax: Function call


functionName (actual parameter list);

Syntax: Formal Parameter List


dataType& variable, dataType& variable, …..

Syntax: Function call


expression or variable, expression or variable, …
USER DEFINED FUNCTION
Parameter Types
● Parameter provide a communication link between the calling function and the
called function.

● Two type of formal parameter:

● Value parameter – formal parameter that receives a copy of the content of


the corresponding actual parameter.
● Reference parameter – formal parameter that receives the location
(memory address) of the corresponding actual parameter.

● When you attach & after the dataType in the formal parameter list of a
function, the variable following that dataType become reference parameter.
USER DEFINED FUNCTION
Parameter Types

value parameter reference parameter


Example

void AreaNPerimeter (int length, int width, int& area, int&


perimeter)
{
area=length * width;
perimeter=2 * (length + width);
}
USER DEFINED FUNCTION
Scope of an Identifier

● Scope of an identifier refers to where in the program an


identifier is accessible (visible).

● Recall that an identifier is the name of something in C++,


such as variable or function name.

● Two term involved:


 Local identifier – identifiers declared within a function.
Not accessible outside of the function.
 Global identifier – identifier declared outside of every
function definition.
USER DEFINED FUNCTION
Static and Automatic Variables
 Automatic variable
• A variable for which memory is allocated at block entry and
deallocated at block exit.
• Example are variable declared within a block.

 Static variable
• A variable for which memory remain allocated as long as the
program executes.
• Example are global variable.
• Declare static variable with static reserve word.
• Syntax : static dataType identifier;
USER DEFINED FUNCTION

Function Overloading

• Creating several function with the same name.

• Function that have the same name.

• Also called overloading a function name.

• Examples:
void functionXYZ ( )
void functionXYZ (int x, int y)
void functionXYZ (int one, float y)
void functionXYZ (int x, float y, char name)
Let’s Try !

Mark the following statement as TRUE or FALSE:

 To use a predefined function in a program, you need to know


only the name of the function and how to use it.

 A value returning function returns only one value.

 Parameter allow you to use different values each time the


function is called.
Let’s Try !
Consider the following function definition:

int func (int x, float y, char u, string name)


{
// function body
}

Which of the following are correct function prototypes of the function


func?

i. int func (x, y, u, name);


ii.int func (int s, float k, char ch, string name);
iii.int func (int, float, char, string);
iv.Func (x, y, u, name);
Let’s Try !
Consider the following function prototype:

void funcDefaultParam ( float x=7.3, int


y=4, char z=‘*’ );

Which of the following function call is correct?

i. funcDefaultParam ( );
ii.funcDefaultParam ( 2.8 );
iii.funcDefaultParam ( 3.2, 0, ‘h’ );
iv.funcDefaultParam ( 9.2, ‘*’ );
v. funcDefaultParam ( 7, 3 );

You might also like