SlideShare una empresa de Scribd logo
1 de 23
Descargar para leer sin conexión
C             Programming
                       Language
               By:
    Yogendra Pal
yogendra@learnbywatch.com
  Dedicated to My mother and Father
t
                                                               y
         Keep your notebook with you.

Write important point and questions that comes in your mind

     Solve Mind band exercise.


                                                               C
                                       Rewind when not clear


              Ask Questions by call or SMS or by mail


Keep Watching Keep Learning

THIS IS FUNCTIONS


                                   2
Function
• Allow us to group commonly used code into a
  compact unit that can be used repeatedly.
• main() function is must in each program.
• Other functions call directly or indirectly from
  main().
Need
• Consider a problem:
  – Write a program that take 3 numbers x, y, z as input
    from user and calculate and print x5+y5+z5
General form
 Return_type function_name(parameters)
 {
       statements;
       return value;
 }

• Return type : any data type.
• Function name : used to call the function.
• Parameters : zero or more variables.
An Example
int square(int num)
{
    return num*num;
}

• This function accept an integer value and return its
  square.
Function-Call
• Pass function_name and Parameters.
• Function performs a specific task that is given
  to it.
• Result will return.
• Default return type is int.
• If you want to return noting use void.
function(1)



             function(2)
main()


             function(3)

         8
function1()   function2()   function3()



program1()                     program2()




                program3()

                     9
More about function
• All variable defined inside functions are local
  and known only in function defined.
• Parameters are used to communicate between
  functions.
Why use functions?
• Make your problem modularize.
• Increase reusability.
• Avoid code repetition.
Function Prototype
• Used to validate function.
• Prototype only needed if function definition
  comes after use in program.
  – Ex: int maximum(int, int, int);
• Write a function that return the maximum value
  among three integers.
Problems
• Write a function to calculate the factorial value
  of an integer entered through the keyboard.
• Write a function power(a, b), to calculate the
  value of ab
• Any year is entered through the keyboard.
  Write a function to determine whether the
  year is a leap or not.
Global Variable
• Global variables are defined outside of any
  function.
• Available to many functions.
• Functions are always external.
• C does not allow functions to be defined inside
  other functions.
• Any function may access an external variable
  by referring to it by name.

                        14
Scope Rules
• File scope
  – Identifier defined outside function, known in all
    functions.
  – Used for global variables, function definitions,
    function prototypes.
• Function scope
  – Can only be referenced inside a function body
  – Used only for labels (start:, case: , etc.)
Scope Rules
• Block scope
  – Identifier declared inside a block
     • Block scope begins at definition, ends at right brace
  – Used for variables, function parameters (local
    variables of function)
  – Outer blocks "hidden" from inner blocks if there is a
    variable with the same name in the inner block
• Function prototype scope
  – Used for identifiers in parameter list
Function calling
• Call by value
  – Copy of arguments passed to function.
  – Changes in function do not effect original.
  – Use when function does not need to modify
    arguments.
• Call by reference
  – Passes original arguments
  – Changes in function effect original variables.
Header Files
• Contain function             prototypes     for   library
  functions.
  – Ex: conio.h, stdio.h, stdlib.h, math.h.
• Load     with    #include               <filename>    or
  #include “filename”
Some standard header files
Standard       Explanation
library header
<float.h>     Contains the floating point size limits of the system.
<limits.h>    Contains the integral size limits of the system.
<locale.h>    Contains function prototypes and other information that enables a program to be
              modified for the current locale on which it is running. The notion of locale enables
              the computer system to handle different conventions for expressing data like dates,
              times, dollar amounts and large numbers throughout the world.
<math.h>      Contains function prototypes for math library functions.
<stdio.h>     Contains function prototypes for the standard input/output library functions, and
              information used by them.
<stdlib.h>    Contains function prototypes for conversions of numbers to text and text to
              numbers, memory allocation, random numbers, and other utility functions.
<string.h>    Contains function prototypes for string processing functions.
<time.h>      Contains function prototypes and types for manipulating the time and date.


                                              19
Custom header files
•   Create header file with function.
•   Save filename.h
•   Load in other files with #include “filename.h”.
•   Reuse function
Recursion
• Functions that call themselves.
• Function launches a new copy of itself.
• Ex:
  – Calculate factorial using recursion.
Mind Bend
• Write a function table(int num) that print the
  table of the number that is passed to it.
• Write a function table_atob(int a, int b) that
  accept two numbers a and b as an input and
  call table(int num) print the tables from a to b.
