Sie sind auf Seite 1von 13

FUNCTIONS

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 cab be used as call b reference.

SUBPROGRAMS OR USERDEFINED FUNCTIONS

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.

FUNCTIONS MAINLY OF 2 TYPES


Function or Sub-Program

Library Function
or
Standard-in-built Function
or
Compiler Function
(like sqrt(),pow(),tan(),etc.)

User-Defined Function
or
Self-contained Program
or
Call-by-value & Call-by-reference Function
(like addition(),rajan(),factorial() etc.

ADVANTAGES OF FUNCTION
SUB-PROGRAM

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.

Funcion 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 funcions.
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

.IMPORTANT POINT TO NOTE

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.

CATEGORIES OF FUNCTIONS

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();
}
printline()
{
for(int i=1;i<=40;i++)
{
printf(-);
}
}
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);
return;
}

FUNCTION WITH ARGUMENT AND NO RETURN


VALUE
#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); /*formal parameters*/
getch();
}
si(int x,int y,int z)
{
int m;
m=(x*y*z)/100;
printf(\nSimple Interest is:%d,m);
return;
}

FUNCTION WITH ARGUMENT AND RETURN


VALUE
#include<stdio.h>
#include<conio.h>
int simple(int,int,int);

void main()
{
int p,r,t,si;

printf(Enter the value for p,r,t);


scanf(%d %d %d %d,&p,&r,&t);
si=simple(p,r,t,s); / /actual parameter
printf(Simple interest is %d",si);
getch();
}

int simple(x,y,z) // formal


parameter
int x,y,z;
{
int m=(x*y*z)/100;
return(m);
}

Das könnte Ihnen auch gefallen