Docsity

Programming - Unit 6: Functions (slides), Diapositivas de Programación Informática

Vista previa parcial del texto

¡Descarga Programming - Unit 6: Functions (slides) y más Diapositivas en PDF de Programación Informática solo en Docsity!

U N I T 6.

F U N C T I O N S

P R O G R A M M I N G Y E A R 2 0 2 0 - 2 0 2 1

CONTENTS

1. Modular programming. What is a function?

2. Function definition (building a function)

3. Calling a function (using a function)

4. Formal and actual parameters

5. Scope of variables and visibility

6. Arrays and structures as parameters of functions

7. Function files

MODULAR PROGRAMMING

  • Programming technique based on the decomposition of a problem in

simpler problems (modules) that can be analysed, programmed,

debugged and tested independently

PROGRAMMING PARADIGMS

  • What is a programming paradigm?
    • Basic criteria that rules the design of a programming language
    • A style of building the structure and elements of computer programs
  • Some paradigms
    • Structured programming (sequential + conditional + iterative)
    • Modular programming (functions)
    • Imperative programming vs declarative programming
    • Object-oriented programming

ADVANTAGES OF MODULAR PROGRAMMING

  • More structured and easier to read programs
    • shorter and simpler programs
  • Functions are reusable.
    • modules can be reused in different programs that require similar
functionalities
  • Functions are independent
    • They can be created, compiled and tested independently
    • So different people can work together in a large software project
    • A subprogram can be modified without having to change the rest of the
program

DESIGNING A GOOD DIVISION OF A PROGRAM IN FUNCTIONS IS A KEY ASPECT IN SOFTWARE DEVELOPMENT

5. 2. F U N C T I O N

D E F I N I T I O N

BU I L D I N G A F U N C T I O N

FUNCTION DEFINITION

  • A function definition has two parts: header and body function minimum = findMinimum (n1,n2,n3 ) end minimum=n1; if (n2 < minimum) minimum=n2; end if (n3 < minimum) minimum=n3; end header body Output Name Input

LOCAL VARIABLES

  • A function may have it's own variables
    • We call them local variables
    • They are visible only within the block where they are declared, invisible to
the rest of the program
  • Local variables come to existence when the function is called
  • They cease to exist when the function ends

LOCAL VARIABLES (EXAMPLE)

  • Define a function that, given a number, returns whether the number is

prime or not.

14 function pr = isPrime(number) pr = true; for n = 2:number- if mod(number,n) == 0 % if we find a divisor, the number is not prime pr = false; end end end n is a local variable

CALLING A FUNCTION

  • A function is called (invoked) when its name is used in an expression or

instruction

  • examples min=findMinimum(a, b, c); min=findMinimum(3, 17, -2); fprintf("%i", findMinimum(a,b,c));

CALLING A FUNCTION

  • When a function is called
    • the parameters are evaluated, and a copy of the values is passed to the
function
  • the execution control is handled to the function (the instructions within the
function are executed)
  • Syntax
    • Function name followed by list of parameters
    • Divided by comas, within parenthesis
  • A function call can be done from the main function or from within

another function

4. F O R M A L A N D A C T U A L

P A R A M E T E R S

%% Main script a=input('a?: ');b=input('b?: ');c=input('c?: ‘); min=findMinimum(a,b,c); min=findMinimum(3,17,-2); fprintf("%i", findMinimum(a,5,c)); %% Function definition function minimum = findMinimum (n1, n2,n3 ) minimum=n1; % minimum initialized to first value if (n2 < minimum) minimum=n2; end if (n3 < minimum) minimum=n3; end end

PARAMETERS

Function call: actual parameters Function definition: formal parameters