SlideShare une entreprise Scribd logo
1  sur  17
CSE 191 – COMPUTER PROGRAMMING
Najia Manjur
Lecturer
Department of Computer Science & Engineering (CSE)
Military Institute of Science & Technology (MIST)
Mirpur, Dhaka – 1216, Bangladesh
Introduction to Function
 A large program can be divided into many subprograms. These
subprograms can be called Functions.
 They allow us to reuse code instead of rewriting it.
 Functions allow us to test small parts of our program in isolation from the
rest.
 Save time and space of the code.
 Library Functions : printf( ), scanf( ), gets( ), pow( ) etc.
 User Defined Functions : main( )
2
Functions
 Function Definition.
 Function Call
 Function Declaration.
3
#include<stdio.h>
long cube(long x); //function prototype
long input,answer;
int main( )
{ printf(“Enter values: “);
scanf(“%d”,&input);
answer= cube(input); //function call
printf(“The cub of %ld is %ldn”,input,answer);
return 0; }
long cube(long x) //function definition
{ long x_cubed;
x_cubed = x*x*x;
return x_cubed; }
long cube(long x) function_return_type function_name (parameter
list)
{ function header
long x_cubed; //local variable
x_cubed = x*x*x; //executable statements function body
return x_cubed; //return statement
}
Function Definition
4
int main( )
{
printf(“Enter values: ”);
scanf(“%d”,&input);
answer= cube(input); //function call
printf(“The cub of %ld is %ldn”,input,answer);
return 0; }
Function Call
5
function_return_type function_name (parameter list);
long cube(long x);
Function Prototype
6
 parameter list must be separated by commas.
 parameter names do not need to be same in prototype declaration
and function definition.
 parameters must match in type, number and order.
 return type must be void when the function does not return a value.
 if declared types do not match with types in function definition,
compiler will produce an error.
Important
7
Category of Functions
 no arguments and no return values.
 Called function does not receive any data from calling function.
 Calling function does not receive any data from called function.
 arguments but no return value.
 Calling function sends values to called function using function call with proper
arguments
 Calling function does not receive any data from called function.
 argument and a return value.
 Called function receives data from calling function.
 Calling function receives a value from called function.
 no argument but a return value.
 Calling function sends no data to called function.
 Calling function receives a value from called function.
8
No Arguments and No Return Value
void sum_series (void) ; //declaration
int main( )
{ sum_series( ); // function call
return 0 ; }
void sum_series(void) // definition
{ int sum=0,i=1, n;
scanf(“%d”,&n);
for(i=1 ; i<=n ; i++)
sum+=i;
printf(“%d”,sum); }
9
Step1
Step2
Arguments but No Return Value
void multi(float p , float q); //declaration
int main( )
{ multi(2 .3 , 3 .4); // function call
return 0 ; }
void multi(float a , float b) // definition
{ float ans=0;
ans= a*b;
printf(“%.2f”,ans); }
10
Arguments and A Return Value
float average(float a, int b) ; //declaration
int main( )
{ float a=0;
a=average(2 . 5 , 5); // function call
printf(“%f”,a);
return 0 ; }
float average(float x, int y) // definition
{ float avg=0;
avg=(float) (2.5*5)/2;
return avg;
}
11
No Arguments but A Return Value
int get_number (void) ; //declaration
int main( )
{ int m=get_number( ); // function call
printf(“%d”,m);
return 0 ; }
int get_number(void) // definition
{ int number ;
scanf(“%d”,&number);
return number; }
12
Nested Function ( without declaration)
int isequal(int p, int q)
{ if(p!=q) return 1;
else return 0; }
void difference(int y, int z)
{ if(isequal(y , z))
printf(“%d”,y-z);
else printf(“No difference”); }
int main( )
{ int a, b; scanf(“%d %d”,&a, &b);
difference(a,b);
return 0; }
13
1
3
2
4
Nested Function ( with declaration)
void difference(int y, int z);
int isequal(int p, int q);
void difference(int y, int z)
{ if(isequal(y , z))
printf(“%d”,y-z);
else printf(“No difference”); }
int isequal(int p, int q)
{ if(p!=q) return 1;
else return 0; }
int main( )
{ int a, b; scanf(“%d %d”,&a, &b);
difference(a,b);
return 0; }
14
Array as an argument
void sort(int m, int x[ ]); //declaration
main( )
{
sort(5, marks); //function call
//marks is the array name sent from main
function to sort() function
}
void sort(int m, int x[ ]) // definition
{ //code }
15
Array as an argument
when a function changes the values of the elements of
an array, these changes will be made to the original
array that was passed to the function.
when an entire array is passed as an argument, the
contents of the array are not copied to the formal
parameter array, but information about the address of
array elements are passed on to the function.
so, any changes introduced to the array reflects on the
original array in the calling function.
16
End of Slides

Contenu connexe

Tendances

Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Dharma Kshetri
 
Presentation on function
Presentation on functionPresentation on function
Presentation on functionAbu Zaman
 
Call by value
Call by valueCall by value
Call by valueDharani G
 
Functions (Computer programming and utilization)
Functions (Computer programming and utilization)Functions (Computer programming and utilization)
Functions (Computer programming and utilization)Digvijaysinh Gohil
 
Types of function call
Types of function callTypes of function call
Types of function callArijitDhali
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions imtiazalijoono
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings imtiazalijoono
 
Function in c language(defination and declaration)
Function in c language(defination and declaration)Function in c language(defination and declaration)
Function in c language(defination and declaration)VC Infotech
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9Rumman Ansari
 

Tendances (20)

Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)
 
