SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 

Destacado (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 a 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 a 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
 

Más 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
 

Último

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 

Último (20)

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 

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