Functions A function is a block of code with a name. function functionName() { code to be executed; }

Slides:



Advertisements
Similar presentations
ALPHABET. The Alphabet  A, B, C, D, E, F,G, H, I,J,K L,M,N,O, P, Q, R, S, T, U, V, W, X, Y and Z.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
PHP Constructs Advance Database Management Systems Lab no.3.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
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.
CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous.
Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
= 5 = 2 = 4 = 3 How could I make 13 from these shapes? How could I make 19 from these shapes? STARTER.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University.
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.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Intro to CS Nov 29, 2016.
Introduction to Programming
PHP Built-In Functions
Chapter 6: Loops.
CHAPTER 4 CLIENT SIDE SCRIPTING PART 3 OF 3
Web Design II Flat Rock Community Schools
PHP 5 Syntax.
PHP Arrays Functions.
CHAPTER 5 SERVER SIDE SCRIPTING
PHP Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in.
Shell Scripting March 1st, 2004 Class Meeting 7.
Week 8 - Friday CS 121.
Modern JavaScript Develop And Design
JavaScript Syntax and Semantics
Deitel- C:How to Program (5ed)
Author: truong toan thinh
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
ITM 352 Flow-Control: Loops
User-Defined Functions
REBOL Writing Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 9 Scope, Lifetime, and More on Functions
Introduction to Web programming
Subroutines Web Programming.
Chapter 9 Classes: A Deeper Look, Part 1
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Classes & Objects: Examples
PHP.
Topics Introduction to Functions Defining and Calling a Void Function
MCS/BCS.
Basics (Cont...).
What Color is it?.
Functions.
Functions.
COMPUTER PROGRAMMING SKILLS
What is a Function? Takes one or more arguments, or perhaps none at all Performs an operation Returns one or more values, or perhaps none at all Similar.
CS561 Computer Architecture Hye Yeon Kim
Methods/Functions.
ITM 352 Functions.
CPS125.
Corresponds with Chapter 5
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Functions A function is a block of code with a name. function functionName() { code to be executed; }

The function will not execute unless it is called. function writeMsg() {   echo "Hello world!"; } writeMsg(); // call the function // Hello world! By calling the name of the function, we can use it repeatedly.

Variable Scope A variable outside a function is not available inside a function. A variable inside a function is not available outside the function.

Information can be passed to functions through arguments Information can be passed to functions through arguments. An argument is like a variable. function writeMsg($message) { echo $message; } writeMsg("Take me to your leader"); // Take me to your leader

Functions can accept multiple arguments, separated by a comma Functions can accept multiple arguments, separated by a comma. function nameAge($name, $year) { $age = 2014 - $year; echo "$name is $age years old"; } nameAge("Barack Obama", "1961"); // Barack Obama is 53 years old

Function arguments can be given a default value Function arguments can be given a default value. function setScore($minscore = 25) { echo "The score is $minscore <br />"; } setScore(110); setScore(); // Uses 25 by default setScore(98); setScore(101);

Return What if we need to store a value inside a function and use it later? function squareNum($x) { $square = $x * $x; return $square; }

Assign the function call to a variable function squareNum($x) { $square = $x * $x; return $square; } $output = squareNum(4); echo $output; // 16

A function may only return one value, but that value can be an array (or an object). When a function reaches return, the function terminates and the function is exited.

Echo the function call to immediately access return function centsToDollars($cents) { $total = $cents / 100; return $total; } echo "$" . centsToDollars(1228); // $12.28

Function with array as an argument function passArray($colors) { foreach ($colors as $name) { echo "$name <br />"; } passArray($colors = array("orange", "blue", "green", "red", "purple"));