You are on page 1of 27

Functions

Sudesh Maduwantha Kumarasiri


Under Graduate student
University Of Kelaniya
BENEFITS OF USING FUNCTIONS

e c h
T
q The function provides modularity.

K -
q The function provides reusable code.

D
q In large programs, debugging and editing tasks is easy
with the use of functions.

q The program can be modularized into smaller parts.

q Separate function independently can be developed


according to the needs.
TWO TYPES OF FUNCTIONS IN C

e c h
T
q Built-in (Library) functions: They are stored in system

-
libraries.

D K
§ scanf, printf, strcpy, strcmp, strlen, round etc. To use
these functions appropriate C header files should be
imported.

q User defined functions

§ The functions that are defined (written) by the


programmer (user) within a program.
h
PARTS OF FUNCTIONS

e c
- T
q Function Prototype (function declaration)

K
❑ Function Definition

D
❑ Function Call
FUNCTION PROTOTYPE(FUNCTION
DECLARATION)

e c h
- T
D K
Syntax:

data_Type function_Name (Parameter List)


FUNCTION DEFINITION

e c h
- T
K
Syntax:

D
return_Type function_Name ( Function arguments )

}
CALLING THE FUNCTION
#include <stdio.h>

/* function declaration */
int addition();

c h
int main()

e
{

T
/* local variable definition */

K -
int answer; /*calling a function to get addition value */
answer = addition();

D
printf("The addition of the two numbers is: %d\n",answer);

return 0;
} /* function returning the addition of two numbers */

int addition()
{
/* local variable definition */
int num1,num25;
return num1+num2:
}
FUNCTIONS

e c h
T
q A function is a group of statements that is

-
executed when it is called from some point of the

K
program.

D
q Using functions, we can structure our program in a
more modular way.

q Function is a module or block of a program code


which deals with a particular task.
HOW TO WRITE AND USE A FUNCTION IN
C

e c h
- T
K
q First a function must be declared and defined:

D
q Function declaration (function prototype) .

q Function definition Then the function can be used:


Function call .
FUNCTION ARGUMENTS

e c h
- T
D K
h
CALL BY VALUE:

e c
- T
D K
CALL BY REFERENCE

e c h
- T
D K
FUNCTION DECLARATION (FUNCTION

h
PROTOTYPE)

e c
- T
D K
C PROGRAM TO DEMONSTRATIVE THE USE OF

h
LIBRARY

e c
T
q Library functions in C language are inbuilt functions which are

-
grouped together and placed in a common place called library.

D K
q Each library function in C performs specific operation.

q We can make use of these library functions to get the pre-defined


output instead of writing our own code to get those outputs.

q These library functions are created by the persons who designed


and created C compilers.
C PROGRAM TO DEMONSTRATIVE THE USE OF
LIBRARY

e c h
qAll C standard library functions are declared in many header files

T
which are saved as file_name.h

K -
qActually, function declaration, definition for macros are given in

D
all header files.

qWe are including these header files in our C program using


“#include” command to make use of the functions those are
declared in the header files.

qWhen we include header files in our C program using “#include”


command, all C code of the header files are included in C program.
Then, this C program is compiled by compiler and executed.
C VARIABLE SCOPE

e c h
q A scope is a region of the program, and the scope of

T
variables refers to the area of the program where the

-
variables can be accessed after its declaration.

D K
q In C every variable defined in scope. You can define
scope as the section or region of a program where a
variable has its existence; moreover, that variable
cannot be used or accessed beyond that region.

q In C programming, variable declared within a function is


different from a variable declared outside of a function.
The variable can be declared in three places. These are:
VARIABLE SCOPES ARE 3 TYPES

e c h
- T
D K
GLOBAL VARIABLES VS LOCAL VARIABLES

e c h
T
Local scope The variable
Global scope The

-
loc1 is a local variable
variable g is a inside the main function.

K
global variable. It can only be accessed

D
It can be from within the scope of
accessed from any main function.
function in the C Local scope The variable
program file loc2 is a local variable
(global scope) inside the display
function. It can only be
accessed from within the
scope of display function.
HERE ARE SOME OF THE MOST COMMONLY USED

h
HEADER FILES

e c
- T
D K
FOUR TYPES OF USER DEFINED

h
FUNCTIONS

e c
- T
q Functions with no parameters and no return.

D K
q Functions with no parameters and a return.

q Functions with parameters and no return.

q Functions with parameters and a return.


FUNCTIONS WITH PARAMETERS AND A
RETURN.

e c h
- T
D K
FUNCTIONS WITH NO PARAMETERS AND NO
RETURN

e c
c h
h
- T
T e
K
K -
D
D
FUNCTIONS WITH NO PARAMETERS AND A
RETURN.

e c h
- T
D K
FUNCTIONS WITH PARAMETERS AND NO
RETURN.

e c h
- T
D K
WHY ARE FUNCTIONS NEEDED?

e c h
T
q Reusability :

K -
v Once a function is defined, it can be used over and over
and … over again.

D
q Abstraction
v In order to use a function –

§ you only need to know its the name, parameters list,


return type and what the function does. you don’t need
to know any implementation details of a function!
QUESTIONS

c h
1. Write a function that takes two integers and returns the

e
minimum of the two numbers.

- T
2. Write a function that takes three integers and returns the

K
average of the three numbers.

D
3. Write a function to display the text “Hello world using a
function!” .

4. Write a function that takes an integer and checks whether the


given number is an odd number or an even number.

5. Write a function that takes in an integer (can be negative or


positive) and returns its positive value
**HAPPY CODING**

You might also like