SlideShare a Scribd company logo
1 of 22
Programming in C
Module IV
Rajagiri School of Engineering &
Technology
โ€ข Functions
โ€ข Elements of User Defined Function
โ€ข Methods of calling a Function
Contents
Module 4 2
Functions
โ€ข You can divide up your code into separate functions. Logically
the division is such that each function performs a specific task.
โ€ข A function can also be referred as a method or a sub-routine or
a procedure.
Functions
Module 4 4
A function is a group of statements that together perform a task.
1. Function Prototype (function declaration)
2. Function Definition
3. Function Call
Elements of User Defined Function
Module 4 5
โ€ข A function declaration tells the compiler about a function
name and how to call the function.
โ€ข Syntax:
โ€ข Example:
void xyz(int, char,float);
1.
2.
1. Function Declaration
Module 4 6
returntype function_name (Parameter List);
โ€ข Contains all the statements to be executed.
2.Function Definition
Module 4 7
returntype function_name (parameter list)
{
//body of the function
}
Example
Module 4 8
โ€ข When a program calls a function, the program control
is transferred to the called function.
3. Function Call
Module 4 9
function_name(arguments list);
Module 4 10
int max(int num1,int num2);
void main()
{
int a=100,b=200,ret;
ret=max(a,b);
printf(โ€œthe max value is %dโ€,ret);
}
int max(int num1,int num2)
{
int result;
If(num1>num2)
result=num1;
Else result=num2;
return result;
}
Example
Module 4 11
Example
Module 4 12
1. Function with no arguments and no return value
2. Function with no arguments and a return value
3. Function with arguments and no return value
4. Function with arguments and a return value
Categories of User-defined Functions
Module 4 13
1. Function with no arguments and no return value
Module 4 14
Function Call
Module 4 15
2. Function with no arguments and a return value
3. Function with argument and no return value
Module 4 16
4. Function with argument and return value
Module 4 17
โ€ข Call by Value
โ€ข Call by Reference
Methods of calling a Function
Module 4 18
โ€ข In call by value method, the value of the variable is passed to
the function as parameter.
โ€ข The value of the actual parameter can not be modified by
formal parameter.
โ€ข Different Memory is allocated for both actual and formal
parameters.
Call by value method
Module 4 19
#include<stdio.h>
// function declaration
void swap(int a, int b);
int main()
{
int m = 22, n = 44;
printf(" values before swap m = %d n and n = %d", m, n);
swap(m, n);
}
void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" nvalues after swap m = %dn and n = %d", a, b);
}
Example
Module 4 20
โ€ข Pass Individual Array Elements
โ€ข Pass Arrays to Functions
โ€“ Function Prototype: void pass(int []);
โ€“ Function call: pass(a);
โ€“ Function definition:
โ€ข void pass(int b[])
โ€“ {for(int i=0;i<3;i++)
โ€“ printf("%dn",b[i]);}
Pass arrays to a function in C
Module 4 21
//pass second and third elements to display()
display(ageArray[1], ageArray[2]);
Function Definition
void display(int age1, int age2)
{ // code }
22
Module 4

More Related Content

Similar to functions (1).pptx

CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxGebruGetachew2
ย 
Function
FunctionFunction
FunctionSaniati
ย 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxKhurramKhan173
ย 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04Terry Yoast
ย 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04Terry Yoast
ย 
Savitch ch 04
Savitch ch 04Savitch ch 04
Savitch ch 04Terry Yoast
ย 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
ย 
Functions.pptx
Functions.pptxFunctions.pptx
Functions.pptxAkshayKumarK14
ย 
Unit 8
Unit 8Unit 8
Unit 8rohassanie
ย 
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptManivannan837728
ย 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functionsHattori Sidek
ย 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&unionUMA PARAMESWARI
ย 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptAmanuelZewdie4
ย 
Presentation of computer
Presentation of computerPresentation of computer
Presentation of computerSabinDhakal13
ย 

Similar to functions (1).pptx (20)

CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
ย 
Function
FunctionFunction
Function
ย 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
ย 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
ย 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04
ย 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04
ย 
Savitch ch 04
Savitch ch 04Savitch ch 04
Savitch ch 04
ย 
Functions
FunctionsFunctions
Functions
ย 
functions
functionsfunctions
functions
ย 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
ย 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
ย 
Functions.pptx
Functions.pptxFunctions.pptx
Functions.pptx
ย 
Unit 8
Unit 8Unit 8
Unit 8
ย 
Function (rule in programming)
Function (rule in programming)Function (rule in programming)
Function (rule in programming)
ย 
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
ย 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
ย 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
ย 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
ย 
Presentation of computer
Presentation of computerPresentation of computer
Presentation of computer
ย 
Function
Function Function
Function
ย 

More from ssuser47f7f2

Lecture7x.ppt
Lecture7x.pptLecture7x.ppt
Lecture7x.pptssuser47f7f2
ย 
NORMAL-FORMS.ppt
NORMAL-FORMS.pptNORMAL-FORMS.ppt
NORMAL-FORMS.pptssuser47f7f2
ย 
09.LearningMaterial_Sample.pdf
09.LearningMaterial_Sample.pdf09.LearningMaterial_Sample.pdf
09.LearningMaterial_Sample.pdfssuser47f7f2
ย 
Module 2_Conditional Statements.pptx
Module 2_Conditional Statements.pptxModule 2_Conditional Statements.pptx
Module 2_Conditional Statements.pptxssuser47f7f2
ย 
03-FiniteAutomata.pptx
03-FiniteAutomata.pptx03-FiniteAutomata.pptx
03-FiniteAutomata.pptxssuser47f7f2
ย 
15159222.ppt
15159222.ppt15159222.ppt
15159222.pptssuser47f7f2
ย 
FiniteAutomata (1).ppt
FiniteAutomata (1).pptFiniteAutomata (1).ppt
FiniteAutomata (1).pptssuser47f7f2
ย 
Module 1-System Software PROGRAMS.pptx
Module 1-System Software PROGRAMS.pptxModule 1-System Software PROGRAMS.pptx
Module 1-System Software PROGRAMS.pptxssuser47f7f2
ย 
module1 network security.pdf
module1 network security.pdfmodule1 network security.pdf
module1 network security.pdfssuser47f7f2
ย 

More from ssuser47f7f2 (10)

rs1.ppt
rs1.pptrs1.ppt
rs1.ppt
ย 
Lecture7x.ppt
Lecture7x.pptLecture7x.ppt
Lecture7x.ppt
ย 
NORMAL-FORMS.ppt
NORMAL-FORMS.pptNORMAL-FORMS.ppt
NORMAL-FORMS.ppt
ย 
09.LearningMaterial_Sample.pdf
09.LearningMaterial_Sample.pdf09.LearningMaterial_Sample.pdf
09.LearningMaterial_Sample.pdf
ย 
Module 2_Conditional Statements.pptx
Module 2_Conditional Statements.pptxModule 2_Conditional Statements.pptx
Module 2_Conditional Statements.pptx
ย 
03-FiniteAutomata.pptx
03-FiniteAutomata.pptx03-FiniteAutomata.pptx
03-FiniteAutomata.pptx
ย 
15159222.ppt
15159222.ppt15159222.ppt
15159222.ppt
ย 
FiniteAutomata (1).ppt
FiniteAutomata (1).pptFiniteAutomata (1).ppt
FiniteAutomata (1).ppt
ย 
Module 1-System Software PROGRAMS.pptx
Module 1-System Software PROGRAMS.pptxModule 1-System Software PROGRAMS.pptx
Module 1-System Software PROGRAMS.pptx
ย 
module1 network security.pdf
module1 network security.pdfmodule1 network security.pdf
module1 network security.pdf
ย 

Recently uploaded

Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
ย 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
ย 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
ย 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
ย 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
ย 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
ย 
Top Rated Call Girls In chittoor ๐Ÿ“ฑ {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor ๐Ÿ“ฑ {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor ๐Ÿ“ฑ {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor ๐Ÿ“ฑ {7001035870} VIP Escorts chittoordharasingh5698
ย 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
ย 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfRagavanV2
ย 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
ย 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
ย 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
ย 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
ย 
Top Rated Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...Call Girls in Nagpur High Profile
ย 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
ย 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
ย 

Recently uploaded (20)

Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
ย 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
ย 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
ย 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
ย 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
ย 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
ย 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
ย 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
ย 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
ย 
Top Rated Call Girls In chittoor ๐Ÿ“ฑ {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor ๐Ÿ“ฑ {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor ๐Ÿ“ฑ {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor ๐Ÿ“ฑ {7001035870} VIP Escorts chittoor
ย 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ย 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
ย 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ย 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
ย 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
ย 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
ย 
Top Rated Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
ย 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
ย 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
ย 

functions (1).pptx

  • 1. Programming in C Module IV Rajagiri School of Engineering & Technology
  • 2. โ€ข Functions โ€ข Elements of User Defined Function โ€ข Methods of calling a Function Contents Module 4 2
  • 4. โ€ข You can divide up your code into separate functions. Logically the division is such that each function performs a specific task. โ€ข A function can also be referred as a method or a sub-routine or a procedure. Functions Module 4 4 A function is a group of statements that together perform a task.
  • 5. 1. Function Prototype (function declaration) 2. Function Definition 3. Function Call Elements of User Defined Function Module 4 5
  • 6. โ€ข A function declaration tells the compiler about a function name and how to call the function. โ€ข Syntax: โ€ข Example: void xyz(int, char,float); 1. 2. 1. Function Declaration Module 4 6 returntype function_name (Parameter List);
  • 7. โ€ข Contains all the statements to be executed. 2.Function Definition Module 4 7 returntype function_name (parameter list) { //body of the function }
  • 9. โ€ข When a program calls a function, the program control is transferred to the called function. 3. Function Call Module 4 9 function_name(arguments list);
  • 11. int max(int num1,int num2); void main() { int a=100,b=200,ret; ret=max(a,b); printf(โ€œthe max value is %dโ€,ret); } int max(int num1,int num2) { int result; If(num1>num2) result=num1; Else result=num2; return result; } Example Module 4 11
  • 13. 1. Function with no arguments and no return value 2. Function with no arguments and a return value 3. Function with arguments and no return value 4. Function with arguments and a return value Categories of User-defined Functions Module 4 13
  • 14. 1. Function with no arguments and no return value Module 4 14 Function Call
  • 15. Module 4 15 2. Function with no arguments and a return value
  • 16. 3. Function with argument and no return value Module 4 16
  • 17. 4. Function with argument and return value Module 4 17
  • 18. โ€ข Call by Value โ€ข Call by Reference Methods of calling a Function Module 4 18
  • 19. โ€ข In call by value method, the value of the variable is passed to the function as parameter. โ€ข The value of the actual parameter can not be modified by formal parameter. โ€ข Different Memory is allocated for both actual and formal parameters. Call by value method Module 4 19
  • 20. #include<stdio.h> // function declaration void swap(int a, int b); int main() { int m = 22, n = 44; printf(" values before swap m = %d n and n = %d", m, n); swap(m, n); } void swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; printf(" nvalues after swap m = %dn and n = %d", a, b); } Example Module 4 20
  • 21. โ€ข Pass Individual Array Elements โ€ข Pass Arrays to Functions โ€“ Function Prototype: void pass(int []); โ€“ Function call: pass(a); โ€“ Function definition: โ€ข void pass(int b[]) โ€“ {for(int i=0;i<3;i++) โ€“ printf("%dn",b[i]);} Pass arrays to a function in C Module 4 21 //pass second and third elements to display() display(ageArray[1], ageArray[2]); Function Definition void display(int age1, int age2) { // code }