1 / 1100%
In this video, were going to discuss two functions that we wrote for you. Theyre really
wrappers. They wrap the logic needed to connect C++ to Python. The first function is called call
procedure. As you notice, it returns nothing. You pass to it a string. Well talk about that string
in a few minutes. The other function is declared twice. So we can see that its declared here
and its declared here.
The first declaration you pass a string and then string while the other declaration you pass a
string then an integer. This is called function overloading when you declare the same function
twice but you change the parameter types.
So how do we use them? Well if I want to call a function that does nothing other thansorry.
When we want to call a function that returns nothing but it does some work on the Python
site, we would use call procedure. So if we look at print something, lets go to the Python code.
Here we go. Print something. Here we go. Print something simply prints a message, but it
doesnt return anything. So lets comment this line and this line and lets run it. And here you
go. It just says hello from Python. Thats what that procedure does.
If you want to call a function that returns an integer, then we would use this function. And
when we want to pass a string, we would pass the function name in Python and the string
parameter. So were passing the word house to the function print me. So lets look at print me.
Here it is. V is going to receive the word house. This is going to say, youve sent me house
because V has the word house. And then, its going to return 100. I just made up this number.
You can return whatever you want.
So this 100 will replace this code and C++ will print 100. Lets run it. Here it is. You sent me
house and then C++ printed whatever got returned that was returned from the Python code.
Finally, if you want to call a function but instead of passing a string you want to pass an
integer, so you can still use the same function. Thats why its overloaded as you can see. Its
declared here twice, one with two strings, one with a string then an integer. Then you would
just call our function the name of the function in Python and you would send a number.
So this is a mult, its a function called mult. Lets look at it. Oh we dont have mult. OK. So
square value. Good. Lets go to it. Thats good actually. I had one before so its called mult. So
square value, 10. I passed 10. Lets look at this. 10 will be here in V. This will return 10 times
10. So lets run it. 10 times 10 is 100. OK?
So just to note, just to clarify so theres no confusion, why did it display 102 times? Because
this function return 100 really. Its just random. So lets just return 1 here. This function, I just
wanted to demonstrate that it can return a value. So let’s return 1 here. And here it will just
multiply. And instead of 10 let’s pass a 5 for example. If we run it, see? That function, this 1,
gave us a 1. The other call gave us 25.
Students also viewed