C
C#2y ago
Darma

❔ 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
Darma
DarmaOP2y ago
public static int linearSearch(int[] nums, int t, int c)
{
bool isFound = false;

for (int i = 0; i < nums.Length; i++)
{
c++;

if (t == nums[i])
{
isFound = true;
Console.WriteLine("Found!");
break;
}
}

return c;
}
public static int linearSearch(int[] nums, int t, int c)
{
bool isFound = false;

for (int i = 0; i < nums.Length; i++)
{
c++;

if (t == nums[i])
{
isFound = true;
Console.WriteLine("Found!");
break;
}
}

return c;
}
Thinker
Thinker2y ago
Nothing will happen with the parameter you call the method with.
Doombox
Doombox2y ago
in this case it's important to understand how value types actually work if you want to understand the mechanics of what's happening
Thinker
Thinker2y ago
this has nothing to do with value types just passing by value
Doombox
Doombox2y ago
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 returned
Darma
DarmaOP2y ago
Ah thanks
trunksvn
trunksvn2y ago
😳
Doombox
Doombox2y ago
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 confusing
Darma
DarmaOP2y ago
yeah 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
Doombox
Doombox2y ago
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 it
Darma
DarmaOP2y ago
ah so it would in fact return let's say "5" if the loop took 5 tries to get to the target number then
Doombox
Doombox2y ago
yes my question is why would you pass c into the method? surely that should always start at 0?
Darma
DarmaOP2y ago
int count = 0;
int target = 15;
int[] array = new int[40];
linearSearch(array, target, count);
int count = 0;
int target = 15;
int[] array = new int[40];
linearSearch(array, target, count);
Doombox
Doombox2y ago
yeah if the count should always start at 0, then there's no real need for the method to accept it
public static int linearSearch(int[] nums, int t)
{
int c = 0;
c++; // etc...
return c;
}
public static int linearSearch(int[] nums, int t)
{
int c = 0;
c++; // etc...
return c;
}
Darma
DarmaOP2y ago
OH Man
Doombox
Doombox2y ago
then what you want is int count = linearSearch(array, target); because your method returns the count
Darma
DarmaOP2y ago
ok that makes so much sense lol
Doombox
Doombox2y ago
your way could work if you used ref or out but there's no reason to do that returning the number makes way more sense
Darma
DarmaOP2y ago
btw what are ref and out
MODiX
MODiX2y ago
Doombox#1389
REPL Result: Success
void AddOne(ref int val) => val++;
int i = 1;
AddOne(ref i);
Console.WriteLine(i);
void AddOne(ref int val) => val++;
int i = 1;
AddOne(ref i);
Console.WriteLine(i);
Console Output
2
2
Compile: 602.973ms | Execution: 37.258ms | React with ❌ to remove this embed.
Doombox
Doombox2y ago
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 stuff
MODiX
MODiX2y ago
Doombox#1389
REPL Result: Success
var num = "123";
if(int.TryParse(num, out var parsed))
{
Console.WriteLine(parsed);
}
var num = "123";
if(int.TryParse(num, out var parsed))
{
Console.WriteLine(parsed);
}
Console Output
123
123
Compile: 538.635ms | Execution: 36.933ms | React with ❌ to remove this embed.
Doombox
Doombox2y ago
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 future
Darma
DarmaOP2y ago
alright, thank you very much
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server