SlideShare a Scribd company logo
1 of 17
Download to read offline
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

More Related Content

What's hot

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
 

What's hot (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
 

Viewers also liked (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
 

Similar to 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
 

Similar to 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
 

More from Jesmin Akhter

More from 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
 

Recently uploaded

Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...Nguyen Thanh Tu Collection
 
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 11 (CẢ NĂM) - FRIENDS GLOBAL - NĂM HỌC...
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 11 (CẢ NĂM) - FRIENDS GLOBAL - NĂM HỌC...BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 11 (CẢ NĂM) - FRIENDS GLOBAL - NĂM HỌC...
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 11 (CẢ NĂM) - FRIENDS GLOBAL - NĂM HỌC...Nguyen Thanh Tu Collection
 
Farrington HS Streamlines Guest Entrance
Farrington HS Streamlines Guest EntranceFarrington HS Streamlines Guest Entrance
Farrington HS Streamlines Guest Entrancejulius27264
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfChristalin Nelson
 
4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptxmary850239
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroomSamsung Business USA
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17Celine George
 
The role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenshipThe role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenshipKarl Donert
 
Transdisciplinary Pathways for Urban Resilience [Work in Progress].pptx
Transdisciplinary Pathways for Urban Resilience [Work in Progress].pptxTransdisciplinary Pathways for Urban Resilience [Work in Progress].pptx
Transdisciplinary Pathways for Urban Resilience [Work in Progress].pptxinfo924062
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxryandux83rd
 
How to create _name_search function in odoo 17
How to create _name_search function in odoo 17How to create _name_search function in odoo 17
How to create _name_search function in odoo 17Celine George
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 

Recently uploaded (20)

Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
 
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 11 (CẢ NĂM) - FRIENDS GLOBAL - NĂM HỌC...
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 11 (CẢ NĂM) - FRIENDS GLOBAL - NĂM HỌC...BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 11 (CẢ NĂM) - FRIENDS GLOBAL - NĂM HỌC...
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 11 (CẢ NĂM) - FRIENDS GLOBAL - NĂM HỌC...
 
Farrington HS Streamlines Guest Entrance
Farrington HS Streamlines Guest EntranceFarrington HS Streamlines Guest Entrance
Farrington HS Streamlines Guest Entrance
 
Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdf
 
4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17
 
The role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenshipThe role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenship
 
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
 
Transdisciplinary Pathways for Urban Resilience [Work in Progress].pptx
Transdisciplinary Pathways for Urban Resilience [Work in Progress].pptxTransdisciplinary Pathways for Urban Resilience [Work in Progress].pptx
Transdisciplinary Pathways for Urban Resilience [Work in Progress].pptx
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptx
 
CARNAVAL COM MAGIA E EUFORIA _
CARNAVAL COM MAGIA E EUFORIA            _CARNAVAL COM MAGIA E EUFORIA            _
CARNAVAL COM MAGIA E EUFORIA _
 
How to create _name_search function in odoo 17
How to create _name_search function in odoo 17How to create _name_search function in odoo 17
How to create _name_search function in odoo 17
 
Teaching Critical AI Literacies - Maha Bali
Teaching Critical AI Literacies - Maha BaliTeaching Critical AI Literacies - Maha Bali
Teaching Critical AI Literacies - Maha Bali
 
Israel Genealogy Research Assoc. April 2024 Database Release
Israel Genealogy Research Assoc. April 2024 Database ReleaseIsrael Genealogy Research Assoc. April 2024 Database Release
Israel Genealogy Research Assoc. April 2024 Database Release
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
Chi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical VariableChi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical Variable
 

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