Presentation Transcript
Slide 1:Functions
Function:
The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use.
Function
Library User define
function function
Slide 2:Library function:
The library function are not required to be written by us.
Eg:
printf
scanf
Slide 3:User define function:
This function as to be developed by the user at the time of writing a program.
Eg: main()
‘main’ is specially used function in C. Ever program must have main function to indicate, where the program begins its execution. When a program to large and complex then the result of debugging testing and maintaining becomes difficult.
Slide 4:Advantages:
It felicitate top down modular program.
The length of the source program can be reduce by using functions at
It is easy to locate and isolate and fault function easily
A function can used by many other program, it means programmer built an what have already done, insert of starting over scratch.
Slide 5:Main program
Function 1 function 2 function3 function4
Slide 6:Structure of a Function:
There are two main parts of the function. The function header and the function body.
int sum(int x, int y)
{
int ans = 0; //holds the answer that will be returned
ans = x + y; //calculate the sum
return ans //return the answer
}
Slide 7:Function Header:
In the first line of the above code
int sum(int x, int y)
It has three main parts
The name of the function i.e. sum
The parameters of the function enclosed in paranthesis
Return value type i.e. int
Slide 8:Function Body
What ever is written with in { } in the above example is the body of the function.
Function Prototypes
The prototype of a function provides the basic information about a function which tells the compiler that the function is used correctly or not. It contains the same information as the function header contains. The prototype of the function in the above example would be like
int sum (int x, int y);
The only difference between the header and the prototype is the semicolon ;, there must the a semicolon at the end of the prototype.
Slide 9:Program:
#include
add(a,b)
int a;
int b;
int c;
{
c=a+b;
return c;
}
main()
{
int c;
c=add(10,20);
printf();
}