SlideShare uma empresa Scribd logo
1 de 21
Functions in C
 A brief study
Functions
• Function is a self-contained block or a sub-
  program of one or more statements that
  performs a special task when called.
Example of functions
• Hotel management

  – Front Office

  – Reservation

  – Housekeeping

  – Telephone
Hotel management
Hotel
Customer()
   {
      House_keeping(); //Function call
    }
House_keeping() //Function definition
   {
      Cleans rooms;
   }
Hotel
Customer() //calling function
   {
      House_keeping(); //Function call
    }
House_keeping() //called function
   {
      Cleans rooms;
   }
Add two numbers
main()
{
  int a,b;
  int c;
  printf(“Enter the value of a”);
  scanf(“%d”,&a);
  printf(“Enter the value of b”);
  scanf(“%d”,&b);
  c=a+b;
  printf(“Answer is:%d”,c);
}
Add two numbers using functions
main()
{                                 add(x,y)
  int a,b;                        {
  int c;                            z=x+y;
  printf(“Enter the value of a”);   printf(“%d”,z);
  scanf(“%d”,&a);                 }
  printf(“Enter the value of b”);
  scanf(“%d”,&b);
  add(a,b);
}
Arguments/Parameters
• Arguments are used mostly in functions.
• it can be any input parameter which a
  function can use to do it's work.
• Example:      sin(x) sin is a function
                       x is it's argument.
Arguments/Parameters
• Actual arguments:
        • Arguments of calling function

• Formal arguments:
       • Arguments of called function
Add two numbers using functions
main()                               formal arguments
{                                 add(x,y)
  int a,b;                        {
  int c;                              z=x+y;
  printf(“Enter the value of a”);     printf(“%d”,z);
  scanf(“%d”,&a);                  }
  printf(“Enter the value of b”);
  scanf(“%d”,&b);
  add(a,b); //function call
}       actual arguments
Argument/Parameter list
a=2
b=4
      add(a,b);



                    add(x,y)
                               x=2
                               y=4
return statement
• function uses return statement to return the
  value to the called function.
• Exit from the called function.
        return(expression);
Hotel
Customer() //calling function
   {
      House_keeping(); //Function call
    }
House_keeping() //called function
   {
      Cleans rooms;
      return 0;
   }
Hotel
Customer() //calling function
   {
      Front_office(Money); //Function call
    }
Front_office(Money) //called function
   {
      return receipt;
   }
Types of functions
1.   No arguments and no return type
2.   No arguments and return type
3.   With arguments and no return type
4.   With arguments and return type
Passing arguments
• The arguments passed to function can be of two
  types.
       1. Values passed – Call by value
       2. Address passed – Call by reference
Call by value
• main()
  {
  int x=50, y=70;
  add(x,y);
  }

  add(int x1,int y1)
  {
  int z1;
  z1=x1+y1;
  printf(“%d”,z1);
  }
Call by reference
• main()
  {
  int x=50, y=70;
  add(&x,&y);
  }

  add(int *x1,int *y1)
  {
  int z1;
  z1=x1+y1;
  printf(“%d”,z1);
  }
Call by reference
• Address is passed using symbol ‘&’
  value is accessed using symbol ‘*’

  x=50          &x=2000         *x=50
  y=70          &y=2008         *y=70
Thank you

Mais conteúdo relacionado

Mais procurados

C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
 

Mais procurados (20)

Function in c program
Function in c programFunction in c program
Function in c program
 
Function in c
Function in cFunction in c
Function in c
 
Function lecture
Function lectureFunction lecture
Function lecture
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Function
FunctionFunction
Function
 
Function & Recursion in C
Function & Recursion in CFunction & Recursion in C
Function & Recursion in C
 
C function presentation
C function presentationC function presentation
C function presentation
 
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)
 
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
 
C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
 
Functions in c
Functions in cFunctions in c
Functions in c
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#
 
functions in C and types
functions in C and typesfunctions in C and types
functions in C and types
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in C
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
C++ Function
C++ FunctionC++ Function
C++ Function
 

Destaque

Solution manual 8051 microcontroller by mazidi
Solution manual 8051 microcontroller by mazidiSolution manual 8051 microcontroller by mazidi
Solution manual 8051 microcontroller by mazidi
Muhammad Abdullah
 
8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware
Prof. Swapnil V. Kaware
 
8051 Microcontroller Notes
8051 Microcontroller Notes8051 Microcontroller Notes
8051 Microcontroller Notes
Dr.YNM
 
Writing c code for the 8051
Writing c code for the 8051Writing c code for the 8051
Writing c code for the 8051
Quản Minh Tú
 

Destaque (20)

Embedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontrollerEmbedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontroller
 
Function C
Function CFunction C
Function C
 
Embedded systems ppt iv part d
Embedded systems ppt iv   part dEmbedded systems ppt iv   part d
Embedded systems ppt iv part d
 
Embedded c programming guide e book atmel 8051 / 89c51 /89c52
Embedded c programming guide e book atmel 8051 / 89c51 /89c52Embedded c programming guide e book atmel 8051 / 89c51 /89c52
Embedded c programming guide e book atmel 8051 / 89c51 /89c52
 
