Professional Documents
Culture Documents
Programming II
Omega Semester
2021/2022 Session
Module 2
Functions in C Language
2.1 What is a Function in C Language
3 Friday, 8 April 22
2.2 Benefits of C functions
i) C functions are used to avoid rewriting
same logic/code repeatedly in a program.
ii) You can call functions any number of times
in a program and from any place in the
program.
iii) A large C program can easily be tracked
when it is divided into functions.
iv) The core concept of C functions are re-
usability and to improve understandability
of very large C programs.
4 Friday, 8 April 22
2.3 Structure of C Functions
A function definition in C programming
consists of a function header and the
body with the following syntax.
5 Friday, 8 April 22
Structure of C Functions cont’d …
8 Friday, 8 April 22
Example 2.1 – Creating a function in C
9 Friday, 8 April 22
Example 2.1 shows the source code for a
function called min().
10 Friday, 8 April 22
2.3.1 Function Declarations
A function declaration or prototype: tells the
compiler about a function's name, return type, and
parameters. It also tells the compiler how to call the
function.
11 Friday, 8 April 22
2.3.2 Calling a Function
12 Friday, 8 April 22
Example 2.2: Calling a Function in C
1. #include <stdio.h>
2. int min(int, int); /* function declaration or prototype*/
3. int main () {
4. int a = 100;
5. int b = 200;
6. int ret;
7. ret = min(a, b); /* calling a function to get the minimum value */
8. printf( ”Min value is : %d\n", ret );
9. return 0;
10. }
11. int min(int val1, int val2) { /* function definition */
12. int result;
13. if (val1 < val2)
14. result = val1;
15. else
16. result = val2;
17. return result;
18. }
13 Friday, 8 April 22
• The summary of the syntax for C function
declaration, function call and function definition
are shown in Table 2.1.
Table 2.1: Summary of C Function Syntaxes
14 Friday, 8 April 22
Example 2.3 – A C program to compute the square of an input
1. #include<stdio.h>
2. // function prototype, also called function declaration
3. float square ( float x );
4. // main function, program starts from here
5. int main( )
6. {
7. float m, n ;
8. printf ( "\nEnter some number for finding square \n");
9. scanf ( "%f", &m ) ;
10. // function call
11. n = square ( m ) ;
12. printf ( "\nSquare of the given number %f is %f",m,n );
13. }
14. float square ( float x ) // function definition
15. {
16. float p ;
17. p=x*x;
18. return ( p ) ;
19. }
15 Friday, 8 April 22
In the Example 2.3 program, the function
“square” is called from main function.
17 Friday, 8 April 22
• While calling a function, there are two ways
in which arguments can be passed to a
function :
Ø Call by Value: This method copies the
actual value of an argument into the
formal parameter of the function.
Ø C all by Reference: This method
copies the address of an argument into
the formal parameter.
19 Friday, 8 April 22
Example 2.4 - A sample C program to illustrate call by value
1. #include<stdio.h>
2. // function prototype, also called function declaration
3. void swap(int a, int b);
4. int main()
5. {
6. int m = 22, n = 44;
7. // calling swap function by value
8. printf(" values before swap m = %d \nand n = %d", m, n);
9. swap(m, n);
10. }
11.
12. void swap(int a, int b)
13. {
14. int tmp;
15. tmp = a;
16. a = b;
17. b = tmp;
18. printf(" \nvalues after swap m = %d\n and n = %d", a, b);
19. return;
}
20 Friday, 8 April 22
Ø In Example 2.4 program, the values
of the variables “m” and “n” are
passed to the function “swap”.
21 Friday, 8 April 22
b) Function Argument: Call by reference
24 Friday, 8 April 22
2.4 Function Overloading and Library
25 Friday, 8 April 22
A function library is a collection of functions
that share a common area of interest (e.g.
math and time function libraries).
26 Friday, 8 April 22
Module 2 Assignment
Using the concept of function, write a C program
to compute the statistical values Mode, Median,
Mean, Standard Deviation, Kurtosis and
Skewness for any input vector.
Note:
This assignment should be submitted on
Moodle latest before the next class.
27 Friday, 8 April 22