2. Content
Introduction
Syntax
Program
Types of Functions
How the Function works?
Function Prototypes
Advantages of Function
Conclusion
3. Introduction
Function is a name block of statement that are executed
when it is called and performs a specific task.
C program contain at least one function which is main().
Basically a function is used to divide a large program of c
in many subprogram.
7. Program
int main()
{
int n,d;
printf("enter a numbern");
scanf("%d",&n);
d=fac(n);
printf(“factorial = %d",d);
}
int fac(int n)
{
int f;
for(f=1;n>=1;n--)
f=f*n;
return(f);
}
8. Types of Functions
They are two types of function:
1. Pre define function (Library function)
2. User define function
9. Pre define function (Library function)
A function that has already be define in the library of ‘C’
language or library developed by other programmer is
called Pre define function (Library function).
Ex:-
scanf, printf, puts, gets, etc.
10. User define function
A function define by the programmer itself that are called
User define function and it can be n number of function.
11. How the Function works?
“C” program doesn't execute the statement in function until the
function is called.
When function is called the program can send the function
information in the form of one or more argument.
When the function is used it is referred to as the called
function.
Functions often use data that is passed to them from the calling
function.
12. Cont..
Data is passed from the calling function to a called function by
specifying the variables in a argument list.
Argument list cannot be used to send data. Its only copy
data/value/variable that pass from the calling function.
The called function then performs its operation using the copies
and either it may be return some value or not.
13. Function Prototypes
Provides the compiler with the description of functions that
will be used later in the program
Its define the function before it been used/called
Function prototypes need to be written at the beginning of the
program.
The function prototype must have : a return type indicating the
variable that the function will be return
Syntax for Function Prototype
return_type function_name(parameter _list);
14. Advantages of Function
It is much easier to write a structured program
where a large program can be divided into a
functions.
Allowing the code to be called many times
Easier to read and update
It is easier to debug a structured program and easy
to find and fix error.
15. Conclusion
Function is a name block of statement which is used to performs
a specific task. A function is either call by value or by reference.
They are two types of function pre define or user define. A
function name is must be unique and it can be n no. of function
which can be called n no. times means there is no limit. For using
a function it is easy to write it, test it, and debug it.