Embedded systems ppt iv part c
Embedded systems ppt iv   part cEmbedded systems ppt iv   part c
Embedded systems ppt iv part c
 
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...
 
8051 io interface
8051 io interface8051 io interface
8051 io interface
 
Solution manual 8051 microcontroller by mazidi
Solution manual 8051 microcontroller by mazidiSolution manual 8051 microcontroller by mazidi
Solution manual 8051 microcontroller by mazidi
 
8051 Presentation
8051 Presentation8051 Presentation
8051 Presentation
 
8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C
 
Embedded c lab and keil c manual
Embedded  c  lab  and keil c  manualEmbedded  c  lab  and keil c  manual
Embedded c lab and keil c manual
 
Embedded c
Embedded cEmbedded c
Embedded c
 
8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware
 
Question paper with solution the 8051 microcontroller based embedded systems...
Question paper with solution  the 8051 microcontroller based embedded systems...Question paper with solution  the 8051 microcontroller based embedded systems...
Question paper with solution the 8051 microcontroller based embedded systems...
 
Microcontroller 8051 and its interfacing
Microcontroller 8051 and its interfacingMicrocontroller 8051 and its interfacing
Microcontroller 8051 and its interfacing
 
8051 MICROCONTROLLER
8051 MICROCONTROLLER 8051 MICROCONTROLLER
8051 MICROCONTROLLER
 
8051 Microcontroller Notes
8051 Microcontroller Notes8051 Microcontroller Notes
8051 Microcontroller Notes
 
8051 experiments1
8051 experiments18051 experiments1
8051 experiments1
 
Writing c code for the 8051
Writing c code for the 8051Writing c code for the 8051
Writing c code for the 8051
 
1347 Assembly Language Programming Of 8051
1347 Assembly Language Programming Of 80511347 Assembly Language Programming Of 8051
1347 Assembly Language Programming Of 8051
 

Semelhante a Functions in C

Semelhante a Functions in C (20)

Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
Functions
FunctionsFunctions
Functions
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Function (rule in programming)
Function (rule in programming)Function (rule in programming)
Function (rule in programming)
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
function in c
function in cfunction in c
function in c
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
4th unit full
4th unit full4th unit full
4th unit full
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Array Cont
Array ContArray Cont
Array Cont
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 

Último

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
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
AnaAcapella
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
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
heathfieldcps1
 

Último (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
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
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
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
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
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
 
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
 
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
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.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
 
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
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

Functions in C

  • 1. Functions in C A brief study
  • 2. Functions • Function is a self-contained block or a sub- program of one or more statements that performs a special task when called.
  • 3. Example of functions • Hotel management – Front Office – Reservation – Housekeeping – Telephone
  • 5. Hotel Customer() { House_keeping(); //Function call } House_keeping() //Function definition { Cleans rooms; }
  • 6. Hotel Customer() //calling function { House_keeping(); //Function call } House_keeping() //called function { Cleans rooms; }
  • 7. Add two numbers main() { int a,b; int c; printf(“Enter the value of a”); scanf(“%d”,&a); printf(“Enter the value of b”); scanf(“%d”,&b); c=a+b; printf(“Answer is:%d”,c); }
  • 8. Add two numbers using functions main() { add(x,y) int a,b; { int c; z=x+y; printf(“Enter the value of a”); printf(“%d”,z); scanf(“%d”,&a); } printf(“Enter the value of b”); scanf(“%d”,&b); add(a,b); }
  • 9. Arguments/Parameters • Arguments are used mostly in functions. • it can be any input parameter which a function can use to do it's work. • Example: sin(x) sin is a function x is it's argument.
  • 10. Arguments/Parameters • Actual arguments: • Arguments of calling function • Formal arguments: • Arguments of called function
  • 11. Add two numbers using functions main() formal arguments { add(x,y) int a,b; { int c; z=x+y; printf(“Enter the value of a”); printf(“%d”,z); scanf(“%d”,&a); } printf(“Enter the value of b”); scanf(“%d”,&b); add(a,b); //function call } actual arguments
  • 12. Argument/Parameter list a=2 b=4 add(a,b); add(x,y) x=2 y=4
  • 13. return statement • function uses return statement to return the value to the called function. • Exit from the called function. return(expression);
  • 14. Hotel Customer() //calling function { House_keeping(); //Function call } House_keeping() //called function { Cleans rooms; return 0; }
  • 15. Hotel Customer() //calling function { Front_office(Money); //Function call } Front_office(Money) //called function { return receipt; }
  • 16. Types of functions 1. No arguments and no return type 2. No arguments and return type 3. With arguments and no return type 4. With arguments and return type
  • 17. Passing arguments • The arguments passed to function can be of two types. 1. Values passed – Call by value 2. Address passed – Call by reference
  • 18. Call by value • main() { int x=50, y=70; add(x,y); } add(int x1,int y1) { int z1; z1=x1+y1; printf(“%d”,z1); }
  • 19. Call by reference • main() { int x=50, y=70; add(&x,&y); } add(int *x1,int *y1) { int z1; z1=x1+y1; printf(“%d”,z1); }
  • 20. Call by reference • Address is passed using symbol ‘&’ value is accessed using symbol ‘*’ x=50 &x=2000 *x=50 y=70 &y=2008 *y=70