SlideShare una empresa de Scribd logo
1 de 20
FUNCTIONS’ IN C
                  By-Nilam Desai
WHAT IS FUNCTION?
 A function in C language is a block of code that
  performs a specific task.
 It has a name and it is reusable i.e. it can be
  executed from as many different parts in a 
  C Program as required.
 It also optionally returns a value to the calling
  program
FUNCTION PROPERTIES
 Every function has a unique name. This name is
  used to call function from “main()” function. A
  function can be called from within another
  function.
 A function is independent and it can perform its
  task without intervention from or interfering
  with other parts of the program.
 A function performs a specific task. A task is a
  distinct job that your program must perform as a
  part of its overall operation, such as adding two
  or more integer, sorting an array into numerical
  order, or calculating a cube root etc.
PROPERTIES CONT…
   A function returns a value to the calling
    program. This is optional and depends upon the
    task your function is going to accomplish.
    Suppose you want to just show few lines through
    function then it is not necessary to return a
    value. But if you are calculating area of rectangle
    and wanted to use result somewhere in program
    then you have to send back (return) value to the
    calling function.
TYPES OF C FUNCTION
   Inbuilt function: they are already defined in C
    library.
       i.e. Printf, scanf, clrscr etc. 
   User define function: programmer will create it
    based on program requirements.
       i.e. main
GENERAL STRUCTURE OF A C
FUNCTION
   <return type> FunctionName (Argument1, Argument2,
    Argument3……)
    {
    Statement1;
    Statement2;
    Statement3;
    }
   Example
       void main()
        {
         }

       int sum (int x, int y)
        {
        int result;
        result = x + y;
        return (result);
        }
FUNCTION PROTOTYPE AND
FUNCTION DEFINITION
 Function prototype tells compiler that we are
  going to use a function which will have given
  name, return type and parameters.
 Function definition on the other hand is just
  writing logic of any function.
       For example you have one function prototype in C
        program for adding two integer numbers
           int add(int, int)
       but along with prototype you also have to write logic
        how that function will behave (respect to above
        prototype; how you will utilize those two passed
        integer value and how you will return value) when
        you will call that function.
FUNCTION PROTOTYPE AND
FUNCTION DEFINITION EXAMPLE
   #include<stdio.h>
   #include<conio.h>
   void myFunction();
   int add(int, int);
    
   void main()
   {
       clrscr();
    
       myFunction();
       printf("nn%d",add(10,15));
       getch();
   }
EXAMPLE CONT…
 void myFunction()
{

     printf("This is inside function :D");

}

 

 int add(int a, int b)

{

     return a+b;

}
EXPLANATION
    We will start from line no. 4 & 5. In these two
    lines we have function prototype, line no. 4 has
    a simple function prototype which doesn’t have
    parameters or return type. It’s very simple to
    declare a prototype, see line no. 17 & 4, both
    line are same except prototype requires
    semicolon (;) at the end. Code block (line no.
    17-20) is function definition for our first function
    prototype (i.e. line no. 4). In this block we are
    defining behavior of that function and in my code
    it’s just printing a simple message for
    demonstration.
   Similarly we have second function prototype
    (line no. 5) and it has parameters and return
    type. Code block (line no. 22 – 25) is definition of
    above function prototype. One important point
    for function prototype having parameters; in
    prototype, variable name for parameters is
    optional (see line no. 5). There is no variable
    name for int parameters, its (int, int). But in
    that prototype’s function definition you must
    provide parameter variable name (see line no.
    22). We have (int a, int b) parameter name.

   One more important thing, if you writing
    function definition above main() function then
    you don’t need to write prototype of that function.
    But it’s a good practice to keep all the functions
    below main() function.
ADVANTAGES OF USING
FUNCTIONS:

 It makes possible top down modular
  programming. In this style of programming, the
  high level logic of the overall problem is solved
  first while the details of each lower level
  functions is addressed later.
 The length of the source program can be reduced
  by using functions at appropriate places.
 It becomes uncomplicated to locate and separate
  a faulty function for further study.
ADVANTAGES CONT…
 A function may be used later by many other
  programs this means that a c programmer can
  use function written by others, instead of
  starting over from scratch.
 A function can be used to keep away from
  rewriting the same block of codes which we are
  going use two or more locations in a program.
  This is especially useful if the code involved is
  long or complicated.
TYPES OF USER DEFINED
FUNCTIONS:
 Functions with no arguments and no return
  values.
 Functions with arguments and no return values.

 Functions with arguments and return values.

 Functions that return multiple values.

 Functions with no arguments and return values.
