bahjat

Creating a function

3 posts in this topic

So i am trying to make a function out of many functions (i believe it is called recursion, read a post about it earlier on this forum)

When i try to make a number of things into 1 big function so i can call upon it later it doesn't seem to be working but when i take away the "func _hello()" and the "endfunc" from the end, everything seems to be working fine. Can someone please explain this to me. I know the problem is occurring because of the the "Conversion" function but i can't seem to understand why this is happening. Please help 

;;;****Program adds spaces *****
;;;***** the input variable here is $New*****
Global $final
Global $Hexadec
Func _hello()

$DataToBeDecrypted = "55fdaf fdafd"

$2space = $DataToBeDecrypted
$New = $2space

$AddingSpace = StringSplit($New, "")

$Final = ""

If Conversion($AddingSpace[0]) Then

    For $Spacing = 1 to $AddingSpace[0] Step 2
        $Final = $Final & $AddingSpace[$Spacing] & $AddingSpace[$Spacing+1] & " "
    Next

    MsgBox(0, "Adding space to the message so it can be converted back to Hex", $Final)

Else
    MsgBox(0, "Result", "String does not contain an even number of characters.")
EndIf

Func Conversion($Hexadec)
    Return Mod($Hexadec, 2) = 0
EndFunc
;;;***The final value is stored in the $final variable****


;***** Hexadecimals to ASCII*****
;;***Input variable is $HexadecimaltoASCII2******

$HexadecimalToASCII2 =$final
$HexadecimalsToASCII = ChrH($HexadecimalToASCII2)
$Ascii2Hex = Sub($HexadecimalsToASCII)
$v5ar = Chr($HexadecimalsToASCII);char

MsgBox(0,"Hex to ASCII",$HexadecimalsToASCII)

Func ChrH($v8)

    Local $v5=""
    $A1 = StringSplit($v8, " ")
    For $count = 1 To $A1[0]
        $v5 &= Chr(Dec($A1[$count]))
    Next
    Return $v5
endFunc


Func Sub($v8)

    Local $v9=""
    For $count = 1 To StringLen($v8)
        If StringLen(Hex(Asc(StringMid($v8, $count, 1)),2)) = 1 Then
            $v9 &=  "0" & Hex(Asc(StringMid($v8, $count, 1)))
        Else
            $v9 &=  Hex(Asc(StringMid($v8, $count, 1)),2)
        EndIf
        If $count <> StringLen($v8) Then $v9 &=  " "
    Next
    Return $v9
endFunc

;*****HEXADECIMAL to ASCII*****

EndFunc

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Your code is incorrect because a function cannot be inside another function. You can of course have multiple functions calling each other.

And it's not recursion... Recursion means a function (or a child of it) calls itself. Like this:

_test()

Exit

Func _test()

    MsgBox(0, 0, "Calculating '5!' (5 permutation, i.e. 5*4*3*2*1)")

    $answer = _recursivePermutationCalculator(5)

    MsgBox(0, 0, $answer)
EndFunc   ;==>_test

