Function Pointers for beginners! How and when to use Function Pointers?

preview_player
Показать описание
📚 Learn how to solve problems and build projects with these Free E-Books ⬇️

Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

Function pointer is a special type of pointer that is used in order to store the address of a function.
Function pointer syntax is complex and many beginners find it hard to understand.
In this video, I'll explain how to use function pointers in C++ and what is their purpose.
I'll show you how to create a function pointer, how to invoke a function using a function pointer, and also how to use and create function pointers to functions that receive parameters/arguments.
Function pointers are often used in order to optimize the code and make it reusable, and in this video, I'll explain how to use a function pointer in order to pass a function as a parameter/argument to another function.

Contents:
00:00 - Intro to function pointers
01:26 - What are function pointers?
03:40 - How to create a function pointer?
05:25 - How to invoke a function using a function pointer?
06:10 - Pointer to a function with parameters
09:01 - Why and when to use function pointers?
16:12 - How to pass a function as a parameter/argument to another function?

Follow me on other platforms:

*******CODE IS IN THE COMMENTS*******
Рекомендации по теме
Комментарии
Автор

📚 Learn how to solve problems and build projects with these Free E-Books ⬇️
Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

#include <iostream>
#include<vector>
using namespace std;

bool ascendingCompare(int a, int b) {
return a < b;
}
bool descendingCompare(int a, int b) {
return a > b;
}

void sortAscending(vector<int>& numbersVector)
{
for (int startIndex = 0; startIndex < numbersVector.size(); startIndex++)
{
int bestIndex = startIndex;

for (int currentIndex = startIndex + 1; currentIndex < numbersVector.size(); currentIndex++)
{
// We are doing comparison here
if (ascendingCompare(numbersVector[currentIndex], numbersVector[bestIndex]))
bestIndex = currentIndex;
}

swap(numbersVector[startIndex], numbersVector[bestIndex]);
}
}
void sortDescending(vector<int>& numbersVector)
{
for (int startIndex = 0; startIndex < numbersVector.size(); startIndex++)
{
int bestIndex = startIndex;

for (int currentIndex = startIndex + 1; currentIndex < numbersVector.size(); currentIndex++)
{
// We are doing comparison here
if (descendingCompare(numbersVector[currentIndex], numbersVector[bestIndex]))
bestIndex = currentIndex;
}

swap(numbersVector[startIndex], numbersVector[bestIndex]);
}
}


void customSort(vector<int>& numbersVector, bool(*compareFunctionPtr)(int, int))
{
for (int startIndex = 0; startIndex < numbersVector.size(); startIndex++)
{
int bestIndex = startIndex;

for (int currentIndex = startIndex + 1; currentIndex < numbersVector.size(); currentIndex++)
{
// We are doing comparison here
if (compareFunctionPtr(numbersVector[currentIndex], numbersVector[bestIndex]))
bestIndex = currentIndex;
}

swap(numbersVector[startIndex], numbersVector[bestIndex]);
}
}


void printNumbers(vector<int>& numbersVector) {
for (int i = 0; i < numbersVector.size(); ++i)
cout << numbersVector[i] << ' ';
}

int main()
{
vector<int> numbersVector = { 4, 2, 1, 3, 6, 5 };



bool (*funcPtr)(int, int) = descendingCompare;
customSort(numbersVector, funcPtr);
printNumbers(numbersVector);

system("pause>0");
}

CodeBeauty
Автор

Saldina, I said it in the chat and I will say it here: your videos are well worth watching in my opinion as you are concise, thorough, and pleasant to listen to. Please keep up the excellent work!!

NipkowDisk
Автор

Great video, but some aprovements.
First a typedef helps a lot with the strange syntax. For example:
typedef bool (*Compare_T)(int, int);
Compare_T myFuncPtr;
Sort( vector<int>& Numbers, Compare_T OnCompare );

Second why don't you pass your compare function directly?
Sort( myNumbers, AscendingCompare );

sanderverhage
Автор

Saldina! We love you all around the World! Cheers from Argentina!

mexxiano
Автор

I'm self-learning C++ and your videos are really helpful. This is a very clear explanation about function pointers. Thank you so much for your hard work =)

streambumper
Автор

bool compare(int a, int b){ return a < b;}
sort(arr.begin(), arr.end(), compare);
I was always wondering how this function works and today I have learned it from you. Thank you for your all videos <3 I have learned many things from you. And please continue the OOP series.

nishaternotes
Автор

the countless embedded code I've read through and copied and pasted that refused to work because of missing functions that pointers were looking for...I never understood the problem until now...Thank you for the detailed video and the channel, I'm slowly working through all your tutorials!....Beyond that..uff that accent <3

smoothoctopus
Автор

This video is such a good one. I spent 2 hours trying to do the function as a parameter until I think of this video and solved my issue in 4 minutes. You’re awesome Saldina ❤

remimonsel
Автор

I used to hate using Pointers but I start to love it now

This vid pushes my peak interest further

HoangNguyen-xxs
Автор

I'm a C++ beginner and this was well done. This was very easy to follow and I will be tuning in for more videos.

samuelholmes
Автор

Thanks Saldina for explaining the function pointer concept with proper example. Please keep making such videos.

shashankcool
Автор

Excellent video. I think it's simpler and pratical to call the compare function directly in customSort. Like this:

int main(){
vector<int> numbersVector = {4, 2, 1, 3, 6, 5};
customSort(numbersVector, descendingCompare);
printNumbers(numbersVector);
system("pause>0");
}

pedrolobo
Автор

After these videos, i'm feeling myself like senior C++ Developer:) Thanks!

MegaAtlus
Автор

Thank you. My professor assigned a function pointer assignment, but he didn't have time to really explain it. This really helped :-)

sirmovielover
Автор

excellent video, little by little I'm starting to love this girl, because of how smart she is and the dedication that she puts into each video. Thank you for taking the time to publish this kind of videos. much that helps many of us to better understand

jhordyperez
Автор

Hi, I am from Taiwan. Thank you very much! Your tutorial is always very clear! I love the style you teach!

lindeanchuang
Автор

Been 20 years ago when I wrote my last c/c++ program at school. But this serie makes me want to code some old school tasks

MrPassyu
Автор

Thank you for all the videos, they already taught and helped me a lot🥰 and you are kind of someone I look up to soo, just thank you and have a nice day

JC-pxmk
Автор

Function pointers are awesome. That's programming art. Every time, I think I know everything but luckily with you and your channel, there is always something more.

remimonsel
Автор

Great videos, hope you complete 100k subscribers soon❣️

muhammadumair