CSCI 116 Functions.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
Outline What Is Function ? Create Function Call Function Parameters Functions Function Returning Values PHP Variable Scopes Passing by Reference Vs Value.
Objectives Using functions to organize PHP code
Functions Jay Summet. Aug Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Chapter 5 Functions.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
CS 1 with Robots Functions Institute for Personal Robots in Education (IPRE)‏
CSCI 116 Week 3 Functions & Control Structures. 2 Topics Using functions Using functions Variable scope and autoglobals Variable scope and autoglobals.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Intermediate PHP (2) File Input/Output & User Defined Functions.
PHP Flow Control Loops, Functions, Return, Exit, Require, Try...Catch Software University SoftUni Team Technical Trainers.
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
Lecture 5 – Function and Array SFDV3011 – Advanced Web Development 1.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
PHP - Basic Language Constructs CSCI 297 Scripting Languages - Day Two.
Functions, Objects, and Programming IDIA 619 Spring 2014 Bridget M. Blodgett.
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Topic 3 – The General Form of a C Program. CISC 105 – Topic 3 The General Form of a C Program Now, all of the basic building blocks of a C program are.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
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.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
13/2/12 Lecture 6. Functions 2 types: – Built in functions – Custom defined functions Functions minimize the amount of repetition of code. function consists.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Sep CS O'Hara1 CS 1301 Review Keith O’Hara
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
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,
JavaScript, Fourth Edition
PHP Function. Outline o What Is Function ? o Create Function o Call Function o Parameters Functions o Function Returning Values o Passing by Reference.
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.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
 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.
PHP 5 Syntax.
Chapter 6 Functions.
Chapter 5 Functions DDC 2133 Programming II.
Functions CIS 40 – Introduction to Programming in Python
Method.
PHP Functions, Scope MIS 3501, Fall 2015 Jeremy Shafer
User-Defined Functions
Chapter 5 Function Basics
Value returning Functions
Group Status Project Status.
Objectives In this chapter, you will:
Basics (Cont...).
Functions Jay Summet.
Fundamental Programming
COMPUTER PROGRAMMING SKILLS
Functions Jay Summet.
Predefined Functions Revisited
ITM 352 Functions.
Functions Jay Summet.
Presentation transcript:

CSCI 116 Functions

Functions A group of statements that you can execute as a single unit May or may not accept parameters An input to the function Placed inside parentheses May or may not return a value An output of the function

Built-in Functions See http://php.net/manual Type function name in Search box parameters array explode ( string $delimiter , string $string [, int $limit ] ) return value optional

Invoking a Function Pass an argument for each parameter “Capture” a return value, if there is one Assign to a variable As an argument of another function In a print or echo statement In a decision $str = “Tom^Dick^Harry”; $arr = explode (“^”, $str); print_r($arr);

Built-in Functions What are the parameters? What are the return values? How might you invoke the function? addslashes string addslashes ( string $str ) $newPhrase = addslashes($oldPhrase); pow number pow ( number  $base ,  number $exp ) if(pow(2, 3) > 5) … str_repeat string str_repeat ( string $input , int $multiplier ) print str_repeat(“Hello”, 5); rand int rand ( int $min , int $max ) $randomNumber = rand(1, 10);

A function may have zero or more parameters Defining Functions <?php function name_of_function(parameters) { statements; } ?> A function may have zero or more parameters

Function Example function printPhrase($phrase) { defining the function: function printPhrase($phrase) { echo “<p>$phrase</p>”; } printPhrase(“Silly goose!”); invoking the function:

Returning Values A return statement returns a value to the statement that called (invoked) the function A function does not have to return a value function averageNumbers($a, $b, $c) { $sumOfNumbers = $a + $b + $c; $average = $sumOfNumbers / 3; return $average; }

Function Practice Define a function greeting that takes a name as a parameter and prints “Hello, name!”. (This function has no return value.) Invoke the function function greeting($name) { print “Hello, $name!”; } greeting(“Sam”);

Function Practice Write a function average that returns the average of two values. Invoke the function. function average($num1, $num2) { $avg = ($num1 + $num2)/2; return $avg; } $a = 5; $b = 3; print “The average of $a and $b is ” . average($a, $b);

Function Practice Write a function largest that returns the maximum of two values. Invoke the function. function largest($num1, $num2) { if($num1 > $num2) return $num1; else return $num2; } $a = 5; $b = 3; print “The largest of $a and $b is ” . largest($a, $b);

Function Practice Write a function circumference that takes a radius and returns the circumference of a circle (C=3.14*Diameter). Invoke the function. function circumference($radius) { $circ = 3.14 * 2 * $radius; return $circ; } $radius = 5; print “The circumference is ” . circumference($radius);

Setting Default Parameter Values defining the function: function printPhrase($phrase = “Quack”) { echo “<p>$phrase</p>”; } printPhrase(); invoking the function:

Understanding Variable Scope Where in your program a declared variable can be used Can be either global or local Global variable Declared outside a function and available to all parts of your program Local variable Declared inside a function and only available within that function

Variable Scope <?php $globalVar = "Global"; function scopeExample() { global $globalVar; echo "<b>Inside function:</b><br />"; $localVar = "Local"; echo "$localVar<br />"; echo $globalVar.”<br /><br />"; } scopeExample(); echo "<b>Outside function:</b><br />"; echo "$globalVar<br />"; ?> global keyword used inside function to reference global variable

Variable Scope <?php $globalVar = "Global"; function scopeExample() { echo "<b>Inside function:</b><br />"; $localVar = "Local"; echo "$localVar<br />"; echo $GLOBALS[‘globalVar’].”<br /><br />"; } scopeExample(); echo "<b>Outside function:</b><br />"; echo "$globalVar<br />"; ?> $GLOBALS array used inside function to reference global variable

Include Files <?php /* circumference takes a radius functions.php Include Files <?php /* circumference takes a radius * and returns the circumference * of a circle. */ function circumference($radius) { $circ = 3.14 * 2 * $radius; return $circ; } /* area takes a radius and returns * the area of a circle. function area($radius) $area = 3.14 * pow($radius, 2); return $area; ?>

Include Files <?php include 'functions.php'; $radius = 5; testScript.php <?php include 'functions.php'; $radius = 5; print "The circumference is " . circumference($radius); print "The area is " . area($radius); ?>

Why use Functions? Reusability Functions make your code: The same function can be used more than once Within the same script Across different scrips Function libraries Functions make your code: Easier to read Easier to modify Easier to maintain