Professional Documents
Culture Documents
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
}
getch();
}