Professional Documents
Culture Documents
Functions
Functions
Lecture- 9
Raghav Garg
+3
f(x)
x
=
=
y
I input
output
Let’s create new bigger patterns
Take a, b, c as input and print the following pattern:
a = 3, b = 4, c = 5
*
**
***
*
**
***
****
*
**
***
****
*****
Let’s change the code!
What if we we want to make a same pattern for
numbers?
function_name {
// function body
}
- Output
I
~
·
Good Morning
· Have a nice
day
w
~
Good
·
Morning
~
v
· Have a nice
day
·
Good Morning
· Have a nice
day
⑧
Syntax for Functions
I
~
It
E 103
X
I
W I
I -
** *
&
I
↑ * Y
* dX
W
- -
A & & &
v
What is int main()?
L
main function
d
1) sabse pehle yahi chatta hai
max(x,y)
Saut (x) i
Output/Input
I
↳ T 3 Y
y
x
S
&
↳
~
~ 3Y
Ques : Combination and Permutation
Factorial ->
n!= nxn-1xn-2x...3x2x1
Sx4x3X2x1
S!
=
u!
"Pr
!
=
·cr
=
I
int combination (intn, intw)4
"C 2 1!
-
n S
=
=
=
3 2
5
2! 3!
=
+
n -
2 3
=
Ques : Combination and Permutation
nfact urfact
*
~fact
nfact/(wfact*urfact) I
nfact
-
~fact *urfact
Fr
Output
I
enter n: S
enter 2:2
⑯
urfact
Ques : Pascal triangle E 1
em
I
O I 2 3 Y 5 -
I
O I 23Y S
⑧ I Co
0
I 1 ,
is;
I
2 I 2 I
2 ,
3 I 3 3 I 3 "32,22 C,
Y - 4y I 4 "o"C,"C"Cs "Cy
<10/10ici=" "CoSC, C, ,s
s I S
S
d
ic;
x
I? i
Ques : Pascal triangle Optimised
-
came=
ntr+]!- (2+1).w!(n -r-1)!
:
-!
"Cu+="(r.(?)
Ques : Pascal triangle Optimised
123
I i.
D
3,
di ->
32
3C,
next:
(I
cur x
Ques : Pascal triangle Optimised
"Cu+="Cr. ( C
Are arguments passed actually the same?
Printing out the actual address of variables in the
functions…
cntyld
intmlintx,
3
N
int main (14
3
intx;
fency: N
Are arguments passed actually the same?
Printing out the actual address of variables in the
functions…
~ formal parameters
1/ fun (3,7);
↓ ↓
actual parameters
Scope of variable I
Limits
inta Y;
=
Default values of Arguments
- is now
defaultvalue of X
8 is defaultvalue y
of
↳ y
-
Default values of Arguments
IX
I
Default values of Arguments
i
X
y
d
float
Ques : Write a function to compute the greatest
common divisor of two given numbers
X, y
HCE (x, y) min(x,y)
=
L
L ↓
highest, factor
2460
common
a number
1, 2, 3, 4, 6,
12
divisible by both
Ques : Write a function to compute the greatest
common divisor of two given numbers
zu To
↳ RY
↓-
&
ef
State TRUE or FALSE :
6
3! 3x
=
2! =
3! 24
=
y! 4x
=
**
Ques : Swap 2 numbers
output
I
B ·
·
38
8 8
Ques : Swap 2 numbers
Output
I
v 9
↳
.
~
~
-
~
Why does this not work? Output
I
↓
Functions d 3 6
↳Fe
6
~
-
a
V
~
~
w
-
-
a B
-
v &
-
Is there a way to solve this?
What if we are able to store or pass the actual
address inside functions?
Sisters Ref
Next Lecture
Understanding the memory aspects of programming
you