Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Function
Defination: A function is a self-contained subprogram that performs some specific, well-defined
task.
C program consists of one or more functions
If a program has only one function then it must be the main() function
It is first function to be executed when the program starts, all other functions are called Directly
or Indirectly.
Advantage:
1. Generally a difficult problem is divided into sub problems and solved.This divided and
conquer technique is implemented in C through functions
A Program can be divided into functions, each of which performs some specific task.
Thus the use of function modularizes and divides the work of a program.
2. Repetition: When some specific code is to used more than once and at diffferent places in the
program the use of functions avoids repetitions of that code. The function can be written once
and called at different places in the program.
3. What: The program becomes easily understanable, modifiable and easy to debug and test.
It becomes simple to write the program and understand what work is done by each
part of the program. The detail of how the work is done are hidden inside appropriate functions
.This is called “Abstractions”.
4. Function can be stored in a library and can be reused .
Types of functions
Library Functions:
C has the facility to provide library functions for performing some operations
These functions are present in the c library and they are predefined.
Exp.
sqrt() Mathmetical library functions
printf() Output library functions
scanf() Input library functions
strlen() string manipulation
strcmp() string manipulation
To use a library function we have to include corresponding header file using the preprocessor
directive #include
Exp
Input/Output functions printf() and scanf() we have to include stdio.h
for mathematics library functions we have to include math.h
for string library funcitons string.h should be included
#include<stdio.h>
#include<conio.h>
#include<math.h>
Int main()
{
double n,s;
printf(“lf”,&n);
s = sqrt(n);
printf(“Square root of %lf is lfn”,n,s );
return 0;
User-defined functions: Programmers can create their own functions for performing any specific
task of a program.
These types of functions are called “User Defined” functions
Three things
1. Function definition
2. Function declaration
3. Function call
/* Program to draw a line*/
#include<stdio.h>
void drawline(void); /* Function declaration*/
int main()
{
drawline(); /* Function call*/
return 0;
}
void drawline(void) /* Function Defination*/
{
int i;
for(i=0;i<=80;i++)
printf(“_”);
}
/* Program to find sum of two numbers*/
#include<stdio.h>
int sum(int x, int y); /* Function declaration*/
int main()
{
int a,b,s;
printf(“Enter values for a and b: ”);
scanf(“%d%d”,&a, &b);
s = sum(a,b); /* Function call*/
printf(“Sum of %d and %d is %dn”, a,b,c);
return 0;
}
int sum(int x, int y) /* Function Defination*/
{
int s;
s = x+y;
return s;
}
/* Program to find sum of two numbers*/
#include<stdio.h>
int sum(int x, int y); /* Function declaration*/
int main()
{
int a=10,b=20,k;
k = sum(a,b); /* Function call*/
printf(“%dn”,k);
k = sum(4,5);
printf(“%dn”,k);
k = sum(a+b,b*2);
printf(“%dn”,k);
return 0;
}
int sum(int x, int y) /* Function Defination*/
{
int s;
s = x+y;
return s;
}
/* Program to find larger number*/
#include<stdio.h>
int larger(int x, int y); /* Function declaration*/
int main()
{
int x,y;
printf(“Enter any two numbers: ”);
scanf(“%d%d”,&x, &y);
printf(“Larger no. = %dn”,larger(x,y));
return 0;
}
int larger(int x, int y) /* Function Defination*/
{
return x>y?x:y;
}
Function Parameters and Arguments: We can classify functions into four types depending on the
return value and arguments :
1. Function with No Arguments And No Return value
2. Function with No Arguments But Return value
3. Function with Arguments But No Return value
Function with Arguments And Return value
1. Function with No Arguments And No Return value
syntax:
void func(void);
int main(void)
{
Statement;
func();
Statement;
}
void func()
{
Statement;
Statement;
}
The func() is called by main() and function definition is written after the main() function.
Func() has no arguments, main can not send any data to func() and since it has no return
statement, function can not return any value to main().
There is no communication between the calling and called function.
Since there is no return value, these types of functions can not be used as operands in example
/* Function with No Arguments And No Return value */
#include<stdio.h>
void displaymenu(void);
int main(void)
{
int choice;
displaymenu();
printf(“Enter your choice:”);
scanf(“%d”,&choice);
return 0;
}
void displaymenu(void)
{
printf(“1. Create databasen”);
printf(“2. Insert new recordn”);
printf(“3. Modify a recordn”);
printf(“4. Delete a recordn”);
printf(“5. display all recordsn”);
printf(“6. Exit n”);
}
2. Function with No Arguments But A Return value :
These types of functions do not receive any arguments but they can return a value:
syntax:
int func(void);
int main(void)
{
int r;
Statement;
r = func();
return 0;
}
int func(void)
{
Statement;
Statement;
return expression;
}
#include<stdio.h>
int func(void);
int main(void)
{
printf(“%dn”,func());
return 0;
}
/*Sum of square of all odd numbers from 1 to 25*/
int func(void)
{
int num,s=0;
for(num=1;num<=25;num++)
{
if(num%2!=0)
s+= num*num;
}
return s;
}
3. Function with Arguments But No Return value :
These type of functions have arguments, so the calling function can send data to the called
function but the called function does not return any value
Syntax :
void func(int, int);
int main(void)
{
Statement;
func(a,b);
Statement;
return 0;
}
void func(intc, int d)
{
Statement;
Statement;
}
#include<stdio.h>
#include<math.h>
void type(float a, float b, float c);
void area(float a, float b, float c);
int main(void)
{
float a,b,c;
printf(“ Enter sides of Triangle : ”);
scanf(“%f%f%f”,&a,&b,&c);
if( a<b+c && b<c+a && c<a+b)
{
type(a,b,c);
area(a,b,c);
}
else
{
printf(“No Triangle is possible these sidesn”);
}
return 0;
}
void type(float a, float b, float c)
{
if( (a*a) + (b*b) == (c*c) || (b*b) + (c*c) == (a*a) || (c*c) + (a*a) == (b*b) )
{
printf(“Right Angle Trianglen”);
}
if(a==b && b==c)
{
printf(“The Triangle is Equiliteral trianglen”);
}
else if(a==b || b==c || c==a)
{
printf(“Isosceles Trianglen”);
}
else
{
printf(“Scalene n”);
}
}
void area(float a, float b, float c)
{
float s,area;
s = (a+b+c)/2;
area = sqrt(s*(s-a)(s-b)(s-c));
printf(“The Area of triangle is = %fn”, area);
4. Function with Arguments And Return value :
These type of functions have arguments, so the calling function can send data to the called
function. The called function return any value to the calling function using return statement.
Syntax :
int func(int, int);
int main()
{
int r;
statement;
statement;
r = func(a,b);
statement;
func(c,d);
return 0;
}
int func(int a, int b)
{
statement;
statement;
statement;
return expression;
}
#include<stdio.h>
int sum(int n);
int main()
{
int num;
printf(“Enter a Number : ”);
scanf(“%d”,&num);
printf(“Sum of digits of %d is %d n”, num,sum(num));
return 0;
}
int sum (int n)
{
int sum = 0;
while(n>0)
{
sum += n%10;
n /= 10;
}
return sum;
}

More Related Content

Function

  • 1. Function Defination: A function is a self-contained subprogram that performs some specific, well-defined task. C program consists of one or more functions If a program has only one function then it must be the main() function It is first function to be executed when the program starts, all other functions are called Directly or Indirectly. Advantage: 1. Generally a difficult problem is divided into sub problems and solved.This divided and conquer technique is implemented in C through functions A Program can be divided into functions, each of which performs some specific task. Thus the use of function modularizes and divides the work of a program. 2. Repetition: When some specific code is to used more than once and at diffferent places in the program the use of functions avoids repetitions of that code. The function can be written once and called at different places in the program. 3. What: The program becomes easily understanable, modifiable and easy to debug and test. It becomes simple to write the program and understand what work is done by each part of the program. The detail of how the work is done are hidden inside appropriate functions .This is called “Abstractions”. 4. Function can be stored in a library and can be reused . Types of functions
  • 2. Library Functions: C has the facility to provide library functions for performing some operations These functions are present in the c library and they are predefined. Exp. sqrt() Mathmetical library functions printf() Output library functions scanf() Input library functions strlen() string manipulation strcmp() string manipulation To use a library function we have to include corresponding header file using the preprocessor directive #include Exp Input/Output functions printf() and scanf() we have to include stdio.h for mathematics library functions we have to include math.h for string library funcitons string.h should be included #include<stdio.h> #include<conio.h> #include<math.h> Int main() { double n,s; printf(“lf”,&n); s = sqrt(n); printf(“Square root of %lf is lfn”,n,s ); return 0;
  • 3. User-defined functions: Programmers can create their own functions for performing any specific task of a program. These types of functions are called “User Defined” functions Three things 1. Function definition 2. Function declaration 3. Function call /* Program to draw a line*/ #include<stdio.h> void drawline(void); /* Function declaration*/ int main() { drawline(); /* Function call*/ return 0; } void drawline(void) /* Function Defination*/ { int i; for(i=0;i<=80;i++) printf(“_”); }
  • 4. /* Program to find sum of two numbers*/ #include<stdio.h> int sum(int x, int y); /* Function declaration*/ int main() { int a,b,s; printf(“Enter values for a and b: ”); scanf(“%d%d”,&a, &b); s = sum(a,b); /* Function call*/ printf(“Sum of %d and %d is %dn”, a,b,c); return 0; } int sum(int x, int y) /* Function Defination*/ { int s; s = x+y; return s; }
  • 5. /* Program to find sum of two numbers*/ #include<stdio.h> int sum(int x, int y); /* Function declaration*/ int main() { int a=10,b=20,k; k = sum(a,b); /* Function call*/ printf(“%dn”,k); k = sum(4,5); printf(“%dn”,k); k = sum(a+b,b*2); printf(“%dn”,k); return 0; } int sum(int x, int y) /* Function Defination*/ { int s; s = x+y; return s; }
  • 6. /* Program to find larger number*/ #include<stdio.h> int larger(int x, int y); /* Function declaration*/ int main() { int x,y; printf(“Enter any two numbers: ”); scanf(“%d%d”,&x, &y); printf(“Larger no. = %dn”,larger(x,y)); return 0; } int larger(int x, int y) /* Function Defination*/ { return x>y?x:y; }
  • 7. Function Parameters and Arguments: We can classify functions into four types depending on the return value and arguments : 1. Function with No Arguments And No Return value 2. Function with No Arguments But Return value 3. Function with Arguments But No Return value Function with Arguments And Return value 1. Function with No Arguments And No Return value syntax: void func(void); int main(void) { Statement; func(); Statement; } void func() { Statement; Statement; } The func() is called by main() and function definition is written after the main() function. Func() has no arguments, main can not send any data to func() and since it has no return statement, function can not return any value to main(). There is no communication between the calling and called function. Since there is no return value, these types of functions can not be used as operands in example
  • 8. /* Function with No Arguments And No Return value */ #include<stdio.h> void displaymenu(void); int main(void) { int choice; displaymenu(); printf(“Enter your choice:”); scanf(“%d”,&choice); return 0; } void displaymenu(void) { printf(“1. Create databasen”); printf(“2. Insert new recordn”); printf(“3. Modify a recordn”); printf(“4. Delete a recordn”); printf(“5. display all recordsn”); printf(“6. Exit n”); }
  • 9. 2. Function with No Arguments But A Return value : These types of functions do not receive any arguments but they can return a value: syntax: int func(void); int main(void) { int r; Statement; r = func(); return 0; } int func(void) { Statement; Statement; return expression; }
  • 10. #include<stdio.h> int func(void); int main(void) { printf(“%dn”,func()); return 0; } /*Sum of square of all odd numbers from 1 to 25*/ int func(void) { int num,s=0; for(num=1;num<=25;num++) { if(num%2!=0) s+= num*num; } return s; }
  • 11. 3. Function with Arguments But No Return value : These type of functions have arguments, so the calling function can send data to the called function but the called function does not return any value Syntax : void func(int, int); int main(void) { Statement; func(a,b); Statement; return 0; } void func(intc, int d) { Statement; Statement; }
  • 12. #include<stdio.h> #include<math.h> void type(float a, float b, float c); void area(float a, float b, float c); int main(void) { float a,b,c; printf(“ Enter sides of Triangle : ”); scanf(“%f%f%f”,&a,&b,&c); if( a<b+c && b<c+a && c<a+b) { type(a,b,c); area(a,b,c); } else { printf(“No Triangle is possible these sidesn”); } return 0; }
  • 13. void type(float a, float b, float c) { if( (a*a) + (b*b) == (c*c) || (b*b) + (c*c) == (a*a) || (c*c) + (a*a) == (b*b) ) { printf(“Right Angle Trianglen”); } if(a==b && b==c) { printf(“The Triangle is Equiliteral trianglen”); } else if(a==b || b==c || c==a) { printf(“Isosceles Trianglen”); } else { printf(“Scalene n”); } } void area(float a, float b, float c) { float s,area; s = (a+b+c)/2; area = sqrt(s*(s-a)(s-b)(s-c)); printf(“The Area of triangle is = %fn”, area);
  • 14. 4. Function with Arguments And Return value : These type of functions have arguments, so the calling function can send data to the called function. The called function return any value to the calling function using return statement. Syntax : int func(int, int); int main() { int r; statement; statement; r = func(a,b); statement; func(c,d); return 0; } int func(int a, int b) { statement; statement; statement; return expression; }
  • 15. #include<stdio.h> int sum(int n); int main() { int num; printf(“Enter a Number : ”); scanf(“%d”,&num); printf(“Sum of digits of %d is %d n”, num,sum(num)); return 0; } int sum (int n) { int sum = 0; while(n>0) { sum += n%10; n /= 10; } return sum; }