Function

mshoaib15
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;
}
1 von 15

Recomendados

lets play with "c"..!!! :):) von
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)Rupendra Choudhary
594 views23 Folien
Functions in C von
Functions in CFunctions in C
Functions in CPrincy Nelson
444 views21 Folien
Dti2143 chapter 5 von
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
736 views38 Folien
C programming function von
C  programming functionC  programming function
C programming functionargusacademy
1.2K views17 Folien
C function von
C functionC function
C functionthirumalaikumar3
493 views26 Folien
Functions in c von
Functions in cFunctions in c
Functions in cInnovative
1.1K views18 Folien

Más contenido relacionado

Was ist angesagt?

Function & Recursion in C von
Function & Recursion in CFunction & Recursion in C
Function & Recursion in CAditya Nihal Kumar Singh
251 views15 Folien
Function in c von
Function in cFunction in c
Function in csavitamhaske
1.3K views18 Folien
Function in c von
Function in cFunction in c
Function in cCGC Technical campus,Mohali
656 views22 Folien
C standard library functions von
C standard library functionsC standard library functions
C standard library functionsVaishnavee Sharma
2.5K views45 Folien
Lecture 11 - Functions von
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - FunctionsMd. Imran Hossain Showrov
52 views20 Folien
Unit 4 (1) von
Unit 4 (1)Unit 4 (1)
Unit 4 (1)psaravanan1985
73 views33 Folien

Was ist angesagt?(20)

Function in c program von umesh patil
Function in c programFunction in c program
Function in c program
umesh patil1.3K views
C Prog - Functions von vinay arora
C Prog - FunctionsC Prog - Functions
C Prog - Functions
vinay arora1.2K views
Functions and pointers_unit_4 von Saranya saran
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran82 views
46630497 fun-pointer-1 von AmIt Prasad
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
AmIt Prasad253 views
Functions in c language von tanmaymodi4
Functions in c language Functions in c language
Functions in c language
tanmaymodi42.6K views
Types of function call von ArijitDhali
Types of function callTypes of function call
Types of function call
ArijitDhali390 views

Similar a Function

Functions struct&union von
Functions struct&unionFunctions struct&union
Functions struct&unionUMA PARAMESWARI
115 views33 Folien
Functions von
Functions Functions
Functions Dr.Subha Krishna
168 views25 Folien
cp Module4(1) von
cp Module4(1)cp Module4(1)
cp Module4(1)Amarjith C K
529 views47 Folien
Unit 3 (1) von
Unit 3 (1)Unit 3 (1)
Unit 3 (1)Sowri Rajan
17 views28 Folien
Functionincprogram von
FunctionincprogramFunctionincprogram
FunctionincprogramSampath Kumar
44 views24 Folien
CHAPTER 6 von
CHAPTER 6CHAPTER 6
CHAPTER 6mohd_mizan
559 views19 Folien

Similar a Function(20)

Functions and pointers_unit_4 von MKalpanaDevi
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi153 views
Programming Fundamentals Functions in C and types von imtiazalijoono
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
imtiazalijoono510 views
User defined functions in C von Harendra Singh
User defined functions in CUser defined functions in C
User defined functions in C
Harendra Singh17.1K views
VIT351 Software Development VI Unit1 von YOGESH SINGH
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
YOGESH SINGH64 views
Presentation on Function in C Programming von Shuvongkor Barman
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman14.4K views
CH.4FUNCTIONS IN C_FYBSC(CS).pptx von SangeetaBorde3
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde329 views

Más de mshoaib15

Msc prev completed von
Msc prev completedMsc prev completed
Msc prev completedmshoaib15
24 views79 Folien
Msc prev updated von
Msc prev updatedMsc prev updated
Msc prev updatedmshoaib15
25 views76 Folien
Msc chemistry previous von
Msc chemistry previousMsc chemistry previous
Msc chemistry previousmshoaib15
313 views167 Folien
Bsc math previous exam quetions von
Bsc math previous exam quetionsBsc math previous exam quetions
Bsc math previous exam quetionsmshoaib15
12 views2 Folien
Computer lab (programs) von
Computer lab (programs)Computer lab (programs)
Computer lab (programs)mshoaib15
16 views7 Folien
What is algorithm von
What is algorithmWhat is algorithm
What is algorithmmshoaib15
80 views30 Folien

