FUNCTIONS
• The function contains the set of programming statements
enclosed by {} when called perform some specific tasks.
• They are also called subroutines or procedures in other
languages.
• A large program in c can be divided to many subprogram
• The subprogram posses a self contain components and
have well define purpose.
• The subprogram is called as a function
• Basically a job of function is to do something
• C program contain at least one function which is main()
2
Advantages of
function
It is much easier to write a structured program
where a large program can be divided into a
smaller, simpler task.
Allowing the code to be called many times
Easier to read and update
It is easier to debug a structured program
where there error is easy to find and fix
3
 C functions are used to avoid rewriting same
logic/code again and again in a program.
 There is no limit in calling C functions to make
use of same functionality wherever required.
 We can call functions any number of times in
a program and from any place in a program.
 A large C program can easily be tracked when
it is divided into functions.
 The core concept of C functions are, re-
usability, dividing a big task into small pieces
to achieve the functionality and to improve
understandability of very large C programs.
3. Uses of C functions:
4
Classification
Of Function
Library
function
User
define
function
- main()
-printf()
-scanf()
-sqrt()
-getchar()
2. Types of C functions
user-defined function
• A user-defined function is a type of function
in C language that is defined by the user
himself to perform some specific task. It
provides code reusability and modularity to
our program. User-defined functions are
different from built-in functions as their
working is specified by the user and no header
file is required for their usage.
user-defined function
• How to use User-Defined Functions in C
. The user-defined function in C can be divided
into three parts:
• Function Prototype
• Function Definition
• Function Call
C Function Prototype
• C Function Prototype:A function prototype is also known as a
function declaration which specifies the function’s name,
function parameters, and return type. The function prototype
does not contain the body of the function. It is basically used
to inform the compiler about the existence of the user-defined
function which can be used in the later part of the program.
Syntax
return_type function_name (type1 arg1, type2 arg2, ... typeN
argN);
or
return_type function_name (parameter list);
C Function Prototype
C Function Prototype
Components of Function Definition
There are three components of the function definition:
1. Function Parameters
2. Function Body
3. Return Value
• Function Parameters:Function parameters (also known as
arguments) are the values that are passed to the called function
by the caller.
Example
int sum(int a, int b);
Here, a and b are function parameters.
C Function Prototype
• Function Body:The function body is the set of statements
that are enclosed within { } braces. They are the
statements that are executed when the function is called.
Example
int sum (int a, int b)
{
int sum = a + b;
return sum;
}
Here, the statements between { and } is function body.
C Function Prototype
3. Return Value
The return value is the value returned by the function to
its caller. A function can only return a single value and it
is optional. If no value is to be returned, the return type
is defined as void.
The return keyword is used to return the value from a
function.
Syntax
return (expression); or
return;
C Function Definition
• C Function Definition:the function definition contains the actual
statements that will be executed. All the statements of the
function definition are enclosed within { } braces.
Syntax
• return_type function_name (type1 arg1, type2 arg2 .... typeN
argN) {
// actual statements to be executed
// return value if any
}
example
int foo (int a, int b)
{
int sum = a + b;
return sum;
}
The function contains the set of programming statements enclosed by {}  when called perform some specific tasks.

The function contains the set of programming statements enclosed by {}  when called perform some specific tasks.

  • 1.
    FUNCTIONS • The functioncontains the set of programming statements enclosed by {} when called perform some specific tasks. • They are also called subroutines or procedures in other languages. • A large program in c can be divided to many subprogram • The subprogram posses a self contain components and have well define purpose. • The subprogram is called as a function • Basically a job of function is to do something • C program contain at least one function which is main()
  • 2.
    2 Advantages of function It ismuch easier to write a structured program where a large program can be divided into a smaller, simpler task. Allowing the code to be called many times Easier to read and update It is easier to debug a structured program where there error is easy to find and fix
  • 3.
    3  C functionsare used to avoid rewriting same logic/code again and again in a program.  There is no limit in calling C functions to make use of same functionality wherever required.  We can call functions any number of times in a program and from any place in a program.  A large C program can easily be tracked when it is divided into functions.  The core concept of C functions are, re- usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C programs. 3. Uses of C functions:
  • 4.
  • 5.
    user-defined function • Auser-defined function is a type of function in C language that is defined by the user himself to perform some specific task. It provides code reusability and modularity to our program. User-defined functions are different from built-in functions as their working is specified by the user and no header file is required for their usage.
  • 6.
    user-defined function • Howto use User-Defined Functions in C . The user-defined function in C can be divided into three parts: • Function Prototype • Function Definition • Function Call
  • 7.
    C Function Prototype •C Function Prototype:A function prototype is also known as a function declaration which specifies the function’s name, function parameters, and return type. The function prototype does not contain the body of the function. It is basically used to inform the compiler about the existence of the user-defined function which can be used in the later part of the program. Syntax return_type function_name (type1 arg1, type2 arg2, ... typeN argN); or return_type function_name (parameter list);
  • 8.
  • 9.
    C Function Prototype Componentsof Function Definition There are three components of the function definition: 1. Function Parameters 2. Function Body 3. Return Value • Function Parameters:Function parameters (also known as arguments) are the values that are passed to the called function by the caller. Example int sum(int a, int b); Here, a and b are function parameters.
  • 10.
    C Function Prototype •Function Body:The function body is the set of statements that are enclosed within { } braces. They are the statements that are executed when the function is called. Example int sum (int a, int b) { int sum = a + b; return sum; } Here, the statements between { and } is function body.
  • 11.
    C Function Prototype 3.Return Value The return value is the value returned by the function to its caller. A function can only return a single value and it is optional. If no value is to be returned, the return type is defined as void. The return keyword is used to return the value from a function. Syntax return (expression); or return;
  • 12.
    C Function Definition •C Function Definition:the function definition contains the actual statements that will be executed. All the statements of the function definition are enclosed within { } braces. Syntax • return_type function_name (type1 arg1, type2 arg2 .... typeN argN) { // actual statements to be executed // return value if any } example int foo (int a, int b) { int sum = a + b; return sum; }