User Defined
Functions
Classifications of function
 Functions can be classified as
Pre defined function:
Function whose definitions have already been return
inside c(Ex:printf())
User Defined Functions:
Functions which are designed by the
user{Ex:disp(),show()}
Top- to – bottom modular
programming :

Main Program
Function 1 Function 2 Function 3
Function 2.1Function 1.1
Understanding the Flow
of Program
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“n” I am a Programmer ?”n");
getch();
}
Understanding the Flow
of Program

main()
{
…………..
………….. display();
…………
………..
return0;
}
display()
{
StatementBlock;
}
Class work
 Write a C program that calculates Volume of cylinder
using functions
 Your user defined function’s name should be
volume()
 Get the values of Radius and height inside the user
defined function
 volume=( Π x r2 x h )
Elements of user defined
function
A user defined function must have the following elements
 Function Declaration
 Function definition
 Function call
Defining a function
 A function definition otherwise called as function
implementation must have the following elements
 Function name
 Function type
 List of parameters
 Local variable declaration
 Function statements
 A return statement (for types other than void)
General Form of function
function_type function_name(parameter list)
{
local variable declaration;
executable statement 1;
executable statement 2;
.
.
.
return statement;
}
Function header
Function Body
Formal parameters/
Arguments
 Parameters are nothing but variables that are passed along
with the functions
 It is not mandatory to use parameters for every user defined
functions
Ex:
void add(int a, int b) //a&b –Formal parameter
{
…..
…..
}
Example using parameters
void add(int x, int y); //declaration
void main()
{
int x,y;
scanf(“%d%d”,&x,&y);
add(x,y); // function call//formal parameter
}
void add(int x,int y) //fn with arguments//actual
parameter
{
int z;
z=x+y;
printf(“sum of %d and %d is %d”,x,y,z);
}
11
variety of the function calls
add(20,5);
add(y,10);
add(10,z);
add(p,q);
add(p*5,q-10);
Classifications of function
 Based on arguments and return type, functions are
classified as four
1. Without argument without return value
2. with arguments and without return value
3. without argument and with return value
4. with value and with return value
No argument and no return
type
void main()
{
void add();
add();
}
void add()
{
…
…
}
With argument and without
return type
void main()
{
void add(int a, int b);
int p,q; scanf(“%d%d”,&p,&q);
add(p,q);
}
void add(int x, int y)
{
…
…
}
Without argument and with
return type
void main()
{
int add();
add();
}
int add()
{
…
…
return 0;
}
With argument and with
return type
void main()
{
float add(int a, int b);
int p,q; scanf(“%d%d”,&p,&q);
add(p,q);
}
float add(int x, int y)
{
…
…
return 1;
}
Example
Void main()
{
int a=15,b=20;
void add(int , int );
add(a,b); // a and b actual parameters
}
add(int p, int q) // p and q Formal Parameters
{
int c;
c=p,q;
printf(“The sum of %d and %d is %d”,p,q,c);
}
Nesting of functions
 C Permits nesting of function
Ex:
void main()
{
fn1();
}
void fn1()
{
fn2();
}
void fn2()
{
fn3();
}
Recursion
 When a function calls itself, then the process is known
as recursive function
 In other words, if the calling function and the called
function are same, then the process is called as
recursion and that function is called as recursive
function
Ex:
void main()
{
Printf(“Main Function is Executed “);
main();
}
Use of recursion
Ex:
factorial(int i)
{
int f;
if(i==1)
return 1;
else
f=i*factorial(i-1);
return(f);
}
Classification of variables-
Storage classes
 Previously variables are classified based on their
datatype, scope(Lifetime)
 Variables can also be classified based on their storage
classes
 The Four storage classes are
 Automatic
 Static
 External
 Register
Automatic Variable
 This variable is created when a function is created and
will be destroyed at the end of the function
 Automatic Variables are typically private(Local
Variable)
 If a storage class for a variable is not specified, the
compiler by default will consider it as Automatic
 The keyword used for specification of Automatic
variable declaration is auto
Example-Automatic Variable
void main()
{
int v=50;
printf(“%d”,v);
}
int fn1()
{
auto int v=700;
printf(“%d”,v);
}
int fn2()
{
int v=17000;
printf(“%d”,v);
}
External Variable
 External variables are nothing but global variables
which can be accessed throughout the program
 They are declared outside of all the functions
EX:
int n=500;
void main()
{
}
void fn1()
{
}
//global vs local variable
int x=50;
void main()
{
int x=100;
printf(“%d”,x);
}
void fn1()
{
int x=600;
printf(“%d”,x);
}
• When Global and variable and local
variable have the same name, local
Variable Will have precedence over
Global
Using extern keyword
Extern Keyword can be used in the following method
void main()
{
extern int y; //External Variables cant be initialized
printf(“%d”,y+50); //100
fn();
}
void fn()
{
extern int y;
printf(“%d”,y); //50
}
int y=50;
Static Variable
 Static variables are the variables whose value will
persist till the end of the program
 Keyword used is static
 In other words the life of a static variable will be up to
the end of the program not end of the function
Example
Void inc();
Void main()
{
int k;
for(k=0; k<5; k++)
inc();
}
Void inc()
{
static int x=5;
printf(“X:%dn”,x+5);
}
Output:
X:10
X:15
X:20
X:25
X:30
Register variable
 A variable prefixed with keyword register will be placed
in register memory instead of main memory
 Register variables can be accessed faster than the
normal variable that is stored in main memory
Ex: register int x;
 Most frequently used variables can be declared as
register(Loop control variable)
End of Module-9