Más de mshoaib15(8)

Msc prev completed von mshoaib15
Msc prev completedMsc prev completed
Msc prev completed
mshoaib1524 views
Msc prev updated von mshoaib15
Msc prev updatedMsc prev updated
Msc prev updated
mshoaib1525 views
Msc chemistry previous von mshoaib15
Msc chemistry previousMsc chemistry previous
Msc chemistry previous
mshoaib15313 views
Bsc math previous exam quetions von mshoaib15
Bsc math previous exam quetionsBsc math previous exam quetions
Bsc math previous exam quetions
mshoaib1512 views
Computer lab (programs) von mshoaib15
Computer lab (programs)Computer lab (programs)
Computer lab (programs)
mshoaib1516 views
What is algorithm von mshoaib15
What is algorithmWhat is algorithm
What is algorithm
mshoaib1580 views
Boolean expression org. von mshoaib15
Boolean expression org.Boolean expression org.
Boolean expression org.
mshoaib154K views
Number system.... von mshoaib15
Number system....Number system....
Number system....
mshoaib159.1K views

Último

Kyo - Functional Scala 2023.pdf von
Kyo - Functional Scala 2023.pdfKyo - Functional Scala 2023.pdf
Kyo - Functional Scala 2023.pdfFlavio W. Brasil
368 views92 Folien
The Research Portal of Catalonia: Growing more (information) & more (services) von
The Research Portal of Catalonia: Growing more (information) & more (services)The Research Portal of Catalonia: Growing more (information) & more (services)
The Research Portal of Catalonia: Growing more (information) & more (services)CSUC - Consorci de Serveis Universitaris de Catalunya
80 views25 Folien
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... von
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...James Anderson
85 views32 Folien
Case Study Copenhagen Energy and Business Central.pdf von
Case Study Copenhagen Energy and Business Central.pdfCase Study Copenhagen Energy and Business Central.pdf
Case Study Copenhagen Energy and Business Central.pdfAitana
16 views3 Folien
Network Source of Truth and Infrastructure as Code revisited von
Network Source of Truth and Infrastructure as Code revisitedNetwork Source of Truth and Infrastructure as Code revisited
Network Source of Truth and Infrastructure as Code revisitedNetwork Automation Forum
26 views45 Folien

Último(20)

GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... von James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson85 views
Case Study Copenhagen Energy and Business Central.pdf von Aitana
Case Study Copenhagen Energy and Business Central.pdfCase Study Copenhagen Energy and Business Central.pdf
Case Study Copenhagen Energy and Business Central.pdf
Aitana16 views
Five Things You SHOULD Know About Postman von Postman
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About Postman
Postman33 views
handbook for web 3 adoption.pdf von Liveplex
handbook for web 3 adoption.pdfhandbook for web 3 adoption.pdf
handbook for web 3 adoption.pdf
Liveplex22 views
STPI OctaNE CoE Brochure.pdf von madhurjyapb
STPI OctaNE CoE Brochure.pdfSTPI OctaNE CoE Brochure.pdf
STPI OctaNE CoE Brochure.pdf
madhurjyapb14 views
Data Integrity for Banking and Financial Services von Precisely
Data Integrity for Banking and Financial ServicesData Integrity for Banking and Financial Services
Data Integrity for Banking and Financial Services
Precisely21 views
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors von sugiuralab
TouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective SensorsTouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective Sensors
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors
sugiuralab19 views
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ... von Jasper Oosterveld
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
Serverless computing with Google Cloud (2023-24) von wesley chun
Serverless computing with Google Cloud (2023-24)Serverless computing with Google Cloud (2023-24)
Serverless computing with Google Cloud (2023-24)
wesley chun11 views
Empathic Computing: Delivering the Potential of the Metaverse von Mark Billinghurst
Empathic Computing: Delivering  the Potential of the MetaverseEmpathic Computing: Delivering  the Potential of the Metaverse
Empathic Computing: Delivering the Potential of the Metaverse
Mark Billinghurst478 views

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; }