• Write a function that validate the input a and b
  entered in the function table_atob(int a, int b).

                         22
To get complete benefit of this tutorial solve all the quiz on
                       www.learnbywatch.com

              For any problem in this tutorial mail me at
                    yogendra@learnbywatch.com
                        with the subject “C”

                     For Other information mail at
                       info@learnbywatch.com


Keep Watching Keep Learning

NEXT IS ARRAYS

                                    23

Más contenido relacionado

La actualidad más candente

VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1YOGESH SINGH
 
Programming techniques
Programming techniquesProgramming techniques
Programming techniquesPrabhjit Singh
 
Functions in python
Functions in pythonFunctions in python
Functions in pythoncolorsof
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAdam Getchell
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppteShikshak
 
Intro f# functional_programming
Intro f# functional_programmingIntro f# functional_programming
Intro f# functional_programmingMauro Ghiani
 

La actualidad más candente (20)

Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
user defined function
user defined functionuser defined function
user defined function
 
Fda unit 1 lec 1
Fda unit 1 lec  1Fda unit 1 lec  1
Fda unit 1 lec 1
 
Programming techniques
Programming techniquesProgramming techniques
Programming techniques
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Python libraries
Python librariesPython libraries
Python libraries
 
Functions in C
Functions in CFunctions in C
Functions in C
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
Function & Recursion
Function & RecursionFunction & Recursion
Function & Recursion
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppt
 
Intro f# functional_programming
Intro f# functional_programmingIntro f# functional_programming
Intro f# functional_programming
 

Destacado

Destacado (20)

C Language Unit-8
C Language Unit-8C Language Unit-8
C Language Unit-8
 
Festival de navidad
Festival de navidadFestival de navidad
Festival de navidad
 
Festival de navidad
Festival de navidadFestival de navidad
Festival de navidad
 
C-Language Unit-2
C-Language Unit-2C-Language Unit-2
C-Language Unit-2
 
C Language Unit-3
C Language Unit-3C Language Unit-3
C Language Unit-3
 
C Language Unit-5
C Language Unit-5C Language Unit-5
C Language Unit-5
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppt
 
C Language Unit-6
C Language Unit-6C Language Unit-6
C Language Unit-6
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppt
 
Storage class
Storage classStorage class
Storage class
 
Manual de Recarga Lexmark E120.
Manual de Recarga Lexmark E120.Manual de Recarga Lexmark E120.
Manual de Recarga Lexmark E120.
 
American Vintage Lookbook
American Vintage LookbookAmerican Vintage Lookbook
American Vintage Lookbook
 
Mobile Marketing. How to Start? By MEC Interaction
Mobile Marketing. How to Start? By MEC InteractionMobile Marketing. How to Start? By MEC Interaction
Mobile Marketing. How to Start? By MEC Interaction
 
Characterization of reflectors and absorber coatings
Characterization of reflectors and absorber coatingsCharacterization of reflectors and absorber coatings
Characterization of reflectors and absorber coatings
 
DKSHAH RESUME
DKSHAH RESUMEDKSHAH RESUME
DKSHAH RESUME
 
Johannes wilhelm geiger
Johannes wilhelm geigerJohannes wilhelm geiger
Johannes wilhelm geiger
 
Mountainbike Holidays 2016 - Ö, D, CH
Mountainbike Holidays 2016 - Ö, D, CHMountainbike Holidays 2016 - Ö, D, CH
Mountainbike Holidays 2016 - Ö, D, CH
 
04. panorama comex mg abr2011
04. panorama comex mg abr201104. panorama comex mg abr2011
04. panorama comex mg abr2011
 
Like us! Follow us!
Like us! Follow us!Like us! Follow us!
Like us! Follow us!
 
Petflix
PetflixPetflix
Petflix
 

Similar a Functions

Similar a Functions (20)

Functions
FunctionsFunctions
Functions
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 
Functions_new.pptx
Functions_new.pptxFunctions_new.pptx
Functions_new.pptx
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Functions
FunctionsFunctions
Functions
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
 
Intro To C++ - Class #20: Functions, Recursion
Intro To C++ - Class #20: Functions, RecursionIntro To C++ - Class #20: Functions, Recursion
Intro To C++ - Class #20: Functions, Recursion
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptxPython_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
 
CPP07 - Scope
CPP07 - ScopeCPP07 - Scope
CPP07 - Scope
 
