You are on page 1of 27

Course Title: Applied Computer

Programming II

Course Code: GEC 225

Omega Semester
2021/2022 Session
Module 2
Functions in C Language
2.1 What is a Function in C Language

— A function is a collection of code


statements that performs a particular
task.
— Functions are also called method, sub-
routine or procedure.
— Note that every C program has at least
one function, which is named main().

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.

return_type function_name(parameter list )


{
body of the function
}

5 Friday, 8 April 22
Structure of C Functions cont’d …

—  Return Type − this is the data type of the


value the function returns. A function may
return a value whereas some functions
perform the desired operations without
returning a value, in which case the
return_type is void.

—  Function Name −This is the actual name of


the function. The function name and the
parameter list together constitute the
function signature.
6 Friday, 8 April 22
Structure of C Functions cont’d …

—  Parameters − A parameter is a placeholder


in C language, when a function is invoked, you
pass a value to the parameter. This value is
referred to as the actual parameter or
argument.
—  The parameter list refers to the type, order,
and number of the parameters of a function.
Parameters are optional in C language,
meaning that there are some functions that
may contain no parameters.
7 Friday, 8 April 22
—  Function Body − The function body contains a collection of
statements that define what the function does.
—  It begins with the opening brace ({) and follows the closing
parenthesis of the argument list and extends to the closing
brace (}).
—  The syntax for a typical function body is as shown below:

int VolumeOfCube(int width, int length, int height)


{
int volume;
volume = width*length*height;
return volume;
}

8 Friday, 8 April 22
Example 2.1 – Creating a function in C

// A function that returns the minimum between two numbers


1. #include<stdio.h>
2. int min(int val1, int val2)
3. {
4. int result; /* local variable declaration */
5. if (val1 < val2)
6. result = val1;
7. else
8. result = val2;
9. return result;
10. }

9 Friday, 8 April 22
—  Example 2.1 shows the source code for a
function called min().

—  This function takes two parameters:


val1 and val2 and returns the minimum value
between the two.

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.

—  The syntax of a function prototype is:


return_type function_name(parameter list);
—  From Example 2.1, we have:
int min(int val1, int val2);
or
int min(int, int);

11 Friday, 8 April 22
2.3.2 Calling a Function

—  When C function is created, you define what the


function has to do, while to use the function, you
must call it to perform the defined task.
—  A called function performs a defined task and when
its return statement is executed or when its function-
ending closing brace is reached, it returns the
program control back to the main program.
—  You simply pass the required parameters along with
the function name in order to call a function, and you
can store the returned value in case the function
returns a value.

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

C functions aspects Syntax

function definition return_type function_name (arguments list)


{
Body of function;
}

function call function_name (arguments list);

function declaration return_type function_name (argument list);

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.

— The value of “m” is passed as argument to


the function “square”.

— This value is multiplied by itself in this


function and the multiplied value “p” is
returned to main function from function
“square”.
16 Friday, 8 April 22
2.3.3 Function Arguments

—  If a function is to use arguments, it must


declare variables that accept the values of the
arguments. These variables are called the
formal parameters of the function.

—  Formal parameters behave like other local


variables inside the function and are created
upon entry into the function and destroyed
upon exit.

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.

•  By default, C uses call by value to pass


arguments.
18 Friday, 8 April 22
a) Function Argument: Call By Value
—  In call by value method, the value of the variable is passed
to the function as parameter.

—  The value of the actual parameter can not be modified by


formal parameter.

—  Note that actual parameter is the argument which is used


in function call while formal parameter is the argument
that is used in function definition.

—  Different memory is allocated for both actual and formal


parameters because the value of actual parameter is copied to
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”.

Ø These values are copied to formal


parameters “a” and “b” in swap
function and used.

21 Friday, 8 April 22
b) Function Argument: Call by reference

—  In call by reference, the address of the


variable is passed to the function as
parameter.

—  The value of the actual parameter can be


modified by formal parameter.

—  Same memory is used for both actual and


formal parameters since only address is
used by both parameters.
22 Friday, 8 April 22
Example 2.5 - A sample C program to illustrate call by
reference
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 reference
8. printf("values before swap m = %d \n and n = %d",m,n);
9. swap(&m, &n);
10. }
11. void swap(int *a, int *b)
12. {
13. int tmp;
14. tmp = *a;
15. *a = *b;
16. *b = tmp;
17.  printf("\n values after swap a = %d \nand b = %d", *a, *b);
18.  return;
18. }
23 Friday, 8 April 22
—  In Example 2.5 program, the address of the
variables “m” and “n” are passed to the function
“swap”.

—  These values are not copied to formal parameters


“a” and “b” in swap function because, they are just
holding the address of those variables.

—  This address is used to access and change the


values of the variables.

24 Friday, 8 April 22
2.4 Function Overloading and Library

—  When a function shares a common name, but has


two or more different signatures, it is called an
overloaded function. In most cases it is the argument
list that differs across signatures.

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).

—  Many vendors have added new libraries and


add-ons to support their products

—  You can also create your own functions


library in C language.

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

You might also like