function in c

  • 1.
  • 2.
    Classifications of function Functions can be classified as Pre defined function: Function whose definitions have already been return inside c(Ex:printf()) User Defined Functions: Functions which are designed by the user{Ex:disp(),show()}
  • 3.
    Top- to –bottom modular programming :  Main Program Function 1 Function 2 Function 3 Function 2.1Function 1.1
  • 4.
    Understanding the Flow ofProgram #include<stdio.h> #include<conio.h> void main() { clrscr(); printf(“n” I am a Programmer ?”n"); getch(); }
  • 5.
    Understanding the Flow ofProgram  main() { ………….. ………….. display(); ………… ……….. return0; } display() { StatementBlock; }
  • 6.
    Class work  Writea C program that calculates Volume of cylinder using functions  Your user defined function’s name should be volume()  Get the values of Radius and height inside the user defined function  volume=( Π x r2 x h )
  • 7.
    Elements of userdefined function A user defined function must have the following elements  Function Declaration  Function definition  Function call
  • 8.
    Defining a function A function definition otherwise called as function implementation must have the following elements  Function name  Function type  List of parameters  Local variable declaration  Function statements  A return statement (for types other than void)
  • 9.
    General Form offunction function_type function_name(parameter list) { local variable declaration; executable statement 1; executable statement 2; . . . return statement; } Function header Function Body
  • 10.
    Formal parameters/ Arguments  Parametersare nothing but variables that are passed along with the functions  It is not mandatory to use parameters for every user defined functions Ex: void add(int a, int b) //a&b –Formal parameter { ….. ….. }
  • 11.
    Example using parameters voidadd(int x, int y); //declaration void main() { int x,y; scanf(“%d%d”,&x,&y); add(x,y); // function call//formal parameter } void add(int x,int y) //fn with arguments//actual parameter { int z; z=x+y; printf(“sum of %d and %d is %d”,x,y,z); } 11
  • 12.
    variety of thefunction calls add(20,5); add(y,10); add(10,z); add(p,q); add(p*5,q-10);
  • 13.
    Classifications of function Based on arguments and return type, functions are classified as four 1. Without argument without return value 2. with arguments and without return value 3. without argument and with return value 4. with value and with return value
  • 14.
    No argument andno return type void main() { void add(); add(); } void add() { … … }
  • 15.
    With argument andwithout return type void main() { void add(int a, int b); int p,q; scanf(“%d%d”,&p,&q); add(p,q); } void add(int x, int y) { … … }
  • 16.
    Without argument andwith return type void main() { int add(); add(); } int add() { … … return 0; }
  • 17.
    With argument andwith return type void main() { float add(int a, int b); int p,q; scanf(“%d%d”,&p,&q); add(p,q); } float add(int x, int y) { … … return 1; }
  • 18.
    Example Void main() { int a=15,b=20; voidadd(int , int ); add(a,b); // a and b actual parameters } add(int p, int q) // p and q Formal Parameters { int c; c=p,q; printf(“The sum of %d and %d is %d”,p,q,c); }
  • 19.
    Nesting of functions C Permits nesting of function Ex: void main() { fn1(); } void fn1() { fn2(); } void fn2() { fn3(); }
  • 20.
    Recursion  When afunction calls itself, then the process is known as recursive function  In other words, if the calling function and the called function are same, then the process is called as recursion and that function is called as recursive function Ex: void main() { Printf(“Main Function is Executed “); main(); }
  • 21.
    Use of recursion Ex: factorial(inti) { int f; if(i==1) return 1; else f=i*factorial(i-1); return(f); }
  • 22.
    Classification of variables- Storageclasses  Previously variables are classified based on their datatype, scope(Lifetime)  Variables can also be classified based on their storage classes  The Four storage classes are  Automatic  Static  External  Register
  • 23.
    Automatic Variable  Thisvariable is created when a function is created and will be destroyed at the end of the function  Automatic Variables are typically private(Local Variable)  If a storage class for a variable is not specified, the compiler by default will consider it as Automatic  The keyword used for specification of Automatic variable declaration is auto
  • 24.
    Example-Automatic Variable void main() { intv=50; printf(“%d”,v); } int fn1() { auto int v=700; printf(“%d”,v); } int fn2() { int v=17000; printf(“%d”,v); }
  • 25.
    External Variable  Externalvariables are nothing but global variables which can be accessed throughout the program  They are declared outside of all the functions EX: int n=500; void main() { } void fn1() { }
  • 26.
    //global vs localvariable int x=50; void main() { int x=100; printf(“%d”,x); } void fn1() { int x=600; printf(“%d”,x); } • When Global and variable and local variable have the same name, local Variable Will have precedence over Global
  • 27.
    Using extern keyword ExternKeyword can be used in the following method void main() { extern int y; //External Variables cant be initialized printf(“%d”,y+50); //100 fn(); } void fn() { extern int y; printf(“%d”,y); //50 } int y=50;
  • 28.
    Static Variable  Staticvariables are the variables whose value will persist till the end of the program  Keyword used is static  In other words the life of a static variable will be up to the end of the program not end of the function
  • 29.
    Example Void inc(); Void main() { intk; for(k=0; k<5; k++) inc(); } Void inc() { static int x=5; printf(“X:%dn”,x+5); } Output: X:10 X:15 X:20 X:25 X:30
  • 30.
    Register variable  Avariable prefixed with keyword register will be placed in register memory instead of main memory  Register variables can be accessed faster than the normal variable that is stored in main memory Ex: register int x;  Most frequently used variables can be declared as register(Loop control variable)
  • 31.