Title: Special Topics 1 (Oracle)
1Function
User defined function is a code segment (block)
that perform an specific action.
Function DefinitionReturn_DT F_name ( list of
formal parameters) Action_ body
2The Function Definition
- Function are of 4 types
- 1- A function that doesnt return a value without
argument - -Return_DT is void
-Action_body doesnt contain return statement. - Syntax
- void function-name ( )
- local declarations // this is optional
- executable statements
-
3Example function that doesnt return a value
without arguments
- void print_O ( )
- cout ltlt ltlt endl
- cout ltlt ltlt endl
- cout ltlt ltlt endl
- cout ltlt ltlt endl
-
4The Function Definition
- 2- A function that doesnt return a value with
arguments - -Return_DT is void
-Action_body doesnt contain return statement. - Syntax
- void function-name(argument_list)
- local declarations // this is
optional - executable statements
-
5Example function that doesnt return a value
with arguments
- void print_box (int n)
- cout ltlt \n
- cout ltlt ltlt n ltlt \n
- cout ltlt \n
-
6The Function Definition
- 3- A function that returns a result of any type
without arguments - -Return_DT is any type -Action_body
contains return statement. - Syntax
- ltany typegt function-name( )
- local declarations // this is
optional - executable statements
-
7Example function that return a value without
arguments
- int sum ()
- int x5, y7
- return xy
-
8The Function Definition
- 4- A function that returns a result of any type
with arguments - -Return_DT is any type -Action_body
contains return statement. - Syntax
- ltany typegt function-name(argument_list )
- local declarations // this is
optional - executable statements
-
9Example function that return a value with
arguments
- int Rect_area (int L, int W)
- int a
- a L W
- return a
-
10Calling (invoking) Function
- a) The call to a Function that does not return a
value is given in the following syntax - F_name (Actual parameters)
- e.g. draw_circle ( )
- e.g. sum(4,7)
- b) The call to a Function that returns a value is
given as follows - - The name of the functio is given within the
output statement - e.g coutltltsum(x,y)
- - The name of the Function is given within
the - assignment statement
- e.g. result sum (x, y )
11The Call to a function
- program Figure Function
draw_circle
void main() ------- ------- draw_circle
( ) ------ ------ ------
void draw_circle ( ) ------ ------
------ ------
call
return
12Example
include ltiostream.hgt int Max (int Value1, int
Value2) if (Value1 gt Value2) return
Value1 else return Value2 void main()
int a, b coutltlt"\nPlease Enter the values of
a and b " cingtgtagtgtb coutltlt"\n the maximum
one is "ltltMax(a,b)ltltendl
Function Definition Calling the function in
an expression like coutltlt, condition, assignment
statements
13Example
include ltiostream.hgt int Max (int Value1, int
Value2) if (Value1 gt Value2) return
Value1 else return Value2 void main()
int a, b coutltlt"\nPlease Enter the values of
a and b " cingtgtagtgtb coutltlt"\n the maximum
one is "ltltMax(a,b)ltltendl
14Example
include ltiostream.hgt int Sum (int A, int B)
return (AB) void main() int N1, N2,
S coutltlt"\n Please Enter N1 and N2
" cingtgtN1gtgtN2 S Sum(N1,N2) coutltlt"\nSum
"ltltSltltendl
15Example
include ltiostream.hgt bool Positive (int Num)
if (Num gt 0) return true else return
false void main() int Number coutltlt"\nEnte
r Number " cingtgt Number if
(Positive(Number)) coutltlt"\n the number is
positive" else coutltlt"\n the number is
negative" coutltltendl
16Example
include ltiostream.hgt float Area (int R)
return (3.14 R R ) void main() int
Radius coutltlt"Enter the Redius
" cingtgtRadius coutltlt"\nCircle Area is
"ltltArea(Radius) coutltltendl
17Example
include ltiostream.hgt long Power(int Base, int
Exp) int M1 for(int i1 iltExp
i) MBase return M void main() int
B, E coutltlt"\nEnter Base " cingtgtB coutltlt"\n
Enter Exponent " cingtgtE coutltlt"\n Result
"ltltPower(B,E) coutltltendl
18Example
include ltiostream.hgt long Fact (int Num) int
F 1, i Num while (igt1) F i i--
return F void main() int
Number coutltlt"Enter an integer number
" cingtgtNumber coutltltendlltltNumberltlt"!
"ltltFact(Number) coutltltendl
19Void Returned Data Type
include ltiostream.hgt void Print(char Ch, int n)
for (int i1 iltn i) coutltltCh coutltltend
l void main() char Sym int
Number coutltlt"\nEnter the Symbol
" cingtgtSym coutltlt"\nHow many times
" cingtgtNumber Print(Sym,Number)
No Return Statement
20Example
include ltiostream.hgt int Mul(int V1, int V2)
return V1 V2 void Result() coutltlt"\n5
x 9 "ltltMul(5,9) coutltlt"\n4 x 7
"ltltMul(4,7) coutltlt"\n6 x 4 "ltltMul(6,4)ltltendl
void main() Result()
21The Function Prototype
- Like other identifiers in C, function must be
declared before it can be referenced. - To declare a function, we can insert a function
prototype before the main function. - The function prototype provides all information
that the C compiler needs to know to translate
calls to the function correctly. - A function prototype tells the compiler the
- - data type of the function
- - the function name
- - information about the arguments that the
function expects. - Examples void draw_circle ( )
- int m ( )
- void print_box
(int) - int Rect_area (int
, int)
22Function Prototype
include ltiostream.hgt int Mul(int, int) int
Add(int, int) void Show() void main()
Show() int Mul(int X, int Y) return XY
int Add(int X, int Y) return XY void
Show() int A10, B20 coutltltAdd(A,B)ltlt'\t'ltltM
ul(A,B)ltltendl
Function Prototype contains only data types But
may contain identifiers.
23Scope of Variables
- (1) Global variables
- - Those variables that are declared before the
main function. - - These are visible from any point of the
code, inside and outside any function. - (2) Local variables
- - These are declared inside a block or a
function. - - The scope of local variables is limited to
the same - nesting level in which they are declared.
24Example of Local and Global Variables
- // File global.cpp
- include ltiostream.hgt
- int x 7 // global
variables - int fun1 (int ) // function
prototype - void main ( )
- int z // local
variable in main - cout ltlt The global variable ltlt x
- z fun1 ( 5 ) // calling add
function - cout ltlt The result is ltlt z ltlt endl
-
- int fun1 ( int a )
- int r // local
variable in fun1 - r a a a
- return r
25Functions and Passing Parameters
- The mechanisms of passing parameters
- (1) Call by value
- - During the function call, the value of the
argument is - found and passed to the function.
- - Any modification inside the function does
not affect the - argument.
-
- (2) Call by reference
- - During the function call, the address of
the variable is - passed to the function.
- - Any modification made on the parameter
inside the - function will have effect in the passed
variable outside it.
26Difference between Function Definitions for Call
by Value and Call by Reference
- For call by value, we declare the arguments of
the function as usual. - Example
- int func1 ( int , int ) // function
prototype - For call by reference, the type of the argument
is followed by the symbol () to specify that the
variable has to be passed by reference. - Example
- void func2 ( int , int ) //
function prototype
27Call by value
When calling, the value of actual parameter will
be copied to the formal parameter.
include ltiostream.hgt void Increment(int) void
main() int A 10 Increment(A) coutltltAltltend
l void Increment(int X) X
28Call By reference
When calling, the reference formal parameter will
be an alternative name to the actual parameter.
include ltiostream.hgt void Increment(int) void
main() int A 10 Increment(A) coutltltAltltend
l void Increment(int X) X
29Call By reference
When calling, the pointer formal parameter will
points to the actual parameter.
include ltiostream.hgt void Increment(int) void
main() int A 10 Increment(A) coutltltAltlten
dl void Increment(int X) X
30Example 5 Call by Value
- // File calls1.cpp
- include ltiostream.hgt
- // function prototype the arguments to be passed
by value - int add (int , int )
- void main ( )
- int x, y , z // local
variables in main - cout ltlt Enter two integers
- cin gtgt x gtgt y
- cout ltlt x ltlt x ltlt y ltlt y ltlt
endl - z add ( x , y ) // calling add
function - cout ltlt The result is ltlt z
- cout ltltAfter call ltlt x ltlt x ltlt y
ltlt y ltlt endl -
- int add ( int a , int b )
- return a b
31Execution of Example 5
- The user will be prompted to enter two integers.
- The user enters, for example, 10 and 20 that are
saved in x and y respectively. - When function add is called, the value of x and
y are passed to the function. - The function add takes the values 10 and 20 and
links them to a and b respectively. - The function will return the result which is 30.
- The output
- Enter two integers 10 20
- x 10 y 20
- The result is 30
- After call x 10 y 20
32Example 6 Call by Reference
- // File calls2.cpp
- include ltiostream.hgt
- // function prototype the arguments to be passed
by reference - void duplicate (int , int , int )
- void main ( )
- int x, y , z // local
variables in main - cout ltlt Enter three integers
- cin gtgt x gtgt y gtgt z
- cout ltlt Before call ltlt x ltlt x ltlt
y ltlt y - ltlt z ltlt z ltltendl
- duplicate ( x , y , z ) //
calling duplicate function - cout ltlt After call ltlt x ltlt x ltlt
y ltlt y - ltlt z ltlt z ltltendl
-
- void duplicate ( int a , int b , int c )
- a 2
- b 2
- c 2
33Execution of Example 6
- The user will be prompted to enter three
integers. - The user enters, for example, 10, 20, and 30 that
are saved in x, y, and z respectively. - When function duplicate is called, the addresses
of x, y, and z are passed to the function. - The addresses of x, y, and z are linked to the
parameters of the function a, b, and c. The
function will duplicate each parameter and save
the result in the same parameter. Thus, - a becomes 20 hence x becomes 20 also
- b becomes 40 hence x becomes 40 also
- c becomes 60 hence x becomes 60 also
- After the call we see that the values of x, y,
and z are changed. - The output
- Enter three integers 10 20 30
- Before call x 10 y 20 z
30 - After call x 20 y 40 z
60
34Recursion
Function call itself
include ltiostream.hgt int Fact (int N) if
(Nlt1) return 1 else return N
Fact(N-1) void main() coutltltFact(5)ltltendl
35Example
include ltiostream.hgt int Zap (int N) int
Z if (Nlt1) Z1 else Z Zap(N-1)
Zap(N-3) return Z void main()
coutltltZap(5)ltltendl
36Array as parameter
-Array name is pointer (call by reference)
include ltiostream.hgt void Increment (int a)
for (int i0 ilt5 i) ai 10 void
main() int b5 10,20,30,40,50 Increment(
b) for(int i0 ilt5 i) coutltltbiltlt'\t' c
outltltendl
37Array as parameter
-Array element (call by value)
include ltiostream.hgt void Increment (int a)
a void main() int b5
10,20,30,40,50 Increment(b1) coutltltb1ltlte
ndl