C function
C functionC function
C function
 
Function in c
Function in cFunction in c
Function in c
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Call by value
Call by valueCall by value
Call by value
 
Function in c
Function in cFunction in c
Function in c
 
functions in C
functions in Cfunctions in C
functions in C
 
Function
FunctionFunction
Function
 
C function presentation
C function presentationC function presentation
C function presentation
 
Function in c
Function in cFunction in c
Function in c
 
Functions (Computer programming and utilization)
Functions (Computer programming and utilization)Functions (Computer programming and utilization)
Functions (Computer programming and utilization)
 
Function lecture
Function lectureFunction lecture
Function lecture
 
functions
functionsfunctions
functions
 
Types of function call
Types of function callTypes of function call
Types of function call
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Functions
FunctionsFunctions
Functions
 
Function in c language(defination and declaration)
Function in c language(defination and declaration)Function in c language(defination and declaration)
Function in c language(defination and declaration)
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 

En vedette (20)

Luxury buyers still flocking to rhode island's coastline providence busines...
Luxury buyers still flocking to rhode island's coastline   providence busines...Luxury buyers still flocking to rhode island's coastline   providence busines...
Luxury buyers still flocking to rhode island's coastline providence busines...
 
Actividad 13 de edgar alan arguelles servin
Actividad 13 de edgar alan arguelles servinActividad 13 de edgar alan arguelles servin
Actividad 13 de edgar alan arguelles servin
 
Lunchroom.pl - nowoczesny benefit
Lunchroom.pl - nowoczesny benefitLunchroom.pl - nowoczesny benefit
Lunchroom.pl - nowoczesny benefit
 
Geotechnical
GeotechnicalGeotechnical
Geotechnical
 
Bok h p s
Bok h p sBok h p s
Bok h p s
 
Apoyo virtual
Apoyo virtualApoyo virtual
Apoyo virtual
 
Tabela de-parede-linha-leve-2012
Tabela de-parede-linha-leve-2012Tabela de-parede-linha-leve-2012
Tabela de-parede-linha-leve-2012
 
Análise corinthians paulista 2017 - final
Análise corinthians   paulista 2017 - finalAnálise corinthians   paulista 2017 - final
Análise corinthians paulista 2017 - final
 
Welltrado Pitch deck
Welltrado Pitch deckWelltrado Pitch deck
Welltrado Pitch deck
 
Ejercicios word otras operaciones
Ejercicios word otras operacionesEjercicios word otras operaciones
Ejercicios word otras operaciones
 
Progr in c lesson plan
Progr in c lesson planProgr in c lesson plan
Progr in c lesson plan
 
Lecture 01 2017
Lecture 01 2017Lecture 01 2017
Lecture 01 2017
 
Array within a class
Array within a classArray within a class
Array within a class
 
07slide
07slide07slide
07slide
 
Red en nube ventajas y desventajas
Red en nube ventajas y desventajasRed en nube ventajas y desventajas
Red en nube ventajas y desventajas
 
Programming in ansi C by Balaguruswami
Programming in ansi C by BalaguruswamiProgramming in ansi C by Balaguruswami
Programming in ansi C by Balaguruswami
 
Liderazgo educativo
Liderazgo educativoLiderazgo educativo
Liderazgo educativo
 
Structure in c
Structure in cStructure in c
Structure in c
 
Structure in c
Structure in cStructure in c
Structure in c
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
 

Similaire à Functions

An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
chapter-6 slide.pptx
chapter-6 slide.pptxchapter-6 slide.pptx
chapter-6 slide.pptxcricketreview
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)hhliu
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college2017eee0459
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7Rumman Ansari
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...kushwahashivam413
 
Function recap
Function recapFunction recap
Function recapalish sha
 
Function recap
Function recapFunction recap
Function recapalish sha
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directivesVikash Dhal
 

Similaire à Functions (20)

An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
7 functions
7  functions7  functions
7 functions
 
chapter-6 slide.pptx
chapter-6 slide.pptxchapter-6 slide.pptx
chapter-6 slide.pptx
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Array Cont
Array ContArray Cont
Array Cont
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
 
Functions
FunctionsFunctions
Functions
 
Function recap
Function recapFunction recap
Function recap
 
Function recap
Function recapFunction recap
Function recap
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 

Plus de Jesmin Akhter (7)

Lecture 11
Lecture  11Lecture  11
Lecture 11
 
Lecture 10
Lecture  10Lecture  10
Lecture 10
 
