Exit a function

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 2 days old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Exit a function

Post by sekou2331 »

What is the best way to exit a function to move to the next function? Is it return or break. Read break to is to come out of a loop and return is to come out of a function is this true. For example, say the below is in a function if the folder doesn't exist I want to come out of the current function and move to the next function.

For example:
  1. if (!(Test-Path $backUpPath -PathType Container)){
  2.  
  3. return or break
  4. }else{
  5.  
  6. #Do something
  7.  
  8. {
  9.  
  10.        
  11.  
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Exit a function

Post by jvierra »

"break" does not exit a function. It exits a block of code. "return" exits a function.

Try looking these up in help to get all of teh info on how these statements are intended to be used.
yettatoler
Posts: 1
Last visit: Mon Mar 22, 2021 2:17 am

Re: Exit a function

Post by yettatoler »

jvierra wrote: Mon Mar 01, 2021 4:29 pm "break" does not exit a function. It exits a block of code. "return" exits a function.

Try looking these up in help to get all of teh info on how these statements are intended to be used.
That is exactly what I was thinking. Thank you for the reply.
This topic is 3 years and 2 days old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked