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
of Program
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“n” I am a Programmer ?”n");
getch();
}
5. Understanding the Flow
of Program
main()
{
…………..
………….. display();
…………
………..
return0;
}
display()
{
StatementBlock;
}
6. 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 )
7. Elements of user defined
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 of function
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
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
{
…..
…..
}
11. 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
12. variety of the function 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 and no return
type
void main()
{
void add();
add();
}
void add()
{
…
…
}
15. 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)
{
…
…
}
16. Without argument and with
return type
void main()
{
int add();
add();
}
int add()
{
…
…
return 0;
}
17. 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;
}
18. 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);
}
19. Nesting of functions
C Permits nesting of function
Ex:
void main()
{
fn1();
}
void fn1()
{
fn2();
}
void fn2()
{
fn3();
}
20. 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();
}
22. 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
23. 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
25. 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()
{
}
26. //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
27. 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;
28. 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
29. 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
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)