OUTPUT
EXPLANATION
 This program has only function named “add()” .
  Calling this C function from “main()” is very
  simple.
 This “add()” function takes two values as
  arguments, adds those two values and prints the
  result.
 Line 3-8 is a function block of the program. Line
  no. 3 is the header of function,void is return type
  of function, add is function name and (int x, int
  y) are variable which can hold integer values to x
  and y respectively.
 When we call function, line no. “12, 13, 14”, we
  need to send two integer values as its argument.
  Then these two values get stored in variable x
  and y of line no. 3. Now we have two values to
  perform addition; in line no. 5 there is an
  integer declaration named “result”. This integer
  will store the sum of x and y (please see line no.
  6). Line no. 7 simply prints the result with C’s
  inbuilt function “printf”.
 Now imagine the same program without using
  function. We have called “add()” function three
  times, to get the same output without using
  function we have to writeLine no. 6 & 7 three
  time. If you want to add more value later in the
  program then again you have to type those two
  lines.
NESTING OF FUNCTION
 Function calls within another function is called
  Nesting of function.
 C allows nesting of functions easily.

 Main() can call function1,function1 can call
  function2 and so on..

                     Main()


                   Function1()


                   Function2()
RULES FOR PASSING ARRAY AS
FUNCTION PARAMETER OR
ARGUMENT
 The function prototype must show that the
  argument is an array.
 In function definition the formal argument must
  be an array if the argument list have an array as
  parameter.
 Function call must have only the name of an
  array .indexing of the array is not required to
  pass an whole array.

Más contenido relacionado

La actualidad más candente

Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppteShikshak
 
Presentation on function
Presentation on functionPresentation on function
Presentation on functionAbu Zaman
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functionsAlisha Korpal
 
Functions in c
Functions in cFunctions in c
Functions in creshmy12
 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
 
User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1Shameer Ahmed Koya
 
Recursive Function
Recursive FunctionRecursive Function
Recursive FunctionHarsh Pathak
 
Function in C and C++
Function in C and C++Function in C and C++
Function in C and C++Rahul Sahu
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmigAppili Vamsi Krishna
 

La actualidad más candente (20)

Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppt
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
Functions in c
Functions in cFunctions in c
Functions in c
 
[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 07] Comments in C/C++[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 07] Comments in C/C++
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Function in C and C++
Function in C and C++Function in C and C++
Function in C and C++
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Functions in C
Functions in CFunctions in C
Functions in C
 

Destacado

Engage fundamentals
Engage fundamentalsEngage fundamentals
Engage fundamentalsEngage Group
 
compuhelp courses information
compuhelp courses informationcompuhelp courses information
compuhelp courses informationSunil Guleria
 
Social media im patentwesen
Social media im patentwesenSocial media im patentwesen
Social media im patentwesenTim Jagodzinski
 
Career education-review-robert-starks-jr-social-media-strategies-max knowledge
Career education-review-robert-starks-jr-social-media-strategies-max knowledgeCareer education-review-robert-starks-jr-social-media-strategies-max knowledge
Career education-review-robert-starks-jr-social-media-strategies-max knowledgeMaxKnowledge
 
WP-What-Makes-QlikView-Unique_EN
WP-What-Makes-QlikView-Unique_ENWP-What-Makes-QlikView-Unique_EN
WP-What-Makes-QlikView-Unique_ENIbrahim El-zayat
 
Contoh penggunaan-iptables
Contoh penggunaan-iptablesContoh penggunaan-iptables
Contoh penggunaan-iptablesIwan Kurniarasa
 
Engage at Melcrum Summit 2012
Engage at Melcrum Summit 2012Engage at Melcrum Summit 2012
Engage at Melcrum Summit 2012Engage Group
 
#BrightonSEO Client Checklist for SEOs with notes
#BrightonSEO Client Checklist for SEOs with notes#BrightonSEO Client Checklist for SEOs with notes
#BrightonSEO Client Checklist for SEOs with notesSion O'Connor
 
Curso e proinfo
Curso e proinfoCurso e proinfo
Curso e proinfoFatimaLucy
 
Understanding the Social Shift: How State Associations of Private Postseconda...
Understanding the Social Shift: How State Associations of Private Postseconda...Understanding the Social Shift: How State Associations of Private Postseconda...
Understanding the Social Shift: How State Associations of Private Postseconda...MaxKnowledge
 
Membangun web server,_e-mail_server_dan_ftp_server
Membangun web server,_e-mail_server_dan_ftp_serverMembangun web server,_e-mail_server_dan_ftp_server
Membangun web server,_e-mail_server_dan_ftp_serverIwan Kurniarasa
 