CPP06 - Functions
CPP06 - FunctionsCPP06 - Functions
CPP06 - Functions
 
Function in Python
Function in PythonFunction in Python
Function in Python
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
Function
FunctionFunction
Function
 
5. Functions in C.pdf
5. Functions in C.pdf5. Functions in C.pdf
5. Functions in C.pdf
 
Functions
FunctionsFunctions
Functions
 
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
 

Más de Learn By Watch

Demodulation of fm pll detector
Demodulation of fm pll detectorDemodulation of fm pll detector
Demodulation of fm pll detectorLearn By Watch
 
Demodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detectorDemodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detectorLearn By Watch
 
In direct method of fm generation armstrong method
In direct method of fm generation armstrong methodIn direct method of fm generation armstrong method
In direct method of fm generation armstrong methodLearn By Watch
 
Direct method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator methodDirect method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator methodLearn By Watch
 
Spectrum and power of wbfm
Spectrum and power of wbfmSpectrum and power of wbfm
Spectrum and power of wbfmLearn By Watch
 
Narrow band frequency modulation nbfm
Narrow band frequency modulation nbfmNarrow band frequency modulation nbfm
Narrow band frequency modulation nbfmLearn By Watch
 
General expression of fm signal
General expression of fm signalGeneral expression of fm signal
General expression of fm signalLearn By Watch
 
Frequency division multiplexing
Frequency division multiplexingFrequency division multiplexing
Frequency division multiplexingLearn By Watch
 
Demodulation of ssb synchronous detector
Demodulation of ssb synchronous detectorDemodulation of ssb synchronous detector
Demodulation of ssb synchronous detectorLearn By Watch
 
Generarion of ssb phase discrimination method
Generarion of ssb phase discrimination methodGenerarion of ssb phase discrimination method
Generarion of ssb phase discrimination methodLearn By Watch
 
Generarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination methodGenerarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination methodLearn By Watch
 
Demodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiverDemodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiverLearn By Watch
 
Quadrature carrier multiplexing qam
Quadrature carrier multiplexing qamQuadrature carrier multiplexing qam
Quadrature carrier multiplexing qamLearn By Watch
 
Demodulation of am synchronous detector
Demodulation of am synchronous detectorDemodulation of am synchronous detector
Demodulation of am synchronous detectorLearn By Watch
 

Más de Learn By Watch (20)

Tutorial 9 fm
Tutorial 9 fmTutorial 9 fm
Tutorial 9 fm
 
Phase modulation
Phase modulationPhase modulation
Phase modulation
 
Demodulation of fm pll detector
Demodulation of fm pll detectorDemodulation of fm pll detector
Demodulation of fm pll detector
 
Demodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detectorDemodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detector
 
In direct method of fm generation armstrong method
In direct method of fm generation armstrong methodIn direct method of fm generation armstrong method
In direct method of fm generation armstrong method
 
Direct method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator methodDirect method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator method
 
Carson's rule
Carson's ruleCarson's rule
Carson's rule
 
Spectrum and power of wbfm
Spectrum and power of wbfmSpectrum and power of wbfm
Spectrum and power of wbfm
 
Narrow band frequency modulation nbfm
Narrow band frequency modulation nbfmNarrow band frequency modulation nbfm
Narrow band frequency modulation nbfm
 
General expression of fm signal
General expression of fm signalGeneral expression of fm signal
General expression of fm signal
 
Angle modulation
Angle modulationAngle modulation
Angle modulation
 
Frequency division multiplexing
Frequency division multiplexingFrequency division multiplexing
Frequency division multiplexing
 
Vsb modulation
Vsb modulationVsb modulation
Vsb modulation
 
Demodulation of ssb synchronous detector
Demodulation of ssb synchronous detectorDemodulation of ssb synchronous detector
Demodulation of ssb synchronous detector
 
Generarion of ssb phase discrimination method
Generarion of ssb phase discrimination methodGenerarion of ssb phase discrimination method
Generarion of ssb phase discrimination method
 
Generarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination methodGenerarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination method
 
Ssb modulation
Ssb modulationSsb modulation
Ssb modulation
 
Demodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiverDemodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiver
 
Quadrature carrier multiplexing qam
Quadrature carrier multiplexing qamQuadrature carrier multiplexing qam
Quadrature carrier multiplexing qam
 
Demodulation of am synchronous detector
Demodulation of am synchronous detectorDemodulation of am synchronous detector
Demodulation of am synchronous detector
 

