Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

C in One Shot

Part - 5
Functions
in One Shot
What and
Why? w
y
=
f(x)

n
y f(x)
3

I
= =
+

Repetition (to
8) avoids -
loops
2) Readable
External
↑ ·Good morning
function
How
you?
·

are
-

-
·
Good Morning
Khatam How
you?
-
· are

I
· Good Morning
2 call
How
· are
you?
R

&RY - do not
repeat yourself
Basic syntax
fun(){

// code

}
main() ->
starts from main()

return ;
↳ Khatam
How functions work : ek ke andar doosra,
doosre ke andar teesra
Kaam ki baate :
1) main() ek hi baar aata hai.
2) Starts with main
3) unlimited functions
Return type : Power function se samajte hai
sum of 2 numbers

return type -arguments -

*
tadd (inta, intb) <

return
-w
a b;
+ -N
3
return 7;

main()

E inta, bi

scant (a), and,at


i

3
WoW
Library functions

rintf ("Hello");
&

scant("ood", (n);

sqrt (49);
pow(2, 3), ->23
Function Prototype - F
useless
Ques : Combination and Permutation
n = 7 #

urs
I

wc =
v= 3 31. 4!

int combination (intn, intr) (

1code

(n v)!
- =
(n v)x(n
- -
r -

1)
w


number
Ques : Combination and Permutation

ncw =
nfact/rfact *uefact

Hact urt e
=
x

"
(+1
=

+2
u or
SAT 35
*

= =
=
Ques : Pascal triangle
A

O I
2 =

21
1- 1

-to
I

2 1-2- 1

3 3 2
3 1

K
- - -

6-4 1
Y 1-1 -
-

10 10 5 1
-1 -
3 -
- - -

15 20 15 6 I
6 1 6 -
- - - -

work:
*
"Pre function
"Pr:ch
Ques : Pascal triangle
j -

O U U
12345 + O 12345 +

O I 0 Co
'C,
&

I I I
L 1 To

I
2 12 1
2 "%,
3 7 33 1
33632, 3C "C
Y I 464( Y "C","Y,"Cy
S I
d
I - 10 10
S S

A A ↑
i i Cj
Ques : Pascal triangle OPTIMISED
Ultimate Method "Maths' -
ncr+=
-

(n + 1)!x(n
-
r
-

1)!
0
0Co
- "Co 'C, · 5 "Cr+ =
H-

rc=-a.*anrore
24.2C 22
3
33C, 3C, "C

"C YC, nc,rc,


-

(n r)! (n v)x(n 1)!

=YCrtEnCLE
-

=
-
-
r -

=
=( (n r
l
-
-
Ques : Swap 2 numbers
inta 3;
=

intb 5;
=

V ↳W
a
b temp
int temp;
temp =
a;
a =b;
b =
temp;
Ques : Swap 2 numbers without
using a third variable

* W
s +58755
-

S
*
S

a b

a a
= +
bi

b a
=
-

b;

a a
=
-

b;
Scope of variable
C aukat

↳ limit

swapout
3

3inti, .
.
.

is

3
Formal parameters and Actual Parameters
void swap (inta, intb/<

int temp =
a;

a b,
=

b= temp;
return;
3

int main (75


a 2;
=
a&b are
formal
b 9;
=

I
209 actual
swap (a,b); are
&
Pass by value & Pass by reference
void swap(inta, intbia

int

a b,
=
temp =
a;
got to
emp
b= temp;
return;
3

int main (75


a

b
=

2;
9;
=
WE a
b
swap (a,b);
2
·

otes
*Pointers
↳ variable ka address stone

5;
W *
inta =

*
int x = ca
a
sh
*x 7; 3 x 20 U
6y522
=

Sx204
rintf("Oop",
& x); e

Printf(""op", (n); =
6y322
printf("Olod", *n); e5
*Pointers Swap 2 numbers
pass by reference
void swap (inta, intb/<

int temp =
a;

a b,
=

b= temp;
return;
3

int main (75


a 2;
=
a&b are
formal
b 9;
=

I
209 actual
Swap (a, 2b) ;
are
2
of *5N
a

zoox
b
500x 000x stox

W
temp
900x
*No a w

x 300 x400 xs
*x + 25 [Pod]
*
y - x300[%p]
**

y
+ 25[Pod]

1) Gate Wallah -
(Programming
2) C++ ->
pointers
Homewor K

Ques : A positive integer is entered through the


-=
-
-
.

keyboard. Write a function to obtain the prime


factors of this number.
Ilint: use
separate function to deck if a number is

prime or not
Ques : Write a function to compute the greatest
common divisor of two given numbers
a, b GCDj HCF

highest common factor

24 +
1, 2, 3, 4, 6, 8, 12, 24

12, 15, 20, 30, 60


60 - 1, 2, 3, 4, 5, 6, 10,

for (inti =
1; i < min (a,b);i+ + k

3
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
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. TOUR
5) A function may contain more than one return
statements. True- Only one should hit

6) Each return statement in a function may return a


different value. True
State TRUE or FALSE :

7) A function can still be useful even if you don’t pass


any arguments to it and the function doesn’t return any
value back. True

8) Same names can be used for different functions


without any conflict. False
State TRUE or FALSE :

9) A function may be called more than once from any


other function True

10) It is necessary for a function to return some value.


No. False
HW : Print the factorials of first ‘n’ numbers
↳ function
using
HW : Print first ‘n’ fibonacci numbers.

using functions

You might also like