Chapter 2 User_defined Function. Chapter Goals In this chapter, you’ll learn all about PHP functions, including how : to create and invoke them, pass.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Outline What Is Function ? Create Function Call Function Parameters Functions Function Returning Values PHP Variable Scopes Passing by Reference Vs Value.
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 Programmer-Defined Functions Functions allow program modularization Variables declared in function are local variables Only known inside function in.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Chapter 5 Functions.
MIRC Matthew Forest. Introduction mIRC itself is a program designed for text based messaging via the IRC (internet relay chat) protocol. (Link:
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
CSCI 116 Functions.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
IS 1181 IS 118 Introduction to Development Tools Chapter 5 Reusing Code.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Control Structures, Operators, and Functions.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Subroutines Just like C, PERL offers the ability to use subroutines for all the same reasons – Code that you will use over and over again – Breaking large.
Web forms in PHP Forms Recap  Way of allowing user interaction  Allows users to input data that can then be processed by a program / stored in a back-end.
Intermediate PHP (2) File Input/Output & User Defined Functions.
Functions & Objects IDIA 618 Spring 2012 Bridget M. Blodgett.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
1 Chapter 4 – Breaking It Up: Functions spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 05.
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Java Script User Defined Functions. Java Script  You can define your own JavaScript functions. Such functions are called user- defined, as opposed to.
JavaScript III Functions and Abstraction. 2 JavaScript so far statements assignment function calls data types numeric string boolean expressions variables.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Python Functions.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
JavaScript, Fourth Edition
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
PHP. PHP User Defined Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used.
PHP-5- Working with Files and Directories. Reading Files PHP’s file manipulation API is extremely flexible: it lets you read files into a string or into.
Expressions and Control Flow. Expressions An expression is a combination of values, variables, operators, and functions that results in a value y = 3(abs(2x)
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
VCE IT Theory Slideshows by Mark Kelly study design By Mark Kelly, vceit.com, Begin.
5 th and 4 th ed: Chapters 9, 10, 11 SY306 Web and Databases for Cyber Operations SlideSet #7: JavaScript Functions and Arrays.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
JavaScript Modularity. Goals By the end of this lecture, you should … Understand why programmers use modularity. Understand how to create a function in.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
 The real power of PHP comes from its functions; it has more than 1000 built-in functions.  PHP User Defined Functions  Besides the built-in PHP functions,
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 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Session 2 Basics of PHP.
CHAPTER 4 DECISIONS & LOOPS
CHAPTER 5 SERVER SIDE SCRIPTING
PHP.
Basics.
G. Pullaiah College of Engineering and Technology
COMPUTER PROGRAMMING SKILLS
ITM 352 Functions.
Presentation transcript:

Chapter 2 User_defined Function

Chapter Goals In this chapter, you’ll learn all about PHP functions, including how : to create and invoke them, pass input, return values to the caller, and Include function libraries

Function: Function is a self-contained block of code that can be called by your scripts. Functions provide a way to group together related statements into a cohesive block. For reusable code, a function saves duplicating statements and makes maintenance of the code easier.

Defining a Function You can define a function using the function statement: function function_name( $argument1, $argument2 ) { // function code here } The name of the function follows the function statement and precedes a set of parentheses. If your function requires arguments, you must place comma-separated variable names within the parentheses.

Calling a Function Functions in a PHP program can be either built-in or user-defined. Regardless of their source, all functions are evaluated in the same way: $some_value = function_name( [ parameter,... ] ); // strlen( ) is a built-in function that returns the length of a string $length = strlen("PHP"); // $length is now 3

Simple Function Call <?php function bold($string){ echo " ". $string. " \n"; } // First example function call (with a static string) echo "this is not bold\n"; bold("this is bold"); echo "this is again not bold\n"; // Second example function call (with a variable) $myString = "this is bold"; bold($myString); ?>

Functions can also return values by using the return statement: <?php function heading($text, $headingLevel){ switch ($headingLevel) { case 1: $result = " ". ucwords($text). " "; break; case 2: $result = " ". ucwords($text). " "; break; case 3: $result = " ". ucfirst($text). “ "; break; default: $result = " ". ucfirst($text). " "; } return($result); } $test = "user defined functions"; echo heading($test, 2); ?>

How Variables Are Passed to Functions 1.) Passing arguments by value By default, variables are passed to functions by value, not by reference Example: <?php function doublevalue($var){ $var = $var * 2; } $variable = 5; doublevalue($variable); echo "\$variable is: $variable"; ?>

2.) Passing arguments by reference An alternative to returning a result or using a global variable is to pass a reference to a variable as an argument to the function. This means that any changes to the variable within the function affect the original variable Passing an argument by reference is done by appending an ampersand(&) to the front of the argument. How Variables Are Passed to Functions

Example: Passing by Reference <?php $cost = 20.00; $tax = 0.05; function calculate_cost(&$cost, $tax) { // Modify the $cost variable $cost = $cost + ($cost * $tax); } calculate_cost($cost,$tax); echo "Tax is: ". ($tax*100)." "; echo "Cost is: $". $cost." "; ?>

Default argument values PHP allows functions to be defined with default values for arguments. A default value is simply supplied in the argument list using the = sign <?php function salestax($price,$tax=.0575) { $total = $price + ($price * $tax); echo "Total cost: $total"; } $price = 15.47; salestax($price); ?>

Using built-in functions PHP’s built-in functions are one reason why PHP is so powerful and useful. isset($varname) empty($varname) die(“message”); File Inclusion Statement PHP offers four statements for including such files into applications, each of which is introduced in this section. Include(“filename”) Include_once(“filename”) Require(“filename”) Require_once(“filename”)

The end of Chapter 2 Thanks for your paying attention