Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)

Slides:



Advertisements
Similar presentations
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Advertisements

Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
PHP Using Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
IE 212: Computational Methods for Industrial Engineering
CMPS 211 JavaScript Topic 2 Functions and Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Slide 1 PHP Arrays and User Defined Functions ITWA133.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
C# Programming Methods.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Arrays Chapter 7.
Chapter 11 - JavaScript: Arrays
Run-Time Environments Chapter 7
Test 2 Review Outline.
Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study.
Excel IF Function.
Passing Objects to Methods
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Arrays in PHP are quite versatile
Arrays An array in PHP is an ordered map
Arrays 2/4 By Pius Nyaanga.
Principles of programming languages 4: Parameter passing, Scope rules
Chapter 7 Part 1 Edited by JJ Shepherd
JavaScript: Functions
JavaScript: Functions.
Counted Loops.
User-Defined Functions
This pointer, Dynamic memory allocation, Constructors and Destructor
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Dynamic Memory Allocation
Arrays, For loop While loop Do while loop
Visual Basic .NET BASICS
User Defined Functions
7 Arrays.
Lists in Python.
Chapter 8 Arrays Objectives
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
Group Status Project Status.
Lecture 18 Arrays and Pointer Arithmetic
Can store many of the same kind of data together
JavaScript Arrays.
Classes and Objects.
CS2011 Introduction to Programming I Arrays (I)
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Building Java Programs
Chapter 6 Arrays.
Lecture 5: Functions and Parameters
Chapter 8 Arrays Objectives
The structure of programming
Suggested self-checks: Section 7.11 #1-11
CIS 199 Final Review.
Submitted By : Veenu Saini Lecturer (IT)
Data Structures & Algorithms
The structure of programming
C++ Array 1.
Thinking procedurally
Class rational part2.
Corresponds with Chapter 5
Presentation transcript:

Lesson 3 Functions

Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters) { body of function }

Functions Lesson 3

Functions Lesson 3 Passing Arguments Arguments provide a way to pass input to the function. When we write the code cube(3), “3” is the argument, the argument is available within the function as the parameter $num. The parameter takes on the value of the corresponding argument that is passed to it in the Invocation of the function. The cube function then uses this value to compute the return value.

Functions Lesson 3

Functions Lesson 3 By default, arguments are passed by value. This means that the parameter variable within the function holds a copy of the value passed to it. If the parameter’s value changes, it will not change the value of a variable in the calling statement. Consider the following :

Functions Lesson 3

Functions Lesson 3 When an argument is passed by reference, changes to the parameter variable do Result in changes to the calling statement’s variable. An ampersand (&) is placed before the parameter’s name to indicate that the argument should passed by reference.

Functions Lesson 3

Functions Lesson 3 It is also possible to establish a default value for a parameter

Functions Lesson 3 Variable Scope and Lifetime The Scope of a variable determines which parts of the program have access to it Consider the following example:

Functions Lesson 3

Functions Lesson 3

Functions Lesson 3

Functions Lesson 3 Static variables retain their previous values each time a function is invoked

Functions Lesson 3 Static variables retain their previous values each time a function is invoked

Functions Lesson 3 Nested Function You may wish for a function to contain other functions on which it depends

Functions Lesson 3 Recursion Function When Function calls itself

Functions Lesson 3 Recursion Function When Function calls itself

Lesson 3 Arrays

Lesson 3 Initializing Arrays This code creates an array with three elements. Since we did not explicitly specify indices inside the square brackets, the elements have been given the default indices 0,1 and 2.

Arrays Lesson 3 It is usually practical to assign indices in sequential order, as we have done above; but if necessary, we can assign any integer index you please :

Arrays Lesson 3 If we need to know how many elements are in array, we can use the count() function

Arrays Lesson 3 Another way to initialize an array is with array() construct

Arrays Lesson 3 Looping Through an Array

Arrays Lesson 3 Looping Through an Array ( Non Sequentially Indexed Arrays) The two function each() and list() can be used together to loop through an array, even if The indices are non sequential (or even if they are not numbers at all). We can determine the value of the current element using the current() function, and the Current element’s index using the key() function.

Arrays Lesson 3

Arrays Lesson 3 While (list ($key,$value)= each($countries) ) For each element in the array, set $key equal to the element’s key (or index), and $value equal to the value of the element, reset() function sets the internal Pointer to the first element. The each() function moves the array pointer one element forward every time it is called

Arrays Lesson 3 String Indexed Arrays

Arrays Lesson 3 Sorting Functions Sort()

Arrays Lesson 3 Sorting Functions

Arrays Lesson 3 asort()

Arrays Lesson 3 The rsort() and arsort() functions are identical to sort() and asort() respectively, expect That they sort arrays in reverse order. The ksort(), krsort() function sorts arrays by key, rather Than by value : sort()

Arrays Lesson 3

Arrays Lesson 3 rsort()

Arrays Lesson 3 arsort()

Arrays Lesson 3 ksort()

Arrays Lesson 3 krsort()

Lesson 3 End of Lesson 3