You are on page 1of 38

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?

A lot of code change! Duh!!!


Can we do it in a better way?
The importance of functions
d
in projects we
always tend use
to same/similar pice of
code in
projectmultiple times,
the not
but always we

looking for repetition


are
com
loops
Syntax for Functions

Wrapping the logic under a name

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

The need for arguments

function_name(int a, int b, int c) {


// function body
}
Syntax for Functions

The need for a return type: Understanding with


example

<void/int/float> function_name(int a, int b, int c) {


// function body
}
ReturnType:
put

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

2) Ye ek his boar chalta hai


Some inbuilt library functions
min(x,y)

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
*

nfact /rfact 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…

C Drive - Movies - Action -> Spiderman


Formal parameters and Actual Parameters

~ 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 :

1) The variables commonly used in C++ functions are


available to all the functions in a program. False

2) To return the control back to the calling function we


must use the keyword return. True, but not for void
resicality
3) The same variable names can be used in different
functions without any conflict. True
State TRUE or FALSE :

4) Every called function must contain a return


statement. False

5) A function may contain more than one return


statements. True. Only one hit.
can

6) Each return statement in a function may return a


different value. True
Bonus Ques : Print the factorials of first n numbers
=I
1!
2
2! 2x1!
=

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

Working with memory addresses using Pointers!

you

You might also like