C# Complete Tutorials – 35 – Passing Arrays as Function Arguments [w/ subs]



00:00:00 – in this video we are going to talk about
00:00:02 – passing arrays into functions so
00:00:04 – individuals to do delete all the
00:00:07 – existing code in a template and we'll
00:00:08 – just make everything happen from scratch
00:00:09 – so you're going to say link system like
00:00:13 – that so external functionality brought
00:00:15 – into the program then class program open
00:00:18 – close curly braces within our class
00:00:21 – we're going to define a function and
00:00:23 – then main okay so the function that we
00:00:25 – defined will be static void an
00:00:27 – array-like this summer I guess this will
00:00:30 – go with over an array and final entries
00:00:32 – in the some of the entries inside their
00:00:34 – way all right so you're going to type
00:00:36 – here the following that's the most like
00:00:41 – this and then feed ok imagine we collect
00:00:45 – some fees get some money from somebody
00:00:47 – about something so we're going to find
00:00:49 – the sum like that and also put out total
00:00:53 – like this an open close like that day
00:00:55 – again okay so we have an out parameter
00:00:57 – in other words going to return a value
00:00:59 – to the calling code is the purpose of it
00:01:01 – now to find us some you'll use it a for
00:01:05 – each loop so you're going to type does
00:01:06 – decimals sum equals zero okay so some
00:01:10 – variable like this hang in the type for
00:01:13 – each so tab tab like that's a fridge
00:01:16 – variable item in the collection I don't
00:01:20 – like the way to place it there but it's
00:01:21 – okay in our case you're going to type
00:01:24 – decimal because that's the type of the
00:01:26 – data stored inside their fees array and
00:01:29 – then going to say here we call that D or
00:01:32 – maybe for HP like that there you go in
00:01:35 – plz like that there you go yes I imagine
00:01:38 – each entry is a fee I was collected
00:01:40 – that's that then you will type here some
00:01:43 – plus equals and i will say fee goes out
00:01:48 – a semicolon right so this here is going
00:01:50 – to accumulate all of the fees until the
00:01:52 – final thumbs found okay one the sum of
00:01:56 – everything like that that's the purpose
00:01:58 – of that line now the end we're going to
00:02:00 – do of course is returned so does that
00:02:02 – you're going to type that the total over
00:02:04 – here equals the sum like that and so
00:02:08 – return some through
00:02:11 – the total parameter to the out
00:02:14 – parameters you have it that's how you
00:02:16 – return it like that ok and here i
00:02:18 – specify the type there you go that's the
00:02:23 – purpose of this first function remember
00:02:25 – taken it takes an array and that
00:02:28 – parameter equal total it finds the sum
00:02:31 – and then we turn the sum through the out
00:02:34 – parameter called total okay it's the
00:02:37 – purpose all right that's that now let me
00:02:41 – delete this and really dandeli that i'll
00:02:46 – meet this all right next we're going to
00:02:50 – code up the main so that means you going
00:02:52 – to type static void main and no argues
00:02:56 – this time just static void main a
00:02:57 – sufficient remember this is the hook
00:02:59 – point into the program is what the
00:03:01 – operating system needs are you gonna say
00:03:03 – that's the mode total like that so this
00:03:07 – would be an output ok so how variable
00:03:10 – like this they going to say make that
00:03:12 – race four decimal like that ok ARR like
00:03:17 – that so declare right now let's
00:03:21 – initialize it also ok so these decimal
00:03:25 – are like that and then declare the array
00:03:27 – and I are going to say this next ARR
00:03:33 – like that ok rather equals new decimal
00:03:38 – go w late make it five entries and
00:03:44 – specify them after that so 55 m and then
00:03:48 – 10 m + 25 m and say 35 mm and then 100m
00:03:53 – calls out of semicolon there you go i
00:03:55 – guys simple enough there you so now what
00:03:59 – you're going to do is we have the
00:04:01 – entries right we see that there again
00:04:03 – you're going to find the sum of the
00:04:06 – entries so through that look like this
00:04:08 – array summer like that specify that I
00:04:14 – raised in our case that's a RR and then
00:04:16 – comma and then out and then total close
00:04:20 – out the semicolon a yell and now to
00:04:23 – display this from
00:04:24 – you will type console the right line and
00:04:28 – then some else placeholder close up the
00:04:33 – comma and then cuddle like that close
00:04:39 – this with a semicolon day of simple
00:04:41 – enough okay now obviously something with
00:04:43 – some serious in a drawback for example
00:04:45 – she'd be able to collect the entries
00:04:46 – from the user and I'll have to hard code
00:04:47 – them correct the code that I originally
00:04:49 – have and then i will post there as a
00:04:51 – challenge perhaps as that okay so i'll
00:04:55 – specify that in a lecture description
00:04:57 – all right for the code that you do for
00:05:00 – the you know fine month the function on
00:05:03 – top is perfectly fine as it is that you
00:05:05 – don't have to change just the stuff that
00:05:07 – goes inside men you'll have to change
00:05:09 – all right so give this a go now so
00:05:11 – builded let me sure it takes mega go
00:05:17 – debug in step into like that all right
00:05:23 – let me bring this up here like that
00:05:29 – alright need penet so control function
00:05:34 – of eight observe the behavior now there
00:05:38 – we are so first as you can see we've
00:05:41 – made our array so if you expand that
00:05:43 – there are the entries inside array there
00:05:45 – you go all right now you're going to
00:05:47 – pass this over into static void erase
00:05:50 – summer so if you have a mouse over it
00:05:53 – tells you what's inside fees you see
00:05:54 – that like that anything with the total
00:05:57 – call dog vs 0 you see they again now set
00:06:01 – that equal to 0 and I use the for each
00:06:03 – here to grab each entry from the array
00:06:06 – and then add it to the thumb and you
00:06:08 – repeat this and repeat this let's repeat
00:06:10 – this and repeat that and repeated
00:06:14 – collecting everything in the some
00:06:16 – variable until the end now remember then
00:06:19 – at this stage over here right somerset
00:06:23 – to the total which is out variable and
00:06:25 – that's split let's spit out back to here
00:06:28 – where it's highlighted right now now you
00:06:30 – have it here so now you can print that
00:06:31 – and that's it for the sum is 975 all
00:06:36 – right
00:06:36 – close this that's a simple code of
00:06:39 – course because this is money you want to
00:06:40 – put a colon see there to format it as
00:06:43 – currency they go so you have this code
00:06:45 – available a sidebar it's what i want to
00:06:48 – show you when you do the assignment it's
00:06:51 – a considerable exercise what i want you
00:06:53 – to do is you can keep this top that's
00:06:55 – fine if there is nothing to change there
00:06:57 – the only difference is you should be
00:06:59 – able to have a the user-specified so
00:07:01 – many entries to collect then you have to
00:07:03 – ensure that the entries entered our
00:07:05 – convertible to decimal form so tryparse
00:07:09 – is the way to go ok if it's convertible
00:07:12 – successfully then you enter it into
00:07:14 – their array at the end this bit down
00:07:17 – below here this below with this is a
00:07:21 – sign okay that shouldn't change so you
00:07:24 – have to add some logic right here so
00:07:27 – this is the magic logic the magic logic
00:07:30 – right there ok number collect the
00:07:32 – entries ensure each entry as a center is
00:07:35 – convertible to numerical form store it
00:07:37 – in the array at the end call this
00:07:40 – function right here that doesn't change
00:07:41 – ok but there's some you know magic logic
00:07:44 – you got to add so that's it thanks very
00:07:46 – much


Video Url:
http://youtu.be/fh-aNUFxzGI

Comments

comments