Shyam Gurram Graduate Assistant

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Programming Languages and Paradigms The C Programming Language.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Overview creating your own functions calling your own functions.
CS-212 Classes (cont) Dick Steflik. Member functions Member functions of a class include – getters used to retrieve state data – setters used to set state.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Intermediate PHP (2) File Input/Output & User Defined Functions.
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Variables, Operators and Data Types. By Shyam Gurram.
Chapter 6: Functions Starting Out with C++ Early Objects
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Slide: 1 Copyright © AdaCore Subprograms Presented by Quentin Ochem university.adacore.com.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
Learners Support Publications Functions in C++
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Introduction to PHP and MySQL (Creating Database-Driven Websites)
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Chapter 15 - C++ As A "Better C"
CS314 – Section 5 Recitation 9
User-Written Functions
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Computer Science 210 Computer Organization
Examples of Classes & Objects
A bit of C programming Lecture 3 Uli Raich.
COMP 170 – Introduction to Object Oriented Programming
Programming Languages and Paradigms
Functions and an Introduction to Recursion
Chapter 5 Functions DDC 2133 Programming II.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Programming Paradigms
C Basics.
Subroutines in Computer Programming
Computer Science 210 Computer Organization
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Object Oriented Programming COP3330 / CGS5409
An Introduction to Java – Part I, language basics
Pointers, Dynamic Data, and Reference Types
Classes & Objects: Examples
6 Chapter Functions.
PHP.
Type & Typeclass Syntax in function
Lecture 5: Functions and Parameters
Functions and an Introduction to Recursion
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
PHP an introduction.
Programming Languages and Paradigms
ITM 352 Functions.
CPS125.
Presentation transcript:

Shyam Gurram Graduate Assistant Functions Shyam Gurram Graduate Assistant

Defining Functions PHP functions are defined by a prototype that includes a function name, a list of parameters, and a return data type. The basic prototype of a PHP function that takes no parameters and returns no values is void function function_name(); Function names in PHP must start with an alphabetical character or an underscore and consist of only alphabetical characters, numbers, or underscores. Also, function names are global in scope and case insensitive, which means functions can only exist once in a page and its set of included PHP libraries

Call the user-defined function. print_string(); <?php  Call the user-defined function. print_string();   Define a no-parameter function that returns nothing. function print_string()   {   print "Hello PHP World!";   }   ?> PHP does not support overloading because it provides variable-length parameter lists. You will now define a function named my_function() that takes no parameters and returns nothing. It implements the following prototype: void function my_function();

efine('LOCAL',"[The global variable]"); Call function. print_string(); <?php  efine('LOCAL',"[The global variable]");   Call function.   print_string();  $function_local;   function print_string()  {  GLOBAL $function_local;  $function_local = LOCAL." is inside the function.";  }  ?> Functions can access only global variables and variables defined in them, which are known as local variables. We can also define another type of global variable by using the GLOBAL keyword in a function. Creating a variable with the GLOBAL keyword does not create an environment global variable like one created with the define() function.

Parameters by Value or Reference <?php  $var = "Initial"; print "Before function \$var [".$var."]<br>"; print_string($var); print "After function \$var [".$var."]";   function print_string($var) {   $var = "Updated"; print "Inside function \$var [".$var."]<br>"; }  ?>   Parameters by Value or Reference Passing a parameter by value means that you hand a copy of the variable or literal value to a function. The alternative to passing a parameter by value is to pass one by reference, which means that you provide a reference or pointer to the variable in memory.

Parameter Default Values <?php  add_numbers(); add_numbers(2,2); function add_numbers($a = 0,$b = 0) {   print "[(\$a + \$b)][".($a + $b)."]<br>";  }  ?> Parameter Default Values When defining functions, you can define parameters as mandatory or optional. Mandatory parameters must be listed before optional parameters in the argument list. Optional parameters have a default value, which can be any scalar variable, literal, or null value. void function add_numbers(int a = 0, int b = 0);

Using Functions to Return Values This prototype basically says that the function has a single input of a variable that may be any data type and returns a variable of any data type. Function return types are provided by using the return keyword, a variable or literal value, and a semicolon. You can return any valid data type from functions. There are some other functions those have different functionality.

Using Functions to Return Values is_array(): The function takes one formal parameter, which can be any data type. The function returns true if the actual parameter is an array and false if it is anything else. The function has the following pattern: bool is_array(mixed var) is_bool(): The function takes one formal parameter, which can be any data type. The function returns true if the actual parameter is a Boolean and false if it is anything else. The function has the following pattern: bool is_bool(mixed var)

Using Functions to Return Values is_float(): The function takes one formal parameter, which can be any data type. The function returns true if the actual parameter is a float and false if it is anything else. The is_double() and is_real() functions are aliases to the is_ float() function. The function has the following pattern: bool is_float(mixed var) is_int(): The function takes one formal parameter, which can be any data type. The function returns true if the actual parameter is an integer and false if it is anything else. The is_integer() and is_long() functions are aliases to the is_int() function. The function has the following pattern: bool is_int(mixed var)

Using Functions to Return Values is_null(): The function takes one formal parameter, which can be any data type. The function returns true if the actual parameter is a null and false if it is anything else. The function has the following pattern: bool is_null(mixed var) is_numeric(): The function takes one formal parameter, which can be any data type. The function returns true if the actual parameter is a number or a numeric string and false if it is anything else. The function has the following pattern: bool is_numeric(mixed var)

Using Functions to Return Values is_object(): The function takes one formal parameter, which can be any data type. The function returns true if the actual parameter is an object and false if it is anything else. The function has the following pattern: bool is_object(mixed var) is_string(): The function takes one formal parameter, which can be any data type. The function returns true if the actual parameter is a string and false if it is anything else. The function has the following pattern: bool is_string(mixed var)

Managing Dynamic Function Calls The usort() predefined function takes an array by reference and a callback function. Internally, the usort() function manages how values from the array are passed into your user-defined callback function. The function also builds a new array by replacing the old array with a sorted one. The DynamicFunctionCall.php program defines the user_sort() function to demonstrate dynamic function calls. The program builds a dynamic run-time function call by concatenating a function name variable and parentheses containing the array variable.

Using Recursive Functions The ability of a function to call a copy of itself is called recursion. There are two types of recursion: linear and nonlinear. In linear recursion, a function calls only one copy of itself each time. In nonlinear recursion, a function calls more than one copy of itself each time. The only problem with recursion is that it consumes large amounts of system resources. For example, PHP programs that perform over 200 recursive calls run the risk of collapsing the PHP stack, which would mean a depth of 200 linear recursions. Nonlinear recursions require more memory and will probably collapse the PHP stack in half or less the depth of a linear recursion

Using Recursive Functions <?php print "[factorial(7)] = [".factorial(7)."]<br>"; function factorial($var) { if (count($argv = func_get_args()) == 1); if ($argv[0] <= 1) return (double) 1; else return (double) $argv[0] * factorial($argv[0] - 1); }  } ?> Using Recursive Functions The only problem with recursion is that it consumes large amounts of system resources. For example, PHP programs that perform over 200 recursive calls run the risk of collapsing the PHP stack, which would mean a depth of 200 linear recursions. Nonlinear recursions require more memory and will probably collapse the PHP stack in half or less the depth of a linear recursion.