Function
Function
BCAC 102
USING
{C}
Functions Programming
Sudip Barik
Department of Computer Application
Techno International New Town
What is Function?
A function is a group of statements that perform a specific task.
It divides a large program into smaller parts.
A function is something like hiring a person to do a specific job for you.
Every C program can be thought of as a collection of these functions.
Program execution in C language starts from the main function.
Syntax
void main()
{
// body part
}
Why function ?
Avoids rewriting the same code over and over.
Using functions it becomes easier to write programs and keep track of what they doing.
Sudip Barik BCAC 102 (PPS) – Functions 2
Types of Function
Function
void main()
{
....
func1(); Function call
}
void func1()
{
.... Function definition
//function body
....
}
Syntax Example
return-type function-name (arg-1, arg 2, …); void addition(int, int);
Syntax Example
return-type function-name (arg-1, arg 2, …) void addition(int x, int y)
{ {
//... Function body printf("Addition is=%d“,(x+y));
} }
Syntax Example
Declaration
return-type function-name (arg-1, arg 2, …); void addition(int, int);
Definition
return-type function-name (arg-1, arg 2, …) void addition(int x, int y)
{ {
//... Function body printf("Addition is=%d“,(x+y));
} }
Syntax
return;
Or
return (expression);
Output
Enter the number :7
The number 7 is a prime number.
Storage Initial
Storage Scope Life Example
Specifier Value
Automatic int a;
Stack Garbage Within block End of block
{auto} auto int a;
Till end of
Static Data Zero Within block static extern int var;
program
{static} segment static int var;
Thank you