SlideShare uma empresa Scribd logo
1 de 21
By:-
Fatima saqlain
Gaurav raj khaiwal
Gaurav subham
Gautam ahuja
Karan singh bisht
Madhur chopra
•   5.1 Introduction
•   5.2 Math Library Functions
•   5.3 Functions
•   5.4 Function Definitions
•   5.5 Function Prototypes
•   5.6 Header Files
•   5.7 Calling Functions: Call by Value and Call by Reference
•   5.8 Storage Classes
•   5.9 Scope Rules
•   5.10 Recursion
•   5.11 Example Using Recursion: The Fibonacci Series
•   Divide and conquer
    • Construct a program from smaller pieces or
     components
     • These smaller pieces are called modules
    • Each piece more manageable than the original
     program
•   Math library functions
    • perform common mathematical calculations
    • #include <math.h>
•   Format for calling functions
    • FunctionName( argument );
      • If multiple arguments, use comma-separated list
    • Cout<<2f<<sqrt( 900.0 );
      • Calls function sqrt, which returns the square root of its
        argument
      • All math functions return data type double
    • Arguments may be constants, variables, or
      expressions
•   Functions
    • Modularize a program
    • All variables declared inside functions are local variables
      • Known only in function defined
    • Parameters
      • Communicate information between functions
      • Local variables
•   Benefits of functions
    • Divide and conquer
      • Manageable program development
    • Software reusability
      • Use existing functions as building blocks for new programs
      • Abstraction - hide internal details (library functions)
    • Avoid code repetition
•   Function definition format
      • return-value-type function-name( parameter-list )
        {
          declarations and statements
        }
    • Function-name: any valid identifier
    • Return-value-type: data type of the result (default int)
      • void – indicates that the function returns nothing
    • Parameter-list: comma separated list, declares
      parameters
      • A type must be listed explicitly for each parameter unless,
        the parameter is of type int
•   Function definition format (continued)
      • return-value-type function-name( parameter-list )
        {
          declarations and statements
        }
    • Declarations and statements: function body (block)
      • Variables can be declared inside blocks (can be nested)
      • Functions can not be defined inside other functions
    • Returning control
      • If nothing returned
       • return;
       • or, until reaches right brace
      • If something returned
       • return expression;
•   Function prototype
    • Function name
    • Parameters – what the function takes in
    • Return type – data type function returns (default int)
    • Used to validate functions
    • Prototype only needed if function definition comes after use
      in program
    • The function with the prototype
        • int maximum( int, int, int );
        • Takes in 3 ints
        • Returns an int
•   Promotion rules and conversions
    • Converting to lower types can lead to errors
•   Header files
    • Contain function prototypes for library functions
    • <stdlib.h> , <math.h> , etc
    • Load with #include <filename>
      • #include <math.h>
•   Custom header files
    • Create file with functions
    • Save as filename.h
    • Load in other files with #include "filename.h"
    • Reuse functions
•   Used when invoking functions
•   Call by value
    • Copy of argument passed to function
    • Changes in function do not effect original
    • Use when function does not need to modify argument
      • Avoids accidental changes
•   Call by reference
    • Passes original argument
    • Changes in function effect original
    • Only used with trusted functions
•   For now, we focus on call by value
•   In C ++ programming language, variables can be
    referred differently depending on the context. For
    example, if you are writing a program for a low
    memory system, you may want to avoid copying
    larger sized types such as structs and arrays when
    passing them to functions. On the other hand, with
    data types like integers, there is no point in
    passing by reference when a pointer to an integer
    is the same size in memory as an integer itself.
•   Now, let us learn how variables can be passed in a
    C program.
•   When you use pass-by-value, the compiler copies the value of
    an argument in a calling function to a corresponding non-
    pointer or non-reference parameter in the called function
    definition. The parameter in the called function is initialized
    with the value of the passed argument. As long as the
    parameter has not been declared as constant, the value of the
    parameter can be changed, but the changes are only
    performed within the scope of the called function only; they
    have no effect on the value of the argument in the calling
    function.
•   In the following example, main passes func two values: 5 and
    7. The function func receives copies of these values and
    accesses them by the identifiers a and b. The function func
    changes the value of a. When control passes back to main,
    the actual values of x and y are not changed.
•   Storage class specifiers
    • Storage duration – how long an object exists in memory
    • Scope – where object can be referenced in program
    • Linkage – specifies the files in which an identifier is known
      (more in Chapter 14)
•   Automatic storage
    • Object created and destroyed within its block
    • auto: default for local variables
       • auto double x, y;
    • register: tries to put variable into high-speed registers
      • Can only be used for automatic variables
       • register int counter = 1;
•   Static storage
    • Variables exist for entire program execution
    • Default value of zero
    • static: local variables defined in functions.
      • Keep value after function ends
      • Only known in their own function
    • extern: default for global variables and functions
      • Known in any function
•   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.)
•   Block scope
    • Identifier declared inside a block
      • Block scope begins at declaration, 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
