April 24, 2025 A.
Sunil Samson 1
USER DEFINED FUNCTION
April 24, 2025 A. Sunil Samson 2
April 24, 2025 A. Sunil Samson 3
WHAT IS A FUNCTION?
-a block of statements that can be used repeatedly in
a program
- will not execute immediately when a page loads
- will be executed by a call to the function
- takes one or more input in the form of parameter
- does some processing using the input and returns a
value.
April 24, 2025 A. Sunil Samson 4
USER DEFINED FUNCTION
April 24, 2025 A. Sunil Samson 5
USER DEFINED FUNCTION
- option to create user’s own functions
- two parts in a user defined function
(i) Calling Function – The function which invokes
the action
(ii) Called Function - The Function which does
the job by receiving the call
from the calling function
April 24, 2025 A. Sunil Samson 6
CALLING AND CALLED FUNCTION
Calling Function:
The Function
which invokes the
action
Called Function:
The Function
which does the job by
receiving the call from
the Calling function
April 24, 2025 A. Sunil Samson 7
ADVANTAGES OF USING FUNCTION
• Code Reusability - defined only once and can be
invoked many times
• Less Code - saves a lot of code because the user
need not write the logic many times
• Easy to Understand - every logic is divided in the
form of functions
April 24, 2025 A. Sunil Samson 8
FUNCTION DECLARATION
(CALLING FUNCTION)
- tells about a function name and how to call the
function
- It has the following form…
Return_type Function_name(Parameter list);
- Retrurn_type => the data type of the value that the
function should return
- Function_name => Name of the function
- Parameter list => Data to be passed to be used by
the function for operation
April 24, 2025 A. Sunil Samson 9
PASSING PARAMETER
- Data to be passed to be used by the function for
operation
Types of Parameter:
1) Actual Parameter –
Parameters passed to the function actually
2) Formal Parameter –
Parameters received by the function
April 24, 2025 A. Sunil Samson 10
ACTUAL AND FORMAL PARAMETER
April 24, 2025 A. Sunil Samson 11
EXAMPLE: FUNCTION DECLARATION
- Also called function prototype
- Will be
int max(int num1, int num2);
- Once the above statement is encountered, the
control is transferred to the called function
April 24, 2025 A. Sunil Samson 12
DEFINING…?
When a problem is
identified, Stating
the information
needed to solve the
Problem
April 24, 2025 A. Sunil Samson 13
DEFINITION OF A FUNCTION
}
- provides the actual body of the function contains the Executable
Code
- The general form is…
return_type function_name( parameter list )
{
body of the function
}
- Where return_type => the data type of the value that the function
should return
- Function_name => Name of the function
- Parameter list => Data to be passed to be used by the
function for operation
- April
Function
24, 2025
body => contains a collection of statements that 14
A. Sunil Samson
EXAMPLE: FUNCTION DEFINITION
( CALLED FUNCTION)
int square ( int x ) // function definition
{
int p ;
p=x*x;
return ( p ) ;
}
return – answer to be returned to the called function
April 24, 2025 A. Sunil Samson 15
CALLING A FUNCTION
- To use the function definition, the defined function
has to be called by a function call
- Thus, when the program calls the function, the
control will be transferred to the called function
- Then the called function dose the job defined in it.
- Once the job is done, its return statement is
executed
- Therefore the result will be returned to the calling
function
April 24, 2025 A. Sunil Samson 16
Function call, Continued…
- The function call will be of the form…
E
DataType functionName(ParameterList);
Example:
int square ( m ) ;
April 24, 2025 A. Sunil Samson 17
EXAMPLE: FUNCTION
#include<stdio.h> // function definition
// function prototype int square (int x)
//Also called function declaration {
int square ( int x ); int p ;
void main( ) p=x*x;
{ return p;
int m, n ; }
printf ( "\nEnter m: \n");
scanf ( "%d", &m ) ; ------------------------------------
n = square ( m ) ; // function call Output:
printf ( "\nSquare is:%d”, n); Enter m: 4
getch(); Square is 16
}
April 24, 2025 A. Sunil Samson 18
CATEGORIES OF FUNCTION
S. No Function Category Description
1 Function with no argument No Passing any Argument to the
and no Return value called function and No receiving
any from the same
2 Function with no argument No argument is passed to the
and with Return value called function but called
function returns value to its
calling function
3 Function with argument and does not return a value but
No Return value accepts arguments as input.
4 Function with argument and Accepts argument and returns
Return value value
April 24, 2025 A. Sunil Samson 19
NO ARGUMENT AND NO RETURN VALUE
#include<stdio.h> printf("\nEnter radius : ");
void area(); // Prototype scanf("%f",&r);
void main() circle = 3.14 * r * r;
{ printf("Area is = %f", circle);
area(); }
} -----------------------------------------
Output:
void area() Enter radius: 7
{ Area is = 154
float circle, r;
April 24, 2025 A. Sunil Samson 20
NO ARGUMENT AND WITH RETURN VALUE
#include<stdio.h> printf("\nEnter radius : ");
void area(); // Prototype scanf("%f",&r);
void main() circle = 3.14 * r * r;
{
return circle;
float area();
}
printf("Area is = %f", circle);
} ------------------------------------
---
void area() Output:
{ Enter radius: 7
float circle, r; Area is = 154
April 24, 2025 A. Sunil Samson 21
WITH ARGUMENT BUT NO RETURN VALUE
April 24, 2025 A. Sunil Samson 22
WITH ARGUMENT BUT NO RETURN VALUE
#include<stdio.h> float area(float r1)
float area(float r); {
void main() float circle;
{ circle = 3.14 * r1 * r1;
printf("Area is = %f", circle);
float r; }
printf("\nEnter radius : "); --------------------------------------
scanf("%f",&r); Output:
float area(float r); Enter radius: 7
Area is = 154
}
April 24, 2025 A. Sunil Samson 23
WITH ARGUMENT AND RETURN VALUE
#include<stdio.h> float area(float r1)
float area(float r); {
void main() float circle;
{ circle = 3.14 * r1 * r1;
return circle;
float r; }
printf("\n Enter radius : "); ----------------------------------
scanf("%f", &r); Output:
float area(float r); Enter radius: 7
printf("Area is = %f", circle); Area is = 154
} April 24, 2025 A. Sunil Samson 24
April 24, 2025 A. Sunil Samson 25
WHAT IS RECURSION ?
A repeated
Process, or a
Definiton or a
Procedure
April 24, 2025 A. Sunil Samson 26
WHAT IS RECURSIVE?
- breaking down a problem into smaller and
smaller sub problems
- until the problem is made small enough that it can
be solved trivially
April 24, 2025 A. Sunil Samson 27
RECURSIVE FUNCTION
- process of calling a function by itself
- The function call is within the same function
April 24, 2025 A. Sunil Samson 28
EXAMPLE: RECURSIVE FUNCTION
#include <stdio.h> int fact(int n)
int fact(int n); {
void main() if (n = = 0)||(n= =1)
{ return 1;
int n; else
printf("Enter a number: "); return n*fact(n-1);
scanf("%d", &n); }
printf("Factorial is:%d", fact(n)); ------------------------------
} Output:
Enter a number: 5
Factorial is: 120
April 24, 2025 A. Sunil Samson 29
SCOPE OF A VARIABLE
The opportunity or
possibility to do or deal
with something
April 24, 2025 A. Sunil Samson 30
SCOPE OF A VARIABLE
- refers to the area of the program where the
variables can be accessed after its declaration
- variable declared within a function is different
from a variable declared outside a function.
POSITION TYPE
Inside a Function or a Local Variable
Block
Outside of all functions Global Variable
April 24, 2025 A. Sunil Samson 31
LOCAL VARIABLES
- Variables that are declared inside a function or
block
- can be used only by statements that are inside that
function or block of code
- are not known to functions outside their own
- Thus can be used only within the function
April 24, 2025 A. Sunil Samson 32
EXAMPLE: LOCAL VARIABLES
#include <stdio.h>
void main ()
{
int x,y,z; // local variables
x = 20; y = 30; z = x + y;
printf ("value of x :%d”, x);
printf (“\n value of y : %d”, y);
printf (“\n value of z : %d”, z); OUTPUT:
} Value of x: 20
Value of y: 30
Value of z: 50
April 24, 2025 A. Sunil Samson 33