Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Functions

block of code that performs particular


task

Take Do Retur n
Ar gu men Wo r k
t Re su l t

it can be us ed multiple
times increase code
reusability
Syntax 1
Function
Prototype

void printHello( );

> Tell the


Syntax 2
Function
Definition

void printHello() {
pr intf(" Hello");
}

> Do the
Syntax 3

Function Call
int main() {
pr intHello( )
; return 0 ;
}

> U s e the
Properties

- Execution al w a ys st a rts from main

- A function gets called directly or indirectly from


main

- There can be multiple functions in a


program
Function Types

Library U s er -
defined
fu nction
Special declar ed & defined
functions by programmer
inbuilt in C
s canf( ), pr intf(
)
Passing
Arguments
functions can take val ue & give some
v al ue
parameter r etur n
v alu e
Passing
Arguments
void printHello( );

void printTable(int

n); int sum(int a, int

b);
Passing
Arguments
functions can take val ue & give some
v al ue
parameter r etur n
v alu e
Argument v/s
Parameter
v al u e s that v alu es in
are passed in fu nction
function call declaration &
definition

us ed to us ed to
send receive
v al u e v al u e

actual formal
par ameter par ameters
NOTE

a. Function can only retur n one v al u e at a time

b.Changes to parameters in function don't change the v al u e s

in calling function.
Beca us e a copy of argument i s passed to the function
Recursion

When a function calls itself, i t ' s called


recursion
1. Function to print Hello
2 . Function to calculate square of a number
3. Function to calculate n factorial (using
recursion)
Properties of
Recursion
a.Anything that can be done with Iteration, can be done
with recursion and vice-versa.

b. Recursion can sometimes give the most simple solution.

c. Ba s e Ca s e i s the condition which st op s recursion.

d. Iteration has infinite loop & Recursion has stack overflow

You might also like