•   Recursive functions
    • Functions that call themselves
    • Can only solve a base case
    • Divide a problem up into
      • What it can do
      • What it cannot do
       • What it cannot do resembles original problem
       • The function launches a new copy of itself (recursion step) to
         solve what it cannot do
    • Eventually base case gets solved
      • Gets plugged in, works its way up and solves whole
        problem
•   Example: factorials
    • 5! = 5 * 4 * 3 * 2 * 1
    • Notice that
      • 5! = 5 * 4!
      • 4! = 4 * 3! ...
    • Can compute factorials recursively
    • Solve base case (1! = 0! = 1) then plug in
      • 2! = 2 * 1! = 2 * 1 = 2;
      • 3! = 3 * 2! = 3 * 2 = 6;
•   Fibonacci series: 0, 1, 1, 2, 3, 5, 8...
    • Each number is the sum of the previous two
    • Can be solved recursively:
      • fib( n ) = fib( n - 1 ) + fib( n – 2 )
    • Code for the fibaonacci function
      • long fibonacci( long n )
      •{
      • if (n == 0 || n == 1) // base case
      •     return n;
      • else
      •     return fibonacci( n - 1) +
             fibonacci( n – 2 );
      •}
•   Set of recursive calls to function
                       f( 3 )
    fibonacci
                  return       f( 2 )     +   f( 1 )



    return     f( 1 )      +    f( 0 )            return 1



             return 1          return 0
Functions

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
Function & Recursion
Function & RecursionFunction & Recursion
Function & Recursion
 
Functional programming in python
Functional programming in pythonFunctional programming in python
Functional programming in python
 
Functional programming in python
Functional programming in pythonFunctional programming in python
Functional programming in python
 
Intro to JavaScript - Week 2: Function
Intro to JavaScript - Week 2: FunctionIntro to JavaScript - Week 2: Function
Intro to JavaScript - Week 2: Function
 
user defined function
user defined functionuser defined function
user defined function
 
Basics of cpp
Basics of cppBasics of cpp
Basics of cpp
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
 
Functions in C
Functions in CFunctions in C
Functions in C
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8
 
Python functions
Python functionsPython functions
Python functions
 
Java 8 ​and ​Best Practices
Java 8 ​and ​Best PracticesJava 8 ​and ​Best Practices
Java 8 ​and ​Best Practices
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
3. functions modules_programs (1)
3. functions modules_programs (1)3. functions modules_programs (1)
3. functions modules_programs (1)
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
 

Destaque (7)

3263270 human-resource-management-systems-hrms
3263270 human-resource-management-systems-hrms3263270 human-resource-management-systems-hrms
3263270 human-resource-management-systems-hrms
 
Employee Management System
Employee Management SystemEmployee Management System
Employee Management System
 
Student database management system
Student database management systemStudent database management system
Student database management system
 
Student management system
Student management systemStudent management system
Student management system
 
Human Resource Management System (HRMS)
Human Resource Management System (HRMS)Human Resource Management System (HRMS)
Human Resource Management System (HRMS)
 
Project report-on-student-information-management-system-php-mysql
Project report-on-student-information-management-system-php-mysqlProject report-on-student-information-management-system-php-mysql
Project report-on-student-information-management-system-php-mysql
 
Library mangement system project srs documentation.doc
Library mangement system project srs documentation.docLibrary mangement system project srs documentation.doc
Library mangement system project srs documentation.doc
 

Semelhante a 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
ManiMala75
 
OOP-Module-1-Section-4-LectureNo1-5.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptxOOP-Module-1-Section-4-LectureNo1-5.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptx
sarthakgithub
 

Semelhante a Functions (20)

Functions
FunctionsFunctions
Functions
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
 
Functions
FunctionsFunctions
Functions
 
Functions
FunctionsFunctions
Functions
 
c-functions.ppt
c-functions.pptc-functions.ppt
c-functions.ppt
 
c-functions.ppt
c-functions.pptc-functions.ppt
c-functions.ppt
 
c-functions.ppt
c-functions.pptc-functions.ppt
c-functions.ppt
 
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
 
C language
C languageC language
C language
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
 
Functions
FunctionsFunctions
Functions
 
OOP-Module-1-Section-4-LectureNo1-5.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptxOOP-Module-1-Section-4-LectureNo1-5.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptx
 
