Function
Function
Function Prototype
Program
Function General Form
Parameter
Declaration vs Definition
• A declaration specifies the type of an object.
• A definition causes storage for an object to be created
• In function-
• prototype is declaration
• the function itself, which contain the body of the function
is a definition
Function Call
• We can accessed a function by specifying its name, followed by a
list of arguments enclosed in parentheses and separated by
commas.
• If the function call does not require any arguments, an empty pair
of parentheses must follow the name of the function.
Function Call
• A function will carry out tis intended action whenever it is
accessed (called) from some other portion of the program.
• Once the function has carried out its intended action (end of that
function is reached), control will be returned to the point from
which the function was accessed.
• Traditionally, main function is not called by any other function, but
there is no technical restriction.
The function call
1
3
Function 4
Call
5
Finish!
9
10
7
8
End of Function
The function call
1
3
Function 4
Call
5
Finish!
9
10
7
8
End of Function
Function Call
• There are four types of functions and they are:
1. Functions with no arguments and no return values.
2. Functions with arguments and no return values.
3. Functions with no arguments and return values.
4. Functions with arguments and return values.
Functions with no arguments and no return values
Functions with no arguments and no return values
l Pass
Con t r o
p a s sing
gu ment
No ar
No return va
lu e
Control Pass
Pass
r o l b)
Con
t
i n g ( a,
tp ass
u m en
ar g
No return value
Control Pass
l P ass s s ing
tro nt p a
Con rgum
e
n o a
return
Control P value
ass r
P ass g (a , b)
tro l as s in
Con m en tp
ar g u
return value
Control Pass r
• Using recursion
Recursion..
• In recursion, no multiple copies of the recursive function
will create. Only one copy exist.
• When a function is called, storage for its parameters and
local data are allocated on the stack.
• Thus, when a function is called recursively, the function
begins executing with a new set of parameter and local
variables, but the function code remain same.
How recursion actually work
• return (n*factorial(n-1));
n! = n*(n-1)! 2! = 2*1
(n-1)! = (n-1)*(n-2)! 3! = 3*2!
(n-2)! = (n-2)*(n-3)! 4! = 4*3!
……………… ………………
……………… ………………
4! = 4*3! (n-2)! = (n-2)*(n-3)!
3! = 3*2! (n-1)! = (n-1)*(n-2)!
2! = 2*1 n! = n*(n-1)!
Home Task
• Write a recursive function that will copy a string to
another. You can’t use strcpy function.
A Closer Look at Parameters
• We can pass arguments to functions in two way
• Call by value
• Call by referance
Call by value
• This method copies the value of an argument into the
formal parameter of the subroutine (function).
• The change made to a parameter of the subroutine have
to no effect on the argument used to call it.
• Default method to call a function
Call by reference
• In this method, the address of an argument is copied into
the parameter.
• Should use pointer
• Inside the subroutine, the address is used to access the
actual argument.
• This means the change made to the parameter will affect
the argument.