Windows Vista Forums

Functions
  1. #1


    Fil Guest

    Functions

    Hello,

    How do we write a function ?

    Public Function myFunc(byVal a as Integer) as Integer
    myFunc=2+a
    End function

    How do we write a subroutine ?

    Public Sub mySub(byref a as string)
    a=right(a,1)
    End Sub

    Thank you.



    Fil

      My System SpecsSystem Spec

  2. #2


    RichS Guest

    RE: Functions

    Best place to start for function syntax would be to type

    get-help about_function

    at the powershell prompt.

    Subroutines as such do not exist in powershell. Their functionality is
    provided by functions and filters
    --
    Richard Siddaway

    Please note that all scripts are supplied "as is" and with no warranty


    "Fil" wrote:

    > Hello,
    >
    > How do we write a function ?
    >
    > Public Function myFunc(byVal a as Integer) as Integer
    > myFunc=2+a
    > End function
    >
    > How do we write a subroutine ?
    >
    > Public Sub mySub(byref a as string)
    > a=right(a,1)
    > End Sub
    >
    > Thank you.
    >
    > Fil


      My System SpecsSystem Spec

  3. #3


    Fil Guest

    RE: Functions

    Thank you

    "RichS" wrote:

    > Best place to start for function syntax would be to type
    >
    > get-help about_function
    >
    > at the powershell prompt.
    >
    > Subroutines as such do not exist in powershell. Their functionality is
    > provided by functions and filters
    > --
    > Richard Siddaway
    >
    > Please note that all scripts are supplied "as is" and with no warranty
    >
    >
    > "Fil" wrote:
    >
    > > Hello,
    > >
    > > How do we write a function ?
    > >
    > > Public Function myFunc(byVal a as Integer) as Integer
    > > myFunc=2+a
    > > End function
    > >
    > > How do we write a subroutine ?
    > >
    > > Public Sub mySub(byref a as string)
    > > a=right(a,1)
    > > End Sub
    > >
    > > Thank you.
    > >
    > > Fil


      My System SpecsSystem Spec

  4. #4


    Keith Hill [MVP] Guest

    Re: Functions

    "Fil" <Fil@discussions.microsoft.com> wrote in message
    news:3F0A9577-BAAB-4F0F-A976-8120309E4550@microsoft.com...
    > Hello,
    >
    > How do we write a function ?
    >
    > Public Function myFunc(byVal a as Integer) as Integer
    > myFunc=2+a
    > End function


    function myFunc([int]$value = 0) {
    $result = 2 + $value
    foreach ($arg in $args) {
    $result += [int]$arg
    }
    $result
    }

    65 > myfunc
    2
    66 > myfunc 2
    4
    67 > myfunc -value 4
    6
    68 > myfunc 2 3 4 5
    16

    The first call takes advantage of specifying a default value for the Value
    parameter. The second passes in 2 note that you don't use parens when
    calling your own functions. Think of them really as funclets i.e. script
    based cmdlets and you don't invoke cmdlets by putting parameters in parens.
    The third shows that you can even specify parameters by name just like with
    a cmdlet which allows the user to put them in any order. The fourth example
    shows that $args is always there to collect any "extra" parameters specified
    to your function.

    >
    > How do we write a subroutine ?
    >
    > Public Sub mySub(byref a as string)
    > a=right(a,1)
    > End Sub


    function mySub([ref]$a) {
    $a.value = $a.Value.SubString(0, $a.Value.Length-1)
    }

    Call it like so:

    $msg = "Hello World"
    mySub ([ref]$msg)
    $msg
    Hello Worl

    This demonstrates using by ref parameters. In PoSH a function can "write
    output" or not so the distinction between function and subroutine doesn't
    really apply. Also keep in mind that any statement that doesn't capture
    output, effectively places that output as part of the function's output
    e.g.:

    function Sum([int]$value = 0) {
    "Debug msg: `$value is $value"
    $value + 2
    }

    50 > $result = Sum 3
    51 > $result
    Debug msg: $value is 3
    5

    You can fix this like so:

    function Sum([int]$value = 0) {
    Write-Debug "Debug msg: `$value is $value"
    $value + 2
    }

    Whether or not you see the debug message depends on what you
    $DebugPreference variable is set to.

    --
    Keith



      My System SpecsSystem Spec

  5. #5


    Fil Guest

    Re: Functions

    Thank you.

    "Keith Hill [MVP]" wrote:

    > "Fil" <Fil@discussions.microsoft.com> wrote in message
    > news:3F0A9577-BAAB-4F0F-A976-8120309E4550@microsoft.com...
    > > Hello,
    > >
    > > How do we write a function ?
    > >
    > > Public Function myFunc(byVal a as Integer) as Integer
    > > myFunc=2+a
    > > End function

    >
    > function myFunc([int]$value = 0) {
    > $result = 2 + $value
    > foreach ($arg in $args) {
    > $result += [int]$arg
    > }
    > $result
    > }
    >
    > 65 > myfunc
    > 2
    > 66 > myfunc 2
    > 4
    > 67 > myfunc -value 4
    > 6
    > 68 > myfunc 2 3 4 5
    > 16
    >
    > The first call takes advantage of specifying a default value for the Value
    > parameter. The second passes in 2 note that you don't use parens when
    > calling your own functions. Think of them really as funclets i.e. script
    > based cmdlets and you don't invoke cmdlets by putting parameters in parens.
    > The third shows that you can even specify parameters by name just like with
    > a cmdlet which allows the user to put them in any order. The fourth example
    > shows that $args is always there to collect any "extra" parameters specified
    > to your function.
    >
    > >
    > > How do we write a subroutine ?
    > >
    > > Public Sub mySub(byref a as string)
    > > a=right(a,1)
    > > End Sub

    >
    > function mySub([ref]$a) {
    > $a.value = $a.Value.SubString(0, $a.Value.Length-1)
    > }
    >
    > Call it like so:
    >
    > $msg = "Hello World"
    > mySub ([ref]$msg)
    > $msg
    > Hello Worl
    >
    > This demonstrates using by ref parameters. In PoSH a function can "write
    > output" or not so the distinction between function and subroutine doesn't
    > really apply. Also keep in mind that any statement that doesn't capture
    > output, effectively places that output as part of the function's output
    > e.g.:
    >
    > function Sum([int]$value = 0) {
    > "Debug msg: `$value is $value"
    > $value + 2
    > }
    >
    > 50 > $result = Sum 3
    > 51 > $result
    > Debug msg: $value is 3
    > 5
    >
    > You can fix this like so:
    >
    > function Sum([int]$value = 0) {
    > Write-Debug "Debug msg: `$value is $value"
    > $value + 2
    > }
    >
    > Whether or not you see the debug message depends on what you
    > $DebugPreference variable is set to.
    >
    > --
    > Keith
    >
    >
    >


      My System SpecsSystem Spec

Functions problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Functions and Objects ScotsBoy69 PowerShell 1 22 Jan 2009
Functions and how to use them OldDog PowerShell 7 19 Sep 2008
Fun with functions casey.daniell PowerShell 4 03 Apr 2008
Cut & paste functions Scott Vista General 1 19 Feb 2007
Where are the functions? AiAidevil Vista General 5 03 Feb 2007