Membuat mail server di ubuntu
Membuat mail server di ubuntuMembuat mail server di ubuntu
Membuat mail server di ubuntuIwan Kurniarasa
 

Destacado (20)

Engage fundamentals
Engage fundamentalsEngage fundamentals
Engage fundamentals
 
Windows phone and azure
Windows phone and azureWindows phone and azure
Windows phone and azure
 
compuhelp courses information
compuhelp courses informationcompuhelp courses information
compuhelp courses information
 
Social media im patentwesen
Social media im patentwesenSocial media im patentwesen
Social media im patentwesen
 
Career education-review-robert-starks-jr-social-media-strategies-max knowledge
Career education-review-robert-starks-jr-social-media-strategies-max knowledgeCareer education-review-robert-starks-jr-social-media-strategies-max knowledge
Career education-review-robert-starks-jr-social-media-strategies-max knowledge
 
Resume of Jean Christophe ROBLES
Resume of Jean Christophe ROBLESResume of Jean Christophe ROBLES
Resume of Jean Christophe ROBLES
 
WP-What-Makes-QlikView-Unique_EN
WP-What-Makes-QlikView-Unique_ENWP-What-Makes-QlikView-Unique_EN
WP-What-Makes-QlikView-Unique_EN
 
Ipv6
Ipv6Ipv6
Ipv6
 
Contoh penggunaan-iptables
Contoh penggunaan-iptablesContoh penggunaan-iptables
Contoh penggunaan-iptables
 
Engage at Melcrum Summit 2012
Engage at Melcrum Summit 2012Engage at Melcrum Summit 2012
Engage at Melcrum Summit 2012
 
#BrightonSEO Client Checklist for SEOs with notes
#BrightonSEO Client Checklist for SEOs with notes#BrightonSEO Client Checklist for SEOs with notes
#BrightonSEO Client Checklist for SEOs with notes
 
About Engage
About Engage About Engage
About Engage
 
Curso e proinfo
Curso e proinfoCurso e proinfo
Curso e proinfo
 
Understanding the Social Shift: How State Associations of Private Postseconda...
Understanding the Social Shift: How State Associations of Private Postseconda...Understanding the Social Shift: How State Associations of Private Postseconda...
Understanding the Social Shift: How State Associations of Private Postseconda...
 
5
55
5
 
Membangun web server,_e-mail_server_dan_ftp_server
Membangun web server,_e-mail_server_dan_ftp_serverMembangun web server,_e-mail_server_dan_ftp_server
Membangun web server,_e-mail_server_dan_ftp_server
 
Curriculum Vitae of Jean Christophe ROBLES (updated)
Curriculum Vitae of Jean Christophe ROBLES (updated)Curriculum Vitae of Jean Christophe ROBLES (updated)
Curriculum Vitae of Jean Christophe ROBLES (updated)
 
Curriculum Vitae of Jean Christophe ROBLES
Curriculum Vitae of Jean Christophe ROBLESCurriculum Vitae of Jean Christophe ROBLES
Curriculum Vitae of Jean Christophe ROBLES
 
Membuat mail server di ubuntu
Membuat mail server di ubuntuMembuat mail server di ubuntu
Membuat mail server di ubuntu
 
Data encryption standar
Data encryption standarData encryption standar
Data encryption standar
 

Similar a Function

Similar a Function (20)

Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdf
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
 
Presentation 2.pptx
Presentation 2.pptxPresentation 2.pptx
Presentation 2.pptx
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
 
Functions
Functions Functions
Functions
 
C FUNCTIONS
C FUNCTIONSC FUNCTIONS
C FUNCTIONS
 
Unit 4.pdf
Unit 4.pdfUnit 4.pdf
Unit 4.pdf
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1
 