Func _recursivePermutationCalculator($i)
    ; Now *this* is a recursive function, because it calls itself.
    If $i = 1 Then
        ; Because a function is not allowed to be infinitely recursive
        ; (which we don't want anyway, as we need an answer to the math problem),
        ; we need an exit condition.
        Return 1
    Else
        ; We'll add our current input $i to the permutation of $i - 1
        ; ($i)! = ($i) * ($i - 1)!
        Return $i * _recursivePermutationCalculator($i - 1) ; <-- function calling itself!
    EndIf
EndFunc   ;==>_recursivePermutationCalculator

 

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I would like to say a big thank you, to you! That program you gave as an example was actually really helpful, much appreciated buddy. I also have another issue, do you think you can help me out on that too?

Your code is incorrect because a function cannot be inside another function. You can of course have multiple functions calling each other.

And it's not recursion... Recursion means a function (or a child of it) calls itself. Like this:

_test()

Exit

Func _test()

    MsgBox(0, 0, "Calculating '5!' (5 permutation, i.e. 5*4*3*2*1)")

    $answer = _recursivePermutationCalculator(5)

    MsgBox(0, 0, $answer)
EndFunc   ;==>_test

Func _recursivePermutationCalculator($i)
    ; Now *this* is a recursive function, because it calls itself.
    If $i = 1 Then
        ; Because a function is not allowed to be infinitely recursive
        ; (which we don't want anyway, as we need an answer to the math problem),
        ; we need an exit condition.
        Return 1
    Else
        ; We'll add our current input $i to the permutation of $i - 1
        ; ($i)! = ($i) * ($i - 1)!
        Return $i * _recursivePermutationCalculator($i - 1) ; <-- function calling itself!
    EndIf
EndFunc   ;==>_recursivePermutationCalculator

 

Edited by bahjat

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • Karnalsyn
      By Karnalsyn
      As with a lot of programming, the less manual repetition done to achieve the same outcome...the better.
      I package programs for network deployment at the organization I work for. Sometimes these applications require verbose messaging to the user, and sometimes by special request we have to turn that off and deploy silently.
      The way I currently handle these 2 scenarios is this.
      I have a function that builds the splashwin display screen to variable size depending on message length, and displays the desired message to the end user.
      Then at each point throughout my script as I need to make the user aware of what is currently happening, I inject a splashwin function call with the unique message pertaining to that specific event. ie. "Installing Microsoft Office, please wait..."
      In effort to build a more universal script to handle any type of request submitted. I've incorporated a switch command of '/silent'
      If cmdlineraw detects the usage of that switch when the script is triggered it sets a variable flag, ie. $silent = "on"
      Then at every splashwin call I make, I'm prefixing it with an IF statement that checks for the flag, and then does NOT display the message if that flag is "on". Or does display it if its not. Has worked for me just fine. But while I grow my programming skills and look for more streamlined ways of handling areas I find myself creating repetition. I'm curious if I can relocate that flag check. Taking it out of every single splashwin call, and injecting it into the function itself.
      Example of how I currently display or hide the splashwin based on command line...
      If StringInStr($cmdlineraw, "/silent", 0) Then
          $SilentSwitch = "ON"
      EndIf
      If $SilentSwitch = "OFF" Then SplashWin($Uninstalling & $ProductInfo[1][0])
      Some scripts have dozens of those splashwin calls littered throughout, so Is this the best approach I can take already? Or as stated earlier, can I pull the IF statement away from the splashwin call. And left the function as a whole either activate or deactivate as a result of the command line check.
    • wakillon
      By wakillon
      SciTE Hopper : Jump to Any line by one click and many other helpfull functions !


      As Ashalshaikh suggested me after leaving me the task to continue >SciTE Hopper, i open a new Topic about it.
      It will be more easy for futures updates.

      Many helpfull functions in contextual menu.
      SciTE Hopper is now multipurpose.
      • Color Catcher added
      • BinaryToAu3Kompressor added
      • TinyAu3Search replaced by StringFinder
      • RegJumper added
      • Edit With SciTE added (available in Options)
      • SpecialCharactersViewer added (available in Tray menu)
      New Contextual Menu :

      For a right Click on a function :

      • Copy function name
      • Insert a function header
      • Cut function
      • Copy function
      • Delete function
      • Create a variables List
      • Remove usseless Blank lines
      • Remove Debug lines ( all lines starting with ConsoleWrite or _ArrayDisplay )
      • Set AutoIt Code Layout
      • Remove Comments

      For entire script :

      • Create a functions List
      • Create a variables List
      • Remove usseless Blank lines
      • Remove Debug lines ( all lines starting with ConsoleWrite or _ArrayDisplay )
      • Set AutoIt Code Layout
      • Remove Comments
      • ( Open in SciTE a ) Duplicate of current Script in Temp Dir
      • ( Open in SciTE a ) Duplicate of current Script in Same Dir
      • Explore Parent Script Folder
      • Copy Script name
      • Copy Script path
      • Find Unused Functions
      • Find Unused Variables
      • Set all functions in alphabetic order.( func headers and regions ( in fact, all lines between functions ) are not supported.)

      SciTE Editor New Contextual Menu if SciTE Hopper is added ( in Options ) to SciTE Editor :
      when you made a selection in SciTE Editor you can :

      • Open a Reg Key in Regedit using RegJumper
      • Open a Url in your default Browser
      • Search on google in your default Browser
      • Search on AutoIt Website in your default Browser
      Now Can be added to windows start (minimized)
      Options are available by Tray Menu.
      A double click on tray icon will open clipboard content to SciTE Editor ( Handy when you have copied some code from your browser )

      How to proceed for add SciTE Hopper to SciTE Editor :
      Run Scite Editor, run Scite Hopper ( compiled! ) go to Options and select Add/Remove to SciTE Tab. then click on Add button and it's done !
      Right click on SciTE Editor and choose SciTE Hopper in Contextual Menu for run it when you need it !
      Update of 07 Dec 2017
      TinyAu3Search is replaced by StringFinder

      Previous downloads : 1287
      Script and compiled version are available in the download section
       
      Thanks to Ashalshaikh, Melba23, Th3 MMA, Alzri2, taitel, Manadar, taz742, Yashield, Authenticity, Xenobiologist for their help
      and asdf8 for his >Includes Helper and azjio for his >FileSearch.

      Hope it help you and happy Scripting !

       
    • mati
      By mati
      Hi,
      I wonder if
      AutoIt3Wrapper_run_debug_mode = Y can include run information about functions which are called within the main code. So far I noticed in the console that the function itself is called but no specific run information from  inside the function is provided. Is there a chance to show the entire debug log including functions in the console?
      I furthermore noticed a stop of information when a loop is entered. What could be the reason for that?
       
      Thanks for help.
    • TrashBoat
      By TrashBoat
      Is this possible:
      Executing a function from an include, but taking the function name from a gui input and then executing that function using the include:
      #include <Something.au3> ;input reads "Tree" $functionName = GuiCtrlRead($input1) $functionName(1) And the include is gonna have
      Func Tree($x) If $x = 1 Then $this = "text" MsgBox(0,$this,"whatever") EndFunc is it possible?
    • hcI
      By hcI
      Hello, i'm making a little script that lets users enter a question and if the script can't answer to, it ask what would be the answer (a bit like siri on IPhones). And I try to display the "supposed array" I still have an error.. I searched on Google and got nothing looked in the help file too.. I'm here to know if someone have a solution to my error please.
      Here is where $a and $b should become arrays :
      Dim $dossier = @AppDataDir & "\Siri2" Dim $chemin = $dossier & "\data.ini" Global $a = _InfoRead($chemin, 0) Global $b = _InfoRead($chemin, 1) with this function :
      Func _InfoRead($path, $mode = 0) If DirGetSize($dossier) = -1 Then Return SetError(-4) If Not FileExists($chemin) Then Return SetError(-5) Local $readed = IniReadSection($path, "data") If @error Then Return SetError(-3) $size = $readed[0][0] Local $return[$size] For $i = 1 To $size Step 1 $return[$i - 1] = $readed[$i][$mode] Next Return $return EndFunc and the thing that i don't understand is that, after _InfoRead return affected to $a, I can't use _ArrayDisplay because of  error 1 "$aArray is not an array" (and same fpr the variable $b)..
      Can please someone help me to understand why is it doing this ?

      -hcI