Professional Documents
Culture Documents
07-Funtion in C M
07-Funtion in C M
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.
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.
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:
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.
e c h
- T
K
q First a function must be declared and defined:
D
q Function declaration (function prototype) .
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.
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.
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.
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.
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 –
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!” .