Functions, Parameters and Scope

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
The Command Line Mostly MS Dos with some UNIX/LINUX Copyright © Curt Hill.
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Python Functions.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
JavaScript, Fourth Edition
Copyright © 2015 Curt Hill Java for Minecraft Those things you should know.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Copyright © Curt Hill The Compound Statement C-Family Languages and Scope.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
The for Statement A most versatile loop
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Mostly MS Dos with some UNIX/LINUX
Chapter 15 - C++ As A "Better C"
Flow of Control An Overview
COMPUTATIONAL CONSTRUCTS
PowerShell Introduction Copyright © 2016 – Curt Hill.
A Review or Brief Introduction
The switch Statement, and Introduction to Looping
Programming Languages and Paradigms
The Three Attributes of an Identifier
JavaScript Syntax and Semantics
Programming Paradigms
CSCI 161: Introduction to Programming Function
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Lecture 14 Writing Classes part 2 Richard Gesick.
Organizing common actions
Predefined Dialog Boxes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Copyright © – Curt Hill Bash Scripting Fundamentals Copyright © – Curt Hill.
User Defined Functions
The C++ IF Statement Part 2 Copyright © Curt Hill
Concepts From Alice Switching to Java Copyright © Curt Hill.
Chapter 8 JavaScript: Control Statements, Part 2
Objects as Variables Featuring the Date Object
Constants in Java Why and How Copyright © Curt Hill.
6 Chapter Functions.
PHP.
Defining Classes I Part A.
Cmdlets “Command-lets”
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Control Structure Testing
Methods Again Parameter Passage
Multiple Inheritance in C++
G. Pullaiah College of Engineering and Technology
PowerShell Flow of Control Copyright © 2016 – Curt Hill.
Function.
Classes, Objects and Methods
PHP an introduction.
Programming Languages and Paradigms
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Function.
ITM 352 Functions.
Chapter 8 JavaScript: Control Statements, Part 2
Methods Scope How are names handled?
Methods Coding in Java Copyright © Curt Hill.
Presentation transcript:

Functions, Parameters and Scope PowerShell Functions, Parameters and Scope Copyright © 2016 – Curt Hill

Introduction Although we can define functions in PowerShell – there is little need for them Each file is considered a procedure No need for a header to define We do have to consider the notion of parameters as well as scope Copyright © 2016 – Curt Hill

Parameter Connection In C++ there is only one way to connect a actual and formal parameter This is the position in the parameter list void xyz(int a, float b){…} … xyz(4, z/2) The first actual (4) connects to the first formal (a) The second actual (result of z/2) connects with second formal (b) Copyright © 2016 – Curt Hill

The Other Way In several languages, such as Ada and JavaScript, named or keyword parameters are allowed As well as positional parameters In a keyword parameter the call may specify the parameter name and then the value Often positional parameters are used for the required values and keyword parameters for the defaulted ones Copyright © 2016 – Curt Hill

Parameters Recall that in the batch language there was no declaration of parameters Instead they were numbered: %1, %2 … PowerShell is different, there is a declaration This declaration is started with Param and followed with a parenthesized list of the parameters Copyright © 2016 – Curt Hill

The Parameter Set The form is: Param ( [Parameter(options)] [type] name, … [Parameter(options)] [type] name ) There may one or more parameters Several of these pieces are optional Copyright © 2016 – Curt Hill

Using Parameters Positional parameters are given following the name of the script In the same order as the parameter block They follow the script name Keyword parameters have the form: -name value Where –name is the name given in parameter block We now look at examples Copyright © 2016 – Curt Hill

Example 1 Consider the beginning of a script named scr: Param( [string]$spec, [string]$fn ) $files = get-childitem $spec This can be called by: scr *.ppt ppt_size.txt Or by: scr -fn pptsz.txt -spec *.ppt Copyright © 2016 – Curt Hill

Example 2 – Optional Type The [String] that precedes the parameter is optional Thus the parameter block could be: Param( $spec, $fn ) The line orientation is free as well: Param($spec,$fn) Multiple lines are often more readable Copyright © 2016 – Curt Hill

Example 3 - Defaults Parameters may have default values by appending an = and the value For example: Param($spec,$fn=“sizes.txt”) Now it may be called with one parameter: scr *.zip scr –spec *.zip Copyright © 2016 – Curt Hill

Example 4 - Mandatory A parameter left out becomes void Empty string We may use the options to require the parameter: Param( [Parameter(Mandatory=$True)] $spec, $fn="sizes.txt“ ) Now if called with no parameters it asks for the value of Spec Copyright © 2016 – Curt Hill

Switch Besides the string type there is a switch type for parameters By default a switch type is false Only set to true if the parameter is present Copyright © 2016 – Curt Hill

Example 5 Suppose the Script parameter: Param( [Parameter(Mandatory=$True)] $spec, $fn="sizes.txt", [switch]$announce ) … if($announce){ … If –announce is in call then $announce is true otherwise false Copyright © 2016 – Curt Hill

Example 6 – Position Normally the position of the parameters must follow the order in the parameter set This may be changed by the position option Param( $fn="sizes.txt", [Parameter( position = 1)] $spec, [switch]$announce) $spec must still be first Copyright © 2016 – Curt Hill

Functions A function may be defined using the following form: function fname { … } Where fname is the name of the function If you wish to use parameters then a param block is included Just like a script Calling a function has the same form as calling a script file Once a function is defined the name is retained for the duration of the session Copyright © 2016 – Curt Hill

Variables Again Variable is an object in regards to cmdlets The following cmdlets are available Clear-variable Get-variable New-variable Remove-variable Set-variable You should understand what each of these do Copyright © 2016 – Curt Hill

Scope You will recall scope in C++ This is not the way of PowerShell Every {} opens a new block Search current block If not found exit to enclosing block Keep going until global scope or found it This is not the way of PowerShell Copyright © 2016 – Curt Hill

Scope Levels PowerShell believes in several different scopes Global – the parent of all others Local – the current scope May be global, part of session or part of a script Script – Each script file has its own scope Copyright © 2016 – Curt Hill

Accessing A session which starts a script has the parent scope It is part of the global scope Each script that starts another script has the parent scope It is the child of a session or another script Copyright © 2016 – Curt Hill

Get-variable Several parameters of interest The first parameter can be the name of a variable Without the $ The –scope parameter shows all those variable in that scope Values include global, local Many of the variable cmdlets also allow you to specify scope Copyright © 2016 – Curt Hill

Finally This should do it for us Now how about an exercise? Copyright © 2016 – Curt Hill