Lecture 11
Lecture  11Lecture  11
Lecture 11
 
Lecture 10
Lecture  10Lecture  10
Lecture 10
 
1605.01126
1605.011261605.01126
1605.01126
 
Lecture 05 2017
Lecture 05 2017Lecture 05 2017
Lecture 05 2017
 
Recursion
RecursionRecursion
Recursion
 

Dernier

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 

Dernier (20)

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 

Functions

  • 1. CSE 191 – COMPUTER PROGRAMMING Najia Manjur Lecturer Department of Computer Science & Engineering (CSE) Military Institute of Science & Technology (MIST) Mirpur, Dhaka – 1216, Bangladesh
  • 2. Introduction to Function  A large program can be divided into many subprograms. These subprograms can be called Functions.  They allow us to reuse code instead of rewriting it.  Functions allow us to test small parts of our program in isolation from the rest.  Save time and space of the code.  Library Functions : printf( ), scanf( ), gets( ), pow( ) etc.  User Defined Functions : main( ) 2
  • 3. Functions  Function Definition.  Function Call  Function Declaration. 3 #include<stdio.h> long cube(long x); //function prototype long input,answer; int main( ) { printf(“Enter values: “); scanf(“%d”,&input); answer= cube(input); //function call printf(“The cub of %ld is %ldn”,input,answer); return 0; } long cube(long x) //function definition { long x_cubed; x_cubed = x*x*x; return x_cubed; }
  • 4. long cube(long x) function_return_type function_name (parameter list) { function header long x_cubed; //local variable x_cubed = x*x*x; //executable statements function body return x_cubed; //return statement } Function Definition 4
  • 5. int main( ) { printf(“Enter values: ”); scanf(“%d”,&input); answer= cube(input); //function call printf(“The cub of %ld is %ldn”,input,answer); return 0; } Function Call 5
  • 6. function_return_type function_name (parameter list); long cube(long x); Function Prototype 6
  • 7.  parameter list must be separated by commas.  parameter names do not need to be same in prototype declaration and function definition.  parameters must match in type, number and order.  return type must be void when the function does not return a value.  if declared types do not match with types in function definition, compiler will produce an error. Important 7
  • 8. Category of Functions  no arguments and no return values.  Called function does not receive any data from calling function.  Calling function does not receive any data from called function.  arguments but no return value.  Calling function sends values to called function using function call with proper arguments  Calling function does not receive any data from called function.  argument and a return value.  Called function receives data from calling function.  Calling function receives a value from called function.  no argument but a return value.  Calling function sends no data to called function.  Calling function receives a value from called function. 8
  • 9. No Arguments and No Return Value void sum_series (void) ; //declaration int main( ) { sum_series( ); // function call return 0 ; } void sum_series(void) // definition { int sum=0,i=1, n; scanf(“%d”,&n); for(i=1 ; i<=n ; i++) sum+=i; printf(“%d”,sum); } 9 Step1 Step2
  • 10. Arguments but No Return Value void multi(float p , float q); //declaration int main( ) { multi(2 .3 , 3 .4); // function call return 0 ; } void multi(float a , float b) // definition { float ans=0; ans= a*b; printf(“%.2f”,ans); } 10
  • 11. Arguments and A Return Value float average(float a, int b) ; //declaration int main( ) { float a=0; a=average(2 . 5 , 5); // function call printf(“%f”,a); return 0 ; } float average(float x, int y) // definition { float avg=0; avg=(float) (2.5*5)/2; return avg; } 11
  • 12. No Arguments but A Return Value int get_number (void) ; //declaration int main( ) { int m=get_number( ); // function call printf(“%d”,m); return 0 ; } int get_number(void) // definition { int number ; scanf(“%d”,&number); return number; } 12
  • 13. Nested Function ( without declaration) int isequal(int p, int q) { if(p!=q) return 1; else return 0; } void difference(int y, int z) { if(isequal(y , z)) printf(“%d”,y-z); else printf(“No difference”); } int main( ) { int a, b; scanf(“%d %d”,&a, &b); difference(a,b); return 0; } 13 1 3 2 4
  • 14. Nested Function ( with declaration) void difference(int y, int z); int isequal(int p, int q); void difference(int y, int z) { if(isequal(y , z)) printf(“%d”,y-z); else printf(“No difference”); } int isequal(int p, int q) { if(p!=q) return 1; else return 0; } int main( ) { int a, b; scanf(“%d %d”,&a, &b); difference(a,b); return 0; } 14
  • 15. Array as an argument void sort(int m, int x[ ]); //declaration main( ) { sort(5, marks); //function call //marks is the array name sent from main function to sort() function } void sort(int m, int x[ ]) // definition { //code } 15
  • 16. Array as an argument when a function changes the values of the elements of an array, these changes will be made to the original array that was passed to the function. when an entire array is passed as an argument, the contents of the array are not copied to the formal parameter array, but information about the address of array elements are passed on to the function. so, any changes introduced to the array reflects on the original array in the calling function. 16