bahjat Posted May 4, 2015 Posted May 4, 2015 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 expandcollapse popup;;;****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
SadBunny Posted May 4, 2015 Posted May 4, 2015 (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 May 4, 2015 by SadBunny Roses are FF0000, violets are 0000FF... All my base are belong to you.
bahjat Posted May 4, 2015 Author Posted May 4, 2015 (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 May 4, 2015 by bahjat
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now