❔ Parameter Shenanigans
I'm working on a linear search method that will return the count (number of iterations it took to find the target), the parameters are obviously the int table, the target number "t", and the count "c". I'm wondering if the c is incremented inside the method's for-loop, will the returned "c" be the new altered "c" or will it be the one that was used as an input for the function to be called.
25 Replies
Nothing will happen with the parameter you call the method with.
in this case it's important to understand how value types actually work if you want to understand the mechanics of what's happening
this has nothing to do with value types
just passing by value
sure, phrasing
once
c
is passed to the method, only the value is actually passed, because int
is a value type
so whatever happens to be assigned to c
by the return c;
statement is what will be returnedAh thanks
😳
if the
int
parameter c
was actually a reference type, just a class called Foo
, then (assuming you didn't directly assign something new to that reference) you would actually be returning a reference to the original object
we also have ref
and out
keywords for parameters which can make things a bit more confusingyeah it is a bit confusing
so from what I can understand is that since I'm declaring count as an int variable it won't have any actual changes unless I redefine it before the return
but that would be hard to do since count is being altered inside a for loop and I won't be able to access it once I go outside the loop's scope
c++;
this is essentially shorthand for c = c + 1;
in this case the value of c
is scoped to the method, so wherever you modify this value in the scope of the method, c
will always represent whatever value you've assigned to itah so it would in fact return let's say "5" if the loop took 5 tries to get to the target number then
yes
my question is why would you pass
c
into the method? surely that should always start at 0?yeah if the count should always start at 0, then there's no real need for the method to accept it
OH
Man
then what you want is
int count = linearSearch(array, target);
because your method returns the countok that makes so much sense lol
your way could work if you used
ref
or out
but there's no reason to do that
returning the number makes way more sensebtw what are ref and out
Doombox#1389
REPL Result: Success
Console Output
Compile: 602.973ms | Execution: 37.258ms | React with ❌ to remove this embed.
it allows you to actually treat value types as if they were references to that value
instead of just the value itself
out
is kinda old school and is mainly used for the TryParse
pattern stuffDoombox#1389
REPL Result: Success
Console Output
Compile: 538.635ms | Execution: 36.933ms | React with ❌ to remove this embed.
out
is important to understand because TryParse
is very commonly used, but you shouldn't have to create methods of your own that use them really, there are generally better and simpler ways to achieve similar results
they have uses, but you'll get to that in the futurealright, thank you very much
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.