chapter-2-functionseng-1 (1).pdf
chapter-2-functionseng-1 (1).pdfchapter-2-functionseng-1 (1).pdf
chapter-2-functionseng-1 (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
 
Funtional Programming
Funtional ProgrammingFuntional Programming
Funtional Programming
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
Functions_new.pptx
Functions_new.pptxFunctions_new.pptx
Functions_new.pptx
 

Último

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
 
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
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Último (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
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
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
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
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.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
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
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
 
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
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
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
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
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.
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
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
 

Functions

  • 1. By:- Fatima saqlain Gaurav raj khaiwal Gaurav subham Gautam ahuja Karan singh bisht Madhur chopra
  • 2. 5.1 Introduction • 5.2 Math Library Functions • 5.3 Functions • 5.4 Function Definitions • 5.5 Function Prototypes • 5.6 Header Files • 5.7 Calling Functions: Call by Value and Call by Reference • 5.8 Storage Classes • 5.9 Scope Rules • 5.10 Recursion • 5.11 Example Using Recursion: The Fibonacci Series
  • 3. Divide and conquer • Construct a program from smaller pieces or components • These smaller pieces are called modules • Each piece more manageable than the original program
  • 4. Math library functions • perform common mathematical calculations • #include <math.h> • Format for calling functions • FunctionName( argument ); • If multiple arguments, use comma-separated list • Cout<<2f<<sqrt( 900.0 ); • Calls function sqrt, which returns the square root of its argument • All math functions return data type double • Arguments may be constants, variables, or expressions
  • 5. Functions • Modularize a program • All variables declared inside functions are local variables • Known only in function defined • Parameters • Communicate information between functions • Local variables • Benefits of functions • Divide and conquer • Manageable program development • Software reusability • Use existing functions as building blocks for new programs • Abstraction - hide internal details (library functions) • Avoid code repetition
  • 6. Function definition format • return-value-type function-name( parameter-list ) { declarations and statements } • Function-name: any valid identifier • Return-value-type: data type of the result (default int) • void – indicates that the function returns nothing • Parameter-list: comma separated list, declares parameters • A type must be listed explicitly for each parameter unless, the parameter is of type int
  • 7. Function definition format (continued) • return-value-type function-name( parameter-list ) { declarations and statements } • Declarations and statements: function body (block) • Variables can be declared inside blocks (can be nested) • Functions can not be defined inside other functions • Returning control • If nothing returned • return; • or, until reaches right brace • If something returned • return expression;
  • 8. Function prototype • Function name • Parameters – what the function takes in • Return type – data type function returns (default int) • Used to validate functions • Prototype only needed if function definition comes after use in program • The function with the prototype • int maximum( int, int, int ); • Takes in 3 ints • Returns an int • Promotion rules and conversions • Converting to lower types can lead to errors
  • 9. Header files • Contain function prototypes for library functions • <stdlib.h> , <math.h> , etc • Load with #include <filename> • #include <math.h> • Custom header files • Create file with functions • Save as filename.h • Load in other files with #include "filename.h" • Reuse functions
  • 10. Used when invoking functions • Call by value • Copy of argument passed to function • Changes in function do not effect original • Use when function does not need to modify argument • Avoids accidental changes • Call by reference • Passes original argument • Changes in function effect original • Only used with trusted functions • For now, we focus on call by value
  • 11. In C ++ programming language, variables can be referred differently depending on the context. For example, if you are writing a program for a low memory system, you may want to avoid copying larger sized types such as structs and arrays when passing them to functions. On the other hand, with data types like integers, there is no point in passing by reference when a pointer to an integer is the same size in memory as an integer itself. • Now, let us learn how variables can be passed in a C program.
  • 12. When you use pass-by-value, the compiler copies the value of an argument in a calling function to a corresponding non- pointer or non-reference parameter in the called function definition. The parameter in the called function is initialized with the value of the passed argument. As long as the parameter has not been declared as constant, the value of the parameter can be changed, but the changes are only performed within the scope of the called function only; they have no effect on the value of the argument in the calling function. • In the following example, main passes func two values: 5 and 7. The function func receives copies of these values and accesses them by the identifiers a and b. The function func changes the value of a. When control passes back to main, the actual values of x and y are not changed.
  • 13. Storage class specifiers • Storage duration – how long an object exists in memory • Scope – where object can be referenced in program • Linkage – specifies the files in which an identifier is known (more in Chapter 14) • Automatic storage • Object created and destroyed within its block • auto: default for local variables • auto double x, y; • register: tries to put variable into high-speed registers • Can only be used for automatic variables • register int counter = 1;
  • 14. Static storage • Variables exist for entire program execution • Default value of zero • static: local variables defined in functions. • Keep value after function ends • Only known in their own function • extern: default for global variables and functions • Known in any function
  • 15. 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. Block scope • Identifier declared inside a block • Block scope begins at declaration, 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. Recursive functions • Functions that call themselves • Can only solve a base case • Divide a problem up into • What it can do • What it cannot do • What it cannot do resembles original problem • The function launches a new copy of itself (recursion step) to solve what it cannot do • Eventually base case gets solved • Gets plugged in, works its way up and solves whole problem
  • 18. Example: factorials • 5! = 5 * 4 * 3 * 2 * 1 • Notice that • 5! = 5 * 4! • 4! = 4 * 3! ... • Can compute factorials recursively • Solve base case (1! = 0! = 1) then plug in • 2! = 2 * 1! = 2 * 1 = 2; • 3! = 3 * 2! = 3 * 2 = 6;
  • 19. Fibonacci series: 0, 1, 1, 2, 3, 5, 8... • Each number is the sum of the previous two • Can be solved recursively: • fib( n ) = fib( n - 1 ) + fib( n – 2 ) • Code for the fibaonacci function • long fibonacci( long n ) •{ • if (n == 0 || n == 1) // base case • return n; • else • return fibonacci( n - 1) + fibonacci( n – 2 ); •}
  • 20. Set of recursive calls to function f( 3 ) fibonacci return f( 2 ) + f( 1 ) return f( 1 ) + f( 0 ) return 1 return 1 return 0