Didn't understand the concept taught in this video completely

I’m not able to map out the working of ampersand operator in the bubble_sort function in this video. Can you please explain this concept in a bit more detail. Thankyou

Hey @chaturvedia336
This thread discuss that :Sorting/sort function/comparator

I read this thread, it’s still a bit unclear. The problem is that in the function declaration line of bubble_sort we have used pass by reference, while calling the bubble_sort function in main we are directly passing the comparator function as an argument.

This is what I’m having difficulty understanding

Hey,
Let me explain you in simple terms
int &i =j;
Here i is an alias to j i.e they both are same but two different names . Like we can have one official name and one nick name
Now how we pass argument in function using above concept:
solve(int &i){
}
main(){

solve(j);
}
This is how we pass j and receive it in i.

Similarly here we are passing function instead of integer and collecting it in an alias .

That is exactly what I’m asking, why are we even using the alias if they both are same with two different names.
Can we discard &cmp altogether and just use compare instead, in the function declaration line as well as the function call line for bubble_sort in main() ?

Yes we can
bool compare(int a,int b){
return a<b;
}

void solve(bool (cmp)(int a,int b)){
cout<<cmp(5,6);
return ;
}

int main() {
solve(compare);
}

This will work fine ,We use &(i.e pass by reference) so that memory is not wasted.

This answered a lot of what I asked. Just one more thing, in the declaration statement of solve(), can we pass the function compare directly[like you did while calling solve() in main()], instead of what you’re doing[i.e, declaring the placeholder cmp()]. And if not, can you please tell why? … Thanks & Regards

Hey
I am not able to understand what you are asking so please give an example for the same

For example, like in the example that you mentioned [in which you used solve() ] can I write it as—
bool compare(int a,int b){
return a<b;
}

void solve(compare){
cout<<compare(5,6);
return ;
}

int main() {
solve(compare);
}

Basically I’m asking whether we can directly pass the compare() as a parameter in our solve() or not.Please explain this. Thanks & Regards.

Hey yes we can do that but collecting parameter in you code segment is not correct.

bool compare(int a,int b){
return a<b;
}

void solve(bool compare(int a,int b)){
cout<<compare(5,6);
return ;
}

int main() {
solve(compare);

}

This is the correct implementation of the same

I did not understand this line. Can you please elaborate?

This is the correct format to collect compare. and not void solve(compare)

Correct format as in this is the syntax?

yeah I meant correct syntax

So basically we can do void solve(compare), but void solve(bool compare(int a, int b)) is the correct syntax , hence we use the latter…
Correct?

If you will write this ,this will give you compilation error

So the correct syntax to receive a function in argument is this

I hope this is what you are asking.

Okay. Got that…
Thanks a lot for the help. Much appreciated :slight_smile:

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.