You are on page 1of 13

Functions are the building blocks of C in

which all program activity occurs.

A Function code is a private to that function


and cannot be accessed by any statement in
any other function except through a call to
that function.

Function can be used as call by value and


also can be used as call by reference.
It is independent statements, which can be
utilized, whenever it required.

Sub-programs are called complete because


these are the programs which have the
global as well as local declaration statement,
executable statement and function calling
statement like main program.
Function or Sub-Program

Library Function User-Defined Function


or or
Standard-in-built Function Self-contained Program
or or
Compiler Function Call-by-value & Call-by-reference Function
(like sqrt(),pow(),tan(),etc.) (like addition(),factorial() etc.
Functions makes the length and complex program
easy and in short forms
The length of source program can be reduced by
using function by using it at different places in the
program.
By using function,memory space can be properly
utilized.
Funtion can be used by many program.
Function increases the execution speed of the program.
It removes the redundancy and saves the time and space
Debugging becomes very easier.
Function are more flexible than library functions.
Testing is very easy.

Divide and conquer


Manageable program development

Software reusability
Use existing functions as building blocks for new
programs
Abstraction - hide internal details (library
functions)
int function1(); /*function declaration*/
main()
{
function1(); /*function call */
}
int function1() /*function definition */
{
..

return();
}
Returning statement
Returning control
If nothing returned
return;
or, until reaches right brace
If something returned
return(expression);
5.6 Function Prototypes
Function prototype
Function name
Parameters what the function takes in
Return type data type function returns (default
int)
Used to validate functions
Prototype only needed if function definition comes
after use in program
The function with the prototype
int maximum( int, int, int );
Takes in 3 ints
Returns an int
Promotion rules and conversions
Converting to lower types can lead to errors
Every function sub-program should have
last statement return statement.
Every function calling statement ends with
semicolon(;)
Called statement should not have
semicolon(;).
Every function is enclosed within { and }
similar to the main program.
Function with no argument and no return
value
Function with argument and no return value
Function with argument and return value
Function with no argument and no return value
#include<stdio.h>
#include<conio.h>
void printline();
void si();
void main()
{
printline();
si();
printline();
getch();
}
void printline()
{ for(int i=1;i<=40;i++)
{
printf(-);
}
}
Void si()
{
int p,r,t,s;
printf(Enter the value for p,r,t,s:\n);
s=(p*r*t)/100;
printf(\nSimple interest=%d,s);
}
#include<stdio.h>
#include<conio.h>
void si(int,int,int);
void main()
{
int p,r,t,s;
print(Enter the value for p,r,t,s:\n);
scanf(%d %d %d,&p,&r,&t);
si(p,r,t,s);
getch();
}
Void si(int x,int y,int z)

{
int m;
m=(x*y*z)/100;
printf(\nSimple Interest is:%d,m);

}
#include<stdio.h>
#include<conio.h>
int simple(int,int,int);
void main()
{
int p,r,t,si; int simple(x,y,z) // formal
parameter
printf(Enter the value for p,r,t); {
int x,y,z;
scanf(%d %d %d %d,&p,&r,&t);
int m=(x*y*z)/100;
return(m);
si=simple(p,r,t,s); / /actual parameter
}

printf(Simple interest is %d",si);

getch();
}

You might also like