Using Functions after the Pipeline

本文详细介绍了Powershell普通函数的声明方式及使用方法,重点讲解了函数的阶段处理,包括begin、process和end三个步骤。同时,文章还展示了如何通过管道传递参数并进行函数调用,提供了具体的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们都知道Powershell普通函数的声明的样子,使用也很简单:

function(关键字) 函数名 (参数) {代码}


比如:

function add ($x,$y)  
{  
    $n = $x + $y  
    “$x+$y=$n”  
} 

上面的代码还可以用另一种方法来实现:
function add 
{  
    param ($x,$y)
    $n = $x + $y  
    “$x+$y=$n”  
} 
其中,"param"的作用是声明参数。

回正题,今天介绍的是通过管道的参数传递和函数调用。 

先要了解到是  函数的阶段处理


在函数中,还可以进一步分为下列3个处理步骤:

1. begin - 只在函数第一次开始时执行一次,适用于放置初始化函数的代码。

2. process - 每一次调用函数时都执行

3. end - 只在函数结束时执行一次

并不是所有的函数都需要这三个步骤,但是一旦选择使用这种函数表达格式,在这三个代码块之外不能再存在其他代码,并且,每个代码块只能出现一次。

一个典型的例子:

# Function define.
function Test-Pipeline {
param(
# Indicate get parameters from pipe.
[Parameter(ValueFromPipeline=$true)]
$Incoming
)
    # only execute once.
    begin 
    {
        $i = 1
        "This is the begin!"
    }

    # execute for every parameters.
    process 
    {
      # only display the process which has main windows title.
      if ($incoming.MainWindowTitle -ne ''){
      "Number $i process = $Incoming"
      $i++
      }
    }
    # only execute once.
    end
    {
        "This is the end!"
    }
}

# Function Call.
Get-Process | Test-Pipeline 

输出结果:

This is the begin!
Number 1 process = System.Diagnostics.Process (chrome)
Number 2 process = System.Diagnostics.Process (communicator)
Number 3 process = System.Diagnostics.Process (DianDian)
Number 4 process = System.Diagnostics.Process (iexplore)
Number 5 process = System.Diagnostics.Process (OUTLOOK)
Number 6 process = System.Diagnostics.Process (powershell_ise)
Number 7 process = System.Diagnostics.Process (sidebar)
Number 8 process = System.Diagnostics.Process (Ssms)
Number 9 process = System.Diagnostics.Process (VpxClient)
This is the end!



### OpenGL Fixed Function Pipeline vs Programmable Pipeline Differences In traditional OpenGL versions prior to 2.0, the **fixed function pipeline** was utilized where developers had limited control over how vertices were transformed or fragments shaded; this process relied on predefined functions set by the API itself[^1]. The architecture included stages like transformation, lighting, clipping, rasterization, texture application, and finally pixel operations. With the advent of more advanced GPUs, OpenGL introduced the **programmable pipeline**, starting from version 2.0 onwards with specifications such as OpenGL ES 2.0 which emphasized creating shader programs using GLSL (OpenGL Shading Language). This shift allowed for greater flexibility in defining custom behavior at various points within the rendering pipeline through user-defined shaders—specifically vertex shaders that manipulate geometric data before it reaches later stages of processing, and fragment shaders responsible for determining final colors per-pixel after rasterization has occurred. The key distinctions between these two approaches are summarized below: #### Control Over Processing Stages - In the fixed-function approach, each stage operates according to pre-established rules without any direct intervention possible beyond setting parameters. - Conversely, under the programmable model, one writes code directly controlling what happens during both vertex transformations and fragment shading processes via shaders written in GLSL. #### Flexibility & Customizability - Limited customization options exist when working strictly inside a rigid framework provided solely by built-in functionalities offered by earlier APIs. - Enhanced capabilities allow artists/developers full access to modify nearly every aspect related to visual effects generation including but not limited to implementing non-standard algorithms unachievable otherwise due to constraints imposed previously upon them. #### Performance Considerations - While simpler applications may benefit slightly better performance-wise out-of-the-box because they do not require compilation time associated with writing new shaders, - Complex scenes requiring intricate interactions among multiple elements often see significant improvements once optimized specifically tailored towards target hardware characteristics thanks largely in part owing much credit given hereafter attributed primarily toward leveraging modern GPU architectures effectively. ```cpp // Example Vertex Shader Code Snippet #version 330 core layout(location = 0) in vec3 position; void main() { gl_Position = vec4(position, 1.0); } ``` ```cpp // Example Fragment Shader Code Snippet #version 330 core out vec4 FragColor; void main(){ FragColor = vec4(1.0f); // White color output } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值