Último

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Último (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

Function

  • 1. FUNCTIONS’ IN C By-Nilam Desai
  • 2. WHAT IS FUNCTION?  A function in C language is a block of code that performs a specific task.  It has a name and it is reusable i.e. it can be executed from as many different parts in a  C Program as required.  It also optionally returns a value to the calling program
  • 3. FUNCTION PROPERTIES  Every function has a unique name. This name is used to call function from “main()” function. A function can be called from within another function.  A function is independent and it can perform its task without intervention from or interfering with other parts of the program.  A function performs a specific task. A task is a distinct job that your program must perform as a part of its overall operation, such as adding two or more integer, sorting an array into numerical order, or calculating a cube root etc.
  • 4. PROPERTIES CONT…  A function returns a value to the calling program. This is optional and depends upon the task your function is going to accomplish. Suppose you want to just show few lines through function then it is not necessary to return a value. But if you are calculating area of rectangle and wanted to use result somewhere in program then you have to send back (return) value to the calling function.
  • 5. TYPES OF C FUNCTION  Inbuilt function: they are already defined in C library.  i.e. Printf, scanf, clrscr etc.   User define function: programmer will create it based on program requirements.  i.e. main
  • 6. GENERAL STRUCTURE OF A C FUNCTION  <return type> FunctionName (Argument1, Argument2, Argument3……) { Statement1; Statement2; Statement3; }  Example  void main() { }  int sum (int x, int y) { int result; result = x + y; return (result); }
  • 7. FUNCTION PROTOTYPE AND FUNCTION DEFINITION  Function prototype tells compiler that we are going to use a function which will have given name, return type and parameters.  Function definition on the other hand is just writing logic of any function.  For example you have one function prototype in C program for adding two integer numbers  int add(int, int)  but along with prototype you also have to write logic how that function will behave (respect to above prototype; how you will utilize those two passed integer value and how you will return value) when you will call that function.
  • 8. FUNCTION PROTOTYPE AND FUNCTION DEFINITION EXAMPLE  #include<stdio.h>  #include<conio.h>  void myFunction();  int add(int, int);     void main()  {      clrscr();         myFunction();      printf("nn%d",add(10,15));  getch();  }
  • 9. EXAMPLE CONT…  void myFunction() {      printf("This is inside function :D"); }    int add(int a, int b) {      return a+b; }
  • 10. EXPLANATION  We will start from line no. 4 & 5. In these two lines we have function prototype, line no. 4 has a simple function prototype which doesn’t have parameters or return type. It’s very simple to declare a prototype, see line no. 17 & 4, both line are same except prototype requires semicolon (;) at the end. Code block (line no. 17-20) is function definition for our first function prototype (i.e. line no. 4). In this block we are defining behavior of that function and in my code it’s just printing a simple message for demonstration.
  • 11. Similarly we have second function prototype (line no. 5) and it has parameters and return type. Code block (line no. 22 – 25) is definition of above function prototype. One important point for function prototype having parameters; in prototype, variable name for parameters is optional (see line no. 5). There is no variable name for int parameters, its (int, int). But in that prototype’s function definition you must provide parameter variable name (see line no. 22). We have (int a, int b) parameter name.  One more important thing, if you writing function definition above main() function then you don’t need to write prototype of that function. But it’s a good practice to keep all the functions below main() function.
  • 12. ADVANTAGES OF USING FUNCTIONS:  It makes possible top down modular programming. In this style of programming, the high level logic of the overall problem is solved first while the details of each lower level functions is addressed later.  The length of the source program can be reduced by using functions at appropriate places.  It becomes uncomplicated to locate and separate a faulty function for further study.
  • 13. ADVANTAGES CONT…  A function may be used later by many other programs this means that a c programmer can use function written by others, instead of starting over from scratch.  A function can be used to keep away from rewriting the same block of codes which we are going use two or more locations in a program. This is especially useful if the code involved is long or complicated.
  • 14. TYPES OF USER DEFINED FUNCTIONS:  Functions with no arguments and no return values.  Functions with arguments and no return values.  Functions with arguments and return values.  Functions that return multiple values.  Functions with no arguments and return values.
  • 15.
  • 17. EXPLANATION  This program has only function named “add()” . Calling this C function from “main()” is very simple.  This “add()” function takes two values as arguments, adds those two values and prints the result.  Line 3-8 is a function block of the program. Line no. 3 is the header of function,void is return type of function, add is function name and (int x, int y) are variable which can hold integer values to x and y respectively.
  • 18.  When we call function, line no. “12, 13, 14”, we need to send two integer values as its argument. Then these two values get stored in variable x and y of line no. 3. Now we have two values to perform addition; in line no. 5 there is an integer declaration named “result”. This integer will store the sum of x and y (please see line no. 6). Line no. 7 simply prints the result with C’s inbuilt function “printf”.  Now imagine the same program without using function. We have called “add()” function three times, to get the same output without using function we have to writeLine no. 6 & 7 three time. If you want to add more value later in the program then again you have to type those two lines.
  • 19. NESTING OF FUNCTION  Function calls within another function is called Nesting of function.  C allows nesting of functions easily.  Main() can call function1,function1 can call function2 and so on.. Main() Function1() Function2()
  • 20. RULES FOR PASSING ARRAY AS FUNCTION PARAMETER OR ARGUMENT  The function prototype must show that the argument is an array.  In function definition the formal argument must be an array if the argument list have an array as parameter.  Function call must have only the name of an array .indexing of the array is not required to pass an whole array.