C# Complete Tutorials – 36 – Params Keyword in Function Headers [w/ subs]



00:00:00 – in this video we are going to talk about
00:00:02 – the parens keywords so kind of visual
00:00:04 – studio make a project what is the reason
00:00:07 – this keyword allows us to pass a
00:00:11 – variable number of arguments into a
00:00:13 – function in other words it's very
00:00:17 – flexible and I have a lot of flexibility
00:00:19 – to our code let's take a look at our
00:00:20 – someone you know cool examples so type
00:00:23 – the following using system like there's
00:00:26 – some none class program like that okay
00:00:30 – now type here the parentheses I'm sorry
00:00:34 – the curly braces parentheses and that
00:00:37 – type of static integer summer Rae's and
00:00:40 – now say parens like that of the keyword
00:00:45 – of interest and an object open close
00:00:49 – square brackets and the next like this
00:00:52 – okay so here was going to make make a
00:00:56 – couple notes first of all static we've
00:00:57 – seen before but enter here means this
00:01:01 – function returns of value to the calling
00:01:06 – code okay that's it now and the parents
00:01:13 – keyword is very important here because
00:01:15 – param indicates that the number of
00:01:19 – arguments passed in can change it's not
00:01:23 – that two things have to go with a five
00:01:25 – things you can send in five or ten or
00:01:26 – twenty five into the function I have a
00:01:29 – lot of flexibility to to the codes in
00:01:32 – other words that's a great benefit of
00:01:33 – having this keyword alright so next
00:01:36 – let's take a look at this in action
00:01:37 – you're going to type and to just sum
00:01:40 – equals zero so this would be here some
00:01:45 – variable right this will store some and
00:01:47 – then what we are going to do is this
00:01:49 – going to type for each int like this and
00:01:53 – then put that there so for each array of
00:01:57 – integers index like this in other words
00:02:01 – x
00:02:07 – is an array of integers okay but its
00:02:11 – stores other arrays in other words x at
00:02:15 – 0 represents one array X 1 represents
00:02:17 – another array x2 represents another
00:02:19 – array in other words this thing is an
00:02:21 – array of arrays that's the definition of
00:02:23 – Exodus context ranks is an array of
00:02:26 – arrays that's how you think of X in this
00:02:28 – context okay all right now notice if you
00:02:31 – place your cursor over summer Rae's over
00:02:33 – here it says not all code paths you turn
00:02:36 – a value that means you have to specify
00:02:38 – between ridge return so if I return and
00:02:41 – then some like that now I have typed
00:02:47 – before each here for the following
00:02:49 – reason before each allows us to grab
00:02:54 – each array and X and then some it and so
00:02:59 – on and so on you'll see how this works
00:03:01 – once we run with the debugger we'll see
00:03:03 – how cool this is all right now you're
00:03:05 – going to say something relatively easy
00:03:07 – so for type 4 and then type following
00:03:16 – let's do it this way I'll show you so
00:03:20 – the words for each and then type integer
00:03:22 – like this tonight I in are like that so
00:03:29 – now we are nesting for for each loop so
00:03:33 – you can also do this with an inner for
00:03:35 – loop but I'm I'm choosing to do it with
00:03:37 – an inner for each loop okay so that
00:03:40 – looks like this now you would say the
00:03:41 – following some plus equals and then I
00:03:45 – quote that with a semicolon like this ok
00:03:48 – so the outer for each group serves the
00:03:51 – purpose of grabbing a single array out
00:03:52 – of X the inner for loop serve the
00:03:56 – purpose of summing the values within
00:03:59 – that array this for each from the values
00:04:05 – in that array that we have grabbed
00:04:07 – that's the purpose of it and then at the
00:04:11 – end we have returned some that means we
00:04:13 – will return the sum of all the entries
00:04:15 – alright so to make use of this code now
00:04:17 – we're going to type the following
00:04:20 – let me delete the code that i have done
00:04:22 – below and once you run it with the
00:04:23 – debugger you'll see everything happened
00:04:26 – much more clearly so what abstract at
00:04:28 – first it's a challenge of programming in
00:04:31 – general correct you can say static void
00:04:33 – main that's it keep it really simple
00:04:35 – static void main Oh args open closed
00:04:38 – parenthesis and now let me show you the
00:04:41 – power of that code above this is what
00:04:43 – you'll do type int like that and now
00:04:49 – type here a arm I'm sorry type a r1 and
00:04:52 – now a new integer array make it seven
00:04:55 – long and now open closed with a
00:04:58 – semicolon and that was initialized that
00:05:01 – array so one two three make sure it's
00:05:05 – seven entries so let's see one two three
00:05:09 – four five six seven entries ate whatever
00:05:11 – they are they don't have to be in
00:05:13 – sequence or anything in the mantiger are
00:05:15 – two equals new int and then say only
00:05:21 – three for that one and then time type in
00:05:25 – some numbers it doesn't matter there you
00:05:26 – go close out the semicolon what we want
00:05:29 – to do now is find the sum of their
00:05:35 – raised like that find some of their race
00:05:37 – for the code that I have above will do
00:05:39 – that for us okay so take a look when you
00:05:42 – run it with the debugger now so the
00:05:45 – objective is you have pure Rayz find the
00:05:46 – sum of the arrays and the way to do that
00:05:49 – is using this function that I've created
00:05:50 – above so now you are going and of course
00:05:53 – will print the sum so type here CW tab
00:05:56 – tab and now find some write some is
00:05:59 – placeholder as usual so you can place it
00:06:02 – at runtime comma and now call the
00:06:05 – function above what the two arrays so
00:06:07 – you will say some arrays like that and
00:06:15 – then passenger to erase if sometimes
00:06:17 – with the intellisense to escape but just
00:06:19 – give hit escape because you can get in
00:06:21 – their way sometimes it's a little
00:06:22 – annoying it's almost too eager at times
00:06:24 – and now you'll say a r1 and then they
00:06:26 – are two like that okay in other words
00:06:30 – take a look I am passing in two values
00:06:32 – and each
00:06:33 – the values is an array now I look very
00:06:36 – careful you how this will work so debug
00:06:39 – and step into now look at this code here
00:06:44 – all right for make this first I'd make
00:06:47 – the second array if I expand these you
00:06:49 – see they have been made as you can see
00:06:51 – this in the locals window alright next
00:06:54 – I'm calling summer Rae's so now if you
00:06:57 – take a look at X take a look at X X sub
00:07:00 – zero index is 0 and RX actually stores a
00:07:04 – whole another array how do i know if i
00:07:07 – expand this look at this right there you
00:07:10 – see that that's a whole other array that
00:07:12 – is stored inside axe at index 0 see ya
00:07:15 – there you go at index 1 inside axe thats
00:07:18 – throws another array see that so now i
00:07:22 – should be able to extract each array and
00:07:24 – work with it individually so i can do
00:07:26 – that take a look if you look for each
00:07:30 – integer array index take a look what I
00:07:33 – have just done is now ARR refers to the
00:07:37 – first array inside X you see all of
00:07:38 – those entries are accessible now like
00:07:41 – that and now I can form that some I can
00:07:50 – go to the entries in the first step I
00:07:51 – using the other four for each loop like
00:07:55 – that okay and now I grabbed the second
00:07:58 – array like that and then I go through
00:08:02 – those entries and have second araina add
00:08:04 – them and at the end i returned the sum
00:08:07 – there you go and the sum has been
00:08:10 – returned okay and I can print it now if
00:08:18 – i expand the window over here you see so
00:08:22 – the sum is 45 there you go okay that's
00:08:25 – the sum 45 that's a trivial thing but
00:08:28 – it's unloaded of course now we should
00:08:31 – test our code against something you know
00:08:33 – basic math to be sure this actually
00:08:34 – works she's always test your code
00:08:37 – against something really easy so for
00:08:40 – example if i add these up you know make
00:08:41 – sure that they check okay so one and
00:08:43 – three is four and four is eight and then
00:08:46 – 5 13 and 6 is 9
00:08:47 – teen and five is twenty four and six is
00:08:49 – thirty and 5 is 35 and who is 40 foot
00:08:55 – I'm sorry and 7 is 42 and three is 45 so
00:08:58 – checks you can check out any calculated
00:09:00 – to okay and now the power of the parents
00:09:03 – keyword is the following we did this
00:09:05 – would too but guess what if he added 10
00:09:07 – more arrays then once I have the real
00:09:10 – power right up the flexibility of this
00:09:12 – keyword okay imagine I make one here
00:09:15 – that looks like this comment reason
00:09:16 – right like this we throw in some numbers
00:09:18 – there whatever they are doesn't matter
00:09:20 – it goes out to send me coach make sure
00:09:24 – there are 10 of them one two three four
00:09:27 – five six seven eight nine ten the only
00:09:30 – modification have to make is I specify
00:09:33 – that array 3 should be passed as the fit
00:09:36 – the rest of it will work exactly as it
00:09:39 – as no other modifications are required
00:09:41 – for this code to return the some
00:09:44 – perfectly ok and now when I run this
00:09:47 – code because I am passing in three
00:09:51 – arrays this code up here the X that will
00:09:54 – store three arrays also i will not go
00:09:57 – into this one in such detail but just to
00:09:59 – show you just to prove you this works
00:10:01 – take a look all right with ads that pass
00:10:05 – it in expand next in the locals window
00:10:08 – as you can see index 0 and x stores the
00:10:13 – first array index one says the second
00:10:14 – array index to Sosa third array there
00:10:17 – you go see this this is an array of
00:10:18 – arrays all right that is it for this
00:10:21 – video so thanks very much have put a lot
00:10:23 – of comments in here and this is the top
00:10:28 – of the code down to line 19 roughly and
00:10:30 – let me move it down and then from line
00:10:32 – 19 down it looks like this they got us
00:10:36 – the heart of it and it's in the sidebar
00:10:37 – the PDF so please pipe it and run it
00:10:40 – with the debugger as I've shown you it
00:10:42 – will be very helpful


Video Url:
http://youtu.be/ncHvH1SEMZA

Comments

comments