Professional Documents
Culture Documents
ER
PT N
S
H A IO
T
C UN C
F
OVERVIEW
Introduction to function
Types of variable and its scope - block, local & global.
Predefined functions
Independent Functions
Functions data type
Functions with parameter
Value
Reference
INTRODUCTION TO FUNCTION
A complex problem is often easier to solve by dividing it into several
smaller parts each of which can be solved by itself.
A function could be invoked (called) many times through its name (to
execute its task), by the main function or other functions
When a function is invoked, the computer will execute all the instructions
in the function from start to end, and then it goes to the instruction after the
function call
ADVANTAGES OF FUNCTIONS
Separate the concept (what is done) from the implementation (how it is
done)
Can be called several times in the same program, allowing the code to be
reused.
INTRODUCING FUNCTION
Function definition is a description of subprogram that contains :
Function header : defines the return type, function name and list of
parameters. It similar to function prototype, except that the header does not
end with semicolon.
Function body : contains declaration of local variables and executable
statements.
A function is written once in the program and can be used by any other
function in the program.
The function definition is placed either before the main() or after the main()
INTRODUCING FUNCTIONS
A function is a collection of statements that are grouped together to perform
an operation.
Define a function Invoke a function
void method1() {
.
.
for (int i = 1; i < 10; i++)
{
The scope of i .
int j;
.
The scope of j .
.
}
}
SCOPE OF VARIABLES
for (int i = 1; i < 10; i++) for (int i = 1; i < 10; i++)
{ {
x += i; sum += i;
} }
To use the predefined function, the appropriate library file must be included
in the preprocessor directive (# include)
Name Description
cin Will pause the program to allow the
user to enter the data.
cout Output to the computer screen
iomanip.h
Name Description
setw(n) Set the field width to n
setprecision(n) Set the decimal point precision to n
places
setioflags(ios::fixed) Display the number in conventional
fixed-point decimal notation
setiosflags(ios::showpoint) Display a decimal point of that number
PREDEFINED FUNCTIONS
string.h
Name Description
strcpy(a,b) Copies b string into a string
strcmp(a,b) Compares a string with b string
strlen(a) Calculate the length of a string
strcat(a,b) Appends a string to b string
PREDEFINED FUNCTIONS
char.h/ctype.h
Name Description
toupper(c) Convert character c from lowercase to uppercase
tolower(c) Convert character c from uppercase to lowercase
isupper(c) Return true if c is an uppercase
islower(c) Return true if c is a lowercase
isdigit(c) Return true if c is a digit
isalpha(c) Return true if c is an alphanumeric
isspace(c) Return true if c is a space character
PREDEFINED FUNCTIONS
#include<iostream,h>
#include<char.h>
Input is K
void main(){
Enter one character :
char letter; K
cout<<“Enter one character : ”<<endl;
cin>>letter;
character is uppercase
if(isupper(letter)){ character K in lowercase is k
cout<<“character is uppercase”<<endl;
cout<<“character ”<<letter<<“ in
lowercase is ”<<tolower(letter); Input is k
}
else Enter one character :
cout<<“not uppercase letter”; k
}
not uppercase letter
PREDEFINED FUNCTIONS
math.h
Name Description
pow(x,y) Return x raised to the power of y
sqrt(x) Square root of x
stdlib.h
Name Description
abs(i) Convert to the absolute value of i
rand() Generate a random positive integer between
0 and RAND_MAX(32767)
srand(seed) Initialize random number generator where
seed represent the starting point for th rand
function
PREDEFINED FUNCTIONS
#include<iostream.h>
#include<math.h>
#include<stdlib.h>
void main()
{
cout<<“2 power of 3 = “<<pow(2,3);
cout<<endl;
cout<<“square root of 9 is =”<<sqrt(9)<<endl;
cout<<“absolute number of 3.5 is ”<<abs(3.5);
}
Output
2 power of 3 = 8
square root of 9 is 3
absolute number of 3.5 is 3
PREDEFINED FUNCTIONS
#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
void main()
{ unsigned seed;
float randValue;
cout<<“Enter the value of seed”;
cin>>seed;
srand(seed);
cout<<“\n 5 random generated numbers are : \n”;
for (int i; i<=5; i++)
{
randValue = rand();
cout<<set(8)<<randValue<<endl;
}
}
INDEPENDENT FUNCTIONS
functions written by the programmer
the way the functions are used (called) is the same as library functions
to use a function, only the prototype and a description on what the
function does is sufficient
3 requirements to use the independent function :-
a) Function prototype declaration
b) Function call
c) Function definition
PROGRAM STRUCTURE
Basically, the overall program when using independent function are shown as
below:
#include<iostream.h>
void main(){
variable declaration;
statements / function calls; 2
}
Function definition(){ 3
statements;
}
FUNCTION PROTOTYPE DECLARATION
Function prototype declaration is a function declaration before used in the
program.
It allows the compiler to check for the existence of the function parameter
list and the return type is correct
It placed before the main program.
Syntax:-
Return_type Name_of_function (type_of_parameters);
1 2 3
FUNCTION PROTOTYPE DECLARATION
1 Return type
Indicates the type of value that the function will return to the caller
The return value may be: int, double, float, char, void
void is used when the function does not return any value to the caller
2 Name Of Function
Function name must follow the rules in naming identifier.
3 Parameter Type
Name of function must similar with Number of parameter must be the same with the
the number of paramater in prototype
name of function in function prototype
Example of function calls : findMin(num1, num2);
When making a function call, there are two possibilities that must be
considered:
Function call without return value
Function call with return value
FUNCTION CALL
Function call without return value : does not return any value to the
calling function
Function Call
calculation (number);
FUNCTION CALL
Function call with return value
This type of function will return a value to the calling function (function
call).
After completing the last statement in function definition, the control
program is passed back to the calling function with a value to calling
function. This value will be used in the calling function.
There are four ways to call a function with return value:-
1) In an arithmetic expression
2) In a logical expression
3) In an assignment statement
4) In an output statement
PARAMETERS
Two types of parameters :
Format parameter – define at the function header
Actual parameter – define at function call that consists of parameter, variable
or expression.
RULES FOR PARAMETER PASSING
The type of actual must be the same with the formal parameter.
The number of actual parameter must be the same with the number of
formal parameter.
TYPES OF INDEPENDENT FUNCTIONS
1. Function with no return value and no parameter
#include<iostream.h>
main
void calcAreaCircle();
calcAreaCircle
void calcAreaCircle()
{ double radius, area;
7 153.96
cin >> radius;
area = radius * radius * 3.142; radius area
cout << “The area of circle is “<<
area;
}
TYPES OF INDEPENDENT FUNCTIONS
2. Function with no return value and has parameter
Function Prototype Function Call Function Definition
Input x: 6, y:8
#include<iostream.h>
main
void average(int, int);
void main(){ 6 8
int x, y; x y
cin >> x >> y;
average(x, y);
} average
#include<iostream.h> Input x: 2, y : 4
void main(){ 2 4
int x, y;
x y
cin >> x >>y;
cout << “The average of the 2
numbers
average
is “ << average(x, y);
} 2 4 3
a b purata
int average(int a, int b)
{ int purata;
purata = (a + b)/2;
return purata;
}
TYPES OF INDEPENDENT FUNCTIONS
5. Function with no return value and has a reference parameter
reference parameter – used to return values as a return output to
called function.
it is used where more than one value or variable need to be returned
to the called function.
TYPES OF INDEPENDENT FUNCTIONS
void swap(int &, int { int x, y; void swap(int &a, int &b)
&); cin >> x >>y; { int temp;
swap(x, y); temp = a;
} a = b;
b = temp;
}
void calc(int, int, int { int x, y, total; Void calc(int a, int b, int &jum, float &purata)
&, float &); float avg; {
cin >> x >>y; jum = a + b;
calc(x, y, total, purata = jum/2;
avg); }
}
EXAMPLE FUNCTION 5
Input x: 2, y : 4
#include<iostream.h>
main
void calc(int, int, int &, float &);
void main(){ 2 4 6
int x, y, total; x y total
float avg;
3
cin >> x >>y;
average
calc(x, y, total, avg);
} calc