Functions

  • 1. C Programming Language By: Yogendra Pal yogendra@learnbywatch.com Dedicated to My mother and Father
  • 2. t y Keep your notebook with you. Write important point and questions that comes in your mind Solve Mind band exercise. C Rewind when not clear Ask Questions by call or SMS or by mail Keep Watching Keep Learning THIS IS FUNCTIONS 2
  • 3. Function • Allow us to group commonly used code into a compact unit that can be used repeatedly. • main() function is must in each program. • Other functions call directly or indirectly from main().
  • 4. Need • Consider a problem: – Write a program that take 3 numbers x, y, z as input from user and calculate and print x5+y5+z5
  • 5. General form Return_type function_name(parameters) { statements; return value; } • Return type : any data type. • Function name : used to call the function. • Parameters : zero or more variables.
  • 6. An Example int square(int num) { return num*num; } • This function accept an integer value and return its square.
  • 7. Function-Call • Pass function_name and Parameters. • Function performs a specific task that is given to it. • Result will return. • Default return type is int. • If you want to return noting use void.
  • 8. function(1) function(2) main() function(3) 8
  • 9. function1() function2() function3() program1() program2() program3() 9
  • 10. More about function • All variable defined inside functions are local and known only in function defined. • Parameters are used to communicate between functions.
  • 11. Why use functions? • Make your problem modularize. • Increase reusability. • Avoid code repetition.
  • 12. Function Prototype • Used to validate function. • Prototype only needed if function definition comes after use in program. – Ex: int maximum(int, int, int); • Write a function that return the maximum value among three integers.
  • 13. Problems • Write a function to calculate the factorial value of an integer entered through the keyboard. • Write a function power(a, b), to calculate the value of ab • Any year is entered through the keyboard. Write a function to determine whether the year is a leap or not.
  • 14. Global Variable • Global variables are defined outside of any function. • Available to many functions. • Functions are always external. • C does not allow functions to be defined inside other functions. • Any function may access an external variable by referring to it by name. 14
  • 15. Scope Rules • File scope – Identifier defined outside function, known in all functions. – Used for global variables, function definitions, function prototypes. • Function scope – Can only be referenced inside a function body – Used only for labels (start:, case: , etc.)
  • 16. Scope Rules • Block scope – Identifier declared inside a block • Block scope begins at definition, ends at right brace – Used for variables, function parameters (local variables of function) – Outer blocks "hidden" from inner blocks if there is a variable with the same name in the inner block • Function prototype scope – Used for identifiers in parameter list
  • 17. Function calling • Call by value – Copy of arguments passed to function. – Changes in function do not effect original. – Use when function does not need to modify arguments. • Call by reference – Passes original arguments – Changes in function effect original variables.
  • 18. Header Files • Contain function prototypes for library functions. – Ex: conio.h, stdio.h, stdlib.h, math.h. • Load with #include <filename> or #include “filename”
  • 19. Some standard header files Standard Explanation library header <float.h> Contains the floating point size limits of the system. <limits.h> Contains the integral size limits of the system. <locale.h> Contains function prototypes and other information that enables a program to be modified for the current locale on which it is running. The notion of locale enables the computer system to handle different conventions for expressing data like dates, times, dollar amounts and large numbers throughout the world. <math.h> Contains function prototypes for math library functions. <stdio.h> Contains function prototypes for the standard input/output library functions, and information used by them. <stdlib.h> Contains function prototypes for conversions of numbers to text and text to numbers, memory allocation, random numbers, and other utility functions. <string.h> Contains function prototypes for string processing functions. <time.h> Contains function prototypes and types for manipulating the time and date. 19
  • 20. Custom header files • Create header file with function. • Save filename.h • Load in other files with #include “filename.h”. • Reuse function
  • 21. Recursion • Functions that call themselves. • Function launches a new copy of itself. • Ex: – Calculate factorial using recursion.
  • 22. Mind Bend • Write a function table(int num) that print the table of the number that is passed to it. • Write a function table_atob(int a, int b) that accept two numbers a and b as an input and call table(int num) print the tables from a to b. • Write a function that validate the input a and b entered in the function table_atob(int a, int b). 22
  • 23. To get complete benefit of this tutorial solve all the quiz on www.learnbywatch.com For any problem in this tutorial mail me at yogendra@learnbywatch.com with the subject “C” For Other information mail at info@learnbywatch.com Keep Watching Keep Learning NEXT IS ARRAYS 23