* calling function inside procedure call @ 2014-10-03 19:50 Stribor40 2014-10-03 19:56 ` Adam Beneschan 2014-10-03 20:42 ` Marius Amado-Alves 0 siblings, 2 replies; 9+ messages in thread From: Stribor40 @ 2014-10-03 19:50 UTC (permalink / raw) my skeleton code looks something like this..... procedure Something --some declarations -- -- function myFunction (a: in String) return Boolean; function myFunction (a: in String) return Boolean is begin Put(a,3); return true; end; begin ... .. myFunction("Hello World"); ... .. end Something; i am getting error "cant use function myFunction in a procedure call. How would I make a call to myFunction? ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: calling function inside procedure call 2014-10-03 19:50 calling function inside procedure call Stribor40 @ 2014-10-03 19:56 ` Adam Beneschan 2014-10-03 19:57 ` Stribor40 2014-10-03 20:42 ` Marius Amado-Alves 1 sibling, 1 reply; 9+ messages in thread From: Adam Beneschan @ 2014-10-03 19:56 UTC (permalink / raw) On Friday, October 3, 2014 12:50:33 PM UTC-7, Stribor40 wrote: > my skeleton code looks something like this..... > procedure Something > --some declarations > -- > -- > function myFunction (a: in String) return Boolean; > function myFunction (a: in String) return Boolean is > begin > Put(a,3); > return true; > end; > begin > ... > .. > myFunction("Hello World"); > ... > .. > > end Something; > > i am getting error "cant use function myFunction in a procedure call. > > How would I make a call to myFunction? By doing something with the function result. E.g.: B := myFunction("Hello World"); where B is a Boolean variable. Ada doesn't let you just call a function and ignore the result. -- Adam ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: calling function inside procedure call 2014-10-03 19:56 ` Adam Beneschan @ 2014-10-03 19:57 ` Stribor40 0 siblings, 0 replies; 9+ messages in thread From: Stribor40 @ 2014-10-03 19:57 UTC (permalink / raw) On Friday, 3 October 2014 15:56:14 UTC-4, Adam Beneschan wrote: > On Friday, October 3, 2014 12:50:33 PM UTC-7, Stribor40 wrote: > > > my skeleton code looks something like this..... > > > > > procedure Something > > > > > --some declarations > > > -- > > > -- > > > > > function myFunction (a: in String) return Boolean; > > > > > function myFunction (a: in String) return Boolean is > > > begin > > > Put(a,3); > > > return true; > > > end; > > > > > begin > > > ... > > > .. > > > myFunction("Hello World"); > > > ... > > > .. > > > > > > end Something; > > > > > > i am getting error "cant use function myFunction in a procedure call. > > > > > > How would I make a call to myFunction? > > > > By doing something with the function result. E.g.: > > > > B := myFunction("Hello World"); > > > > where B is a Boolean variable. Ada doesn't let you just call a function and ignore the result. > > > > -- Adam sweet thank you so much ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: calling function inside procedure call 2014-10-03 19:50 calling function inside procedure call Stribor40 2014-10-03 19:56 ` Adam Beneschan @ 2014-10-03 20:42 ` Marius Amado-Alves 2014-10-04 1:07 ` Stribor40 2014-10-04 1:08 ` Stribor40 1 sibling, 2 replies; 9+ messages in thread From: Marius Amado-Alves @ 2014-10-03 20:42 UTC (permalink / raw) Did you know you can declare local procedures as you do functions? ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: calling function inside procedure call 2014-10-03 20:42 ` Marius Amado-Alves @ 2014-10-04 1:07 ` Stribor40 2014-10-04 3:33 ` Shark8 2014-10-04 1:08 ` Stribor40 1 sibling, 1 reply; 9+ messages in thread From: Stribor40 @ 2014-10-04 1:07 UTC (permalink / raw) On Friday, 3 October 2014 16:42:43 UTC-4, Marius Amado-Alves wrote: > Did you know you can declare local procedures as you do functions? no.....how would that be done and what would be the benefits....i am still having troubles figuring out when to use function and when to use procedure....As of right now on my baby steps in ada i base it on if i need to return something or now. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: calling function inside procedure call 2014-10-04 1:07 ` Stribor40 @ 2014-10-04 3:33 ` Shark8 2014-10-04 4:31 ` Jeffrey Carter 0 siblings, 1 reply; 9+ messages in thread From: Shark8 @ 2014-10-04 3:33 UTC (permalink / raw) On 10/3/2014 6:07 PM, Stribor40 wrote: > On Friday, 3 October 2014 16:42:43 UTC-4, Marius Amado-Alves wrote: >> Did you know you can declare local procedures as you do functions? > > no.....how would that be done and what would be the benefits? Well consider something kinda complex, say parsing: You have a function Parse which takes a string and parses it into a type containing several components: subtype String_4 is String(1..4); Type Some_Record is record A, B : Integer; C : Boolean; D : String_4; end record; -- ... Parse_Error : Exception; Function Parse( Input : String ) return Some_Record; -- in the body... Function Parse( Input : String ) return Some_Record is Working : Ada.Strings.Unbounded.Unbounded_String:= Ada.Strings.Unbounded.To_Unbounded_String(Input); -- All Get_Segment functions consume the front-end of Working Function Get_Segment return Integer; Function Get_Segment return Boolean; Function Get_Segment return String_4; -- IMPLEMENTATIONS OF GET_SEGMENT GO HERE. begin return Some_Record'( A => Get_Segment, B => Get_Segment, C => Get_Segment, D => Get_Segment ); end Parse; As you can see, this turns the actual body portion of the parsing into something quite small: a single statement. (I could have put it all on one line.) This method allows you to compose the parse function of smaller functions, functions which are not visible [or accessible] outside of the scope of the function. This means you can contain things, and you will be able to change them freely as you are guaranteed that these nested functions will not be used elsewhere. > i am still having troubles figuring out when to use function and when to use procedure Simple rule: does it return a value? If so it's a function, if not procedure. (Yes, the IN OUT and OUT modes on procedures muddy things a little, as does allowing functions to have OUT and IN OUT modes; don't worry about these too much.) > As of right now on my baby steps in ada i base it on if i need to return something or not. That's generally the correct approach. There are some subtleties, like constrained and unconstrained [sub]types that change things: if you're returning an unconstrained type it /must/ be a function. -- But that can, and should, wait until you get comfortable with constrained/unconstrained. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: calling function inside procedure call 2014-10-04 3:33 ` Shark8 @ 2014-10-04 4:31 ` Jeffrey Carter 2014-10-04 6:12 ` Shark8 0 siblings, 1 reply; 9+ messages in thread From: Jeffrey Carter @ 2014-10-04 4:31 UTC (permalink / raw) On 10/03/2014 08:33 PM, Shark8 wrote: > > return Some_Record'( > A => Get_Segment, > B => Get_Segment, > C => Get_Segment, > D => Get_Segment > ); You're relying on the order of evaluation, which is an error. The compiler may evaluate the expressions for the record components in any order, which is clearly not acceptable here. -- Jeff Carter "Strange women lying in ponds distributing swords is no basis for a system of government." Monty Python & the Holy Grail 66 ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: calling function inside procedure call 2014-10-04 4:31 ` Jeffrey Carter @ 2014-10-04 6:12 ` Shark8 0 siblings, 0 replies; 9+ messages in thread From: Shark8 @ 2014-10-04 6:12 UTC (permalink / raw) On 10/3/2014 9:31 PM, Jeffrey Carter wrote: > On 10/03/2014 08:33 PM, Shark8 wrote: >> >> return Some_Record'( >> A => Get_Segment, >> B => Get_Segment, >> C => Get_Segment, >> D => Get_Segment >> ); > > You're relying on the order of evaluation, which is an error. The compiler may > evaluate the expressions for the record components in any order, which is > clearly not acceptable here. > Good catch; I "overoptomized". In the code that I have the components are local variables, so there's no order of evaluation issue. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: calling function inside procedure call 2014-10-03 20:42 ` Marius Amado-Alves 2014-10-04 1:07 ` Stribor40 @ 2014-10-04 1:08 ` Stribor40 1 sibling, 0 replies; 9+ messages in thread From: Stribor40 @ 2014-10-04 1:08 UTC (permalink / raw) On Friday, 3 October 2014 16:42:43 UTC-4, Marius Amado-Alves wrote: > Did you know you can declare local procedures as you do functions? no.....how would that be done and what would be the benefits....i am still having troubles figuring out when to use function and when to use procedure....As of right now on my baby steps in ada i base it on if i need to return something or now. ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2014-10-04 6:12 UTC | newest] Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2014-10-03 19:50 calling function inside procedure call Stribor40 2014-10-03 19:56 ` Adam Beneschan 2014-10-03 19:57 ` Stribor40 2014-10-03 20:42 ` Marius Amado-Alves 2014-10-04 1:07 ` Stribor40 2014-10-04 3:33 ` Shark8 2014-10-04 4:31 ` Jeffrey Carter 2014-10-04 6:12 ` Shark8 2014-10-04 1:08 ` Stribor40
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox