forked from abhibarkade111/codingelite.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.html
More file actions
587 lines (504 loc) · 23.7 KB
/
function.html
File metadata and controls
587 lines (504 loc) · 23.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
<title>CodingElite</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark navm">
<div class="container-fluid">
<a class="navbar-brand" href="index.html">CodingElite</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link " aria-current="page" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link " href="/codingelite.org/blog.html">Blog</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link active dropdown-toggle" href="#" id="navbarDropdown" role="button"
data-bs-toggle="dropdown" aria-expanded="false">
C Tutorials
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="/codingelite.org/introduction.html">introduction</a></li>
<li><a class="dropdown-item" href="/codingelite.org/branching.html">Branching Statements</a></li>
<li><a class="dropdown-item" href="/codingelite.org/looping.html">looping statements</a></li>
<li><a class="dropdown-item" href="/codingelite.org/function.html">Function</a></li>
<li><a class="dropdown-item" href="/codingelite.org/array.html">Array</a></li>
<li><a class="dropdown-item" href="/codingelite.org/string.html">string</a></li>
<li><a class="dropdown-item" href="/codingelite.org/pointer.html">pointers</a></li>
<li><a class="dropdown-item" href="/codingelite.org/structure.html">structures</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="/codingelite.org/blog.html">More Topics</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/book.html">Books</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/about.html">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/contact.html">Contact us</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="bg-light">
<div class="container bg-c">
<h1 class="hed hedi-h1"><strong> Function </strong></h1>
<h3>Function</h3>
<p> Function is set of instructions to carry out a particular task. <br>
A C program has at least one function main( ). Without main() function, there is technically no C program.
A function can be called multiple times to provide reusability and modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known as procedureor subroutinein other programming languages.
</p>
<h4>Why we use function in C programming?</h4>
<ul class="f1">
<li>It is very difficult for the programmer to write large programs.</li>
<li>Very difficult to identify the logical errors and correct.</li>
<li>Very difficult to read and understand.</li>
<li>Large programs are more complex to find the errors.</li>
</ul>
<p>To overcome all the difficulties we use Function in C programming.</p>
<h4>Tyes of function in C</h4>
<p>There are two types of functions in C on basis of whether it is defined by user or not.</p>
<ol>
<li>Library Function</li>
<li>User defined Function</li>
</ol>
<h5>1)Library Function</h5>
<p> Library functions are the pre-defined function in C programming language. </p>
<h6><b>1.main()</b></h6>
<ul>
<li> The execution of every C program starts from this main() function.</li>
<li>If you want to use printf and scanf function, the header file <stdio.h> should be included.</li>
</ul>
<h6><b>2.printf()function</b></h6>
<ul>
<li> prinf() is used for displaying output in C.</li>
</ul>
<h6><b>3.scanf() function</b></h6>
<ul>
<li>scanf() is used for taking input from user in C.</li>
</ul>
<p>There are many library functions available in C programming to help the programmer to write a good
efficient program.</p>
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span><br>
<span class="hljs-meta-keyword">#include<conio.h></span><br>
<span class="hljs-meta-keyword">#include<string.h></span><br>
<span class="hljs-meta-keyword">#include<math.h></span><br>
</span> <br>
<p> In C programming you can copy string by just using strcpy() function which is defined under header file
"string.h". <br> These are some other library functions defined in "string.h" </p>
<ul>
<li> strcmp(); </li>
<li> strcat(); </li>
<li> strlen(); etc.</li>
</ul>
<h5>2)User defined Functions</h5>
<p> C provides programmer to define their own function according to their requirement is known as "user defined
functions".</p>
<h6>Advantages of user defined functions</h6>
<ul>
<li> User defined functions helps to reduce the large program into small segments which makes programmer
easy to understand, maintain and debug.</li>
<li>If repeated code occurs in a program. Function can be used to include those codes and execute when
needed by calling that function.</li>
<li>Programmer working on large project can divide the workload into some function by making different
functions.</li>
</ul>
<h6>Elements of user defined functions:</h6>
<ol>
<li>Function declaration </li>
<li>Function call</li>
<li>Function definition</li>
</ol>
<h4>1.Function declaration</h4>
<p><b> Syntax:</b> </p>
<div class="row">
<div class="col-md-8">
<pre class="pre-bl">Funtion_type function_name(parameter list);</pre>
</div>
</div>
<p> A function declaration consists of four parts. They are,</p>
<ul>
<li> Function type</li>
<li> Function name</li>
<li> Parameter list</li>
<li>Terminating semicolon</li>
</ul>
<p><b>Example:</b></p>
<div class="row">
<div class="col-md-8">
<pre class="pre-bl">int add(int a, int b);</pre>
</div>
</div>
<h4></b>2.Function call</h4>
<p> Invoking(calling) of a function is called "function call".</p>
<p><b> Syntax:</b> </p>
<div class="row">
<div class="col-md-8">
<pre class="pre-bl">function_name(Parameter name);</pre>
</div>
</div>
<p><b>Example:</b></p>
<div class="row">
<div class="col-md-8">
<pre class="pre-bl">add(a,b);</pre>
</div>
</div>
<h4></b>3.Function definition</h4>
<p> The program module that is written to achieve a specific task is called "function definition".</p>
<div class="row">
<div class="col-md-8">
<img src="img/Function.png" alt="Function Structure" class="img-fluid img-p">
</div>
</div>
<p><b> Syntax:</b> </p>
<div class="row">
<div class="col-md-8">
<pre class="pre-bl">Function Type Function name (Parameter List)
{
Local declaration;
Function statements;
Return statements;
}</pre>
</div>
</div>
<h5>Function Header</h5>
<p>The function header consists of three parts: <br> function type, <br> function name <br> list of
parameter. <br><br></p>
<ul>
<li> <b>Function type</b></li>
<p> The function type specifies the type of value (like int or float) that the function is expected to
return to the calling program. If the return type is not specified then C will assume that it is an
integer type(int).</p>
<li> <b>Function name</b></li>
<p>The function name is any valid C identifier and the name should be appropriate to the task performed by
the function.</p>
<li> <b>List of parameters</b></li>
<p>The parameter list declares the variables. In this variables will store the received data sent by the
calling program. They serve as input data to the function to carry out the specific task.</p>
</ul>
<p><b>Example:</b></p>
<div class="row">
<div class="col-md-8">
<pre class="pre-bl">
1) Float sub (float x, float y)
2) int mul (int a, int b)
</pre>
</div>
</div>
<h5>Function body</h5>
<p>Function body is inside the curly braces. It contain Following three parts : <br><br></p>
<ul>
<li> <b>Function variable declaration</b></li>
<p> It is a statement that specify the variables.</p>
<li> <b>Function statement</b></li>
<p> It is a statement that perform the task of the function.</p>
<li> <b>Return statement</b></li>
<p> It is a statement that returns the value evaluated by the function.</p>
</ul>
<p><b>Syntax with Example:</b></p>
<div class="row">
<div class="col-md-8">
<pre class="pre-bl">
int add(int a, int b)
{
int sum;
sum=a+b;
return sum;
}
</pre>
</div>
</div>
<h5>Sample program</h5>
<h6>Addition of two integer numbers using user defined function.</h6>
<div class="col-md-8">
<pre class="pre-bl" >
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
//Function declaration
int sum(int a, int b);
void main()
{
int a,b,x;
clrscr();
printf("Enter any two number of type integer/n");
scanf("%d%d"&a,&b);
// Function call
x=sum(a,b);
printf("Sum of two numbers=%d",x);
getch();
}
// Function Definition
int sum(int a, int b)
{
int sum;
sum=a+b;
return(sum);
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
Enter any two number of type integer
10
11
Sum of two numbers=21
</pre>
</div>
<h4>Functions Prototypes (Function Declaration)</h4>
<p> On the basis of parameters and return values, the functions declaration classified into following types-</p>
<ol>
<li>Function with no return type and no arguments.</li>
<li>Function with return type but with no arguments.</li>
<li>Function with no return type but with arguments.</li>
<li>Function with return type and with arguments.</li>
</ol>
<h5><b>1.Function with no return type and no arguments.</b></h5>
<p> In this method, there is no data transfer between the calling function and called function.Return type is
void. So this returns nothing.</p>
<h5>Sample program</h5>
<h6> Swap two numbers using user defined function with no argument and no return type</h6>
<div class="col-md-8">
<pre class="pre-bl">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
void swap();
void main()
{
clrscr();
swap();
getch();
}
void swap()
{
int num1.num2,temp;
num1=10;
num2=11;
temp=num1;
num1=num2;
num2=temp;
printf("num1=%d" and num2=%d,num1,num2);
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
num1=11 and num2-10
</pre>
</div>
<h5><b>2.Function with return type but with no arguments.</b></h5>
<p> In this method, there is no data transfer between the calling function and called function.Here we won’t
pass any arguments to the function while defining, declaring or calling the function.</p>
<h5>Sample program</h5>
<h6> Implement a program to find maximum of two numbers using function
(Prototype: no argument, with return type)</h6>
<div class="col-md-8">
<pre class="pre-bl">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
int max();
void main()
{
int y;
clrscr();
y=max();
printf("max=%d",y);
getch();
}
int max()
{
int a,b;
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
if(a>b)
{
return(a);
}
else
{
return(b);
}
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
Enter any two numbers
10
11
max=11
</pre>
</div>
<h5><b>3.Function with no return type but with arguments.</b></h5>
<p> In this method, we pass the arguments to the function while calling the function. But, This type of function
will not return any value when we call the function from main() function.</p>
<h5>Sample program</h5>
<h6> Implement a program to find minimum of number using function
(Prototype: with argument, no return type)</h6>
<div class="col-md-8">
<pre class="pre-bl" >
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
void min(int a,int b);
void main()
{
int a,b;
clrscr();
printf("Enter any two numbers");
scanf("%d%d",&a,&b);
min(a,b);
getch();
}
void min(int a,int b)
{
if(a < b)
{
printf("%d is minimum",a);
}
else
{
printf("%d is minimum",b);
}
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
Enter any two numbers
10
11
10 is minimum
</pre>
</div>
<h5><b>4.Function with return type and with arguments.</b></h5>
<p> In this method, it allows us to pass the arguments to the function while calling the function. This type of
function will return some value when we call the function from main() function.</p>
<h5>Sample program</h5>
<h6> Implement a program to find average of three numbers using function
(Prototype: with argument, with return type)</h6>
<div class="col-md-8">
<pre class="pre-bl">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
int avg(int a,int b,int c);
void main()
{
int a,b,c,A;
clrscr();
printf("enter three numbers\n");
scanf("%d%d%d",&a,&b,&c);
A=avg(a,b,c);
printf("average=%d",A);
getch();
}
int avg(int a,int b,int c)
{
int average;
average=(a+b+c)/3;
return(average);
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
enter three numbers
11
10
9
average=10
</pre>
</div>
<h5>Methods of parameter(Argument) passing</h5>
<ol>
<li>Pass by value(call by value)</li>
<li>Pass by address (call by address) </li>
</ol>
<h6>1.Pass by value(call by value)</h6>
<p> In this method, the value of the actual parameters is copied into the formal parameters.
The actual parameter is the argument used in the function call whereas and parameter is the argument which
is used in the function definition. Here we can't modify the value of the actual parameter by the formal
parameter. <br>
By default, C programming uses call by value to pass arguments.</p>
<h5>Sample program</h5>
<div class="col-md-8">
<pre class="pre-bl">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
</span>
int sum(int a, int b)
{
int c=a+b;
return c;
}
int main(
{
int var1 =10;
int var2 = 20;
int var3 = sum(var1, var2);
printf("sum=%d", var3);
return 0;
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
sum=30
</pre>
</div>
<h6>2.Pass by address (call by address) </h6>
<p> [We will learn this concept in pointer tutorial.] </p>
<h5>Examples For Practice</h5>
<ol>
<li>Implement a program to find maximum of two numbers using function
(Prototype: no argument, no return type)</li>
<li>Find Factorial of any number using user defined function.</li>
<li>Find whether given number is Armstrong number or not using user defined function
Armstrong Number: If the number is equal to sum of cube of each digits in number.
</li>
</ol>
<a href="/codingelite.org/looping.html"> <button type="button" class="btn btn-primary btn-m"> <<Previous</button> </a>
<a href="/codingelite.org/array.html"> <button type="button" class="btn btn-primary btn-m btn-r">Next >></button></a>
</div>
</div>
<footer class="page-footer font-small blue navbar-dark bg-light footer-ptt">
<div class="footer-copyright text-center py-3">
© Copyright 2021. All Right Reserved. | Designed by the <a href="/codingelite.org/about.html">CodingElite team</a>. |
<a class="color-b" href="/codingelite.org/privacyPolicy.html">Privacy policy</a> | <a class="color-b" href="/codingelite.org/contact.html">Contact us</a>
</div>
</footer>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW"
crossorigin="anonymous"></script>
<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js" integrity="sha384-q2kxQ16AaE6UbzuKqyBE9/u/KzioAlnx2maXQHiDX9d4/zp8Ok3f+M7DPm+Ib6IU" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.min.js" integrity="sha384-pQQkAEnwaBkjpqZ8RU1fF1AKtTcHJwFl3pblpTlHXybJjHpMYo79HY3hIi4NKxyj" crossorigin="anonymous"></script>
-->
</body>
</html>