Function | Vyper (0.2) - العناوين المزدوجة

Hey, how's it going everyone?
In this video, let's go over how to write functions in Viper.
First I'll show you a basic example, and then we'll talk about visibility, and then talk about mutability.
visibility declares who can call this function and mutability declares if the function can write to the blockchain or not.
So write to blockchain question mark.
Alright, so first let me show you a basic of a function in Viper.
So, we'll say external, and then we'll say peer.
For now, don't worry what these declarations mean.
I'll explain them in details when we go over visibility and mutability.
All this is saying here is that this function can be called from outside of the contract
and here means that this will be a read-only function.
I'll name the function simple and the can take multiple inputs.
So for example we'll say x is of type un256 Another input named B will be a boolean and Lastly we'll turn in a string.
So we'll say s is uptight string and most 10 characters Now function can return multiple outputs and we can declare that by
typing a narrow and inside the parentheses we'll say the first output is UN256,
the second output will be a boolean and the third output will be a And that's how you declare a function signature.
Let's now write the body of the function.
For the first output, we'll need to return a unit.
So we'll say return parentheses.
And we'll just return x plus one.
We need a second output to be a Boolean.
So we'll just say not b.
Since B is a Boolean here, we're just negating it.
And lastly, we'll return a string by taking the original string and adding in some extra characters.
We can do that by saying concat,
parentheses, s, and then we'll put in a Alright, so let's just quickly go over what this function will do.
The first declaration, external, tells us that this function can be called from outside this contract.
Peer tells us that this function is read-only.
The name of the function is simple.
It takes in three inputs, uptight.
Uint, Boolean, and string.
For the output,
it will return a Uint,
Boolean, and also a string, and we return x plus one, not B, and string with a question mark by using the So that's a basic example.
Let's now talk about visibility.
Visibility declares that who can call this function.
So let me write this down.
Who can call this function.
And the two bizabilities that you can declare are either internal or external.
Internal means that the function can only be called inside this contract.
On the other hand, external means that that function can only be called from outside of this contract.
Let's start with, for example, the external function.
And again,
external means that that function can only be called from outside of the So we'll declare it as external and then say that this function is read only by saying
at view.
Later when we go over mutability, I'll explain the difference between peer and view functions.
We will name the function xthunk and for example we'll just take in a UN for input and for the
output it will return a UN.
For the output let's just multiply the input.
So you'll say return x multiplied by x.
So this is an example of an external function.
Now I'm going to show you an example of an internal function.
And this will give you a better understanding of the difference between internal and external functions.
Alright, so let's go over our example of our internal function.
And internal means that the function can only be called from inside this object.
We declare internal function by saying internal and we'll make this a pure function.
So it will be a read only function and we'll name it int func.
And for this example, we'll just take in two UNs, and then that's just output a UN to 56 and a Boolean.
And for the body of the function, we'll just return the addition of the image.
and that's just return true.
So what is the difference between internal and external functions?
Internal means that this function can only be called inside this contract.
So once this contract is deployed, we won't be able to call this init function.
On the other hand, external means that this function can only be called from outside of this contract.
So once the contract is deployed,
we will be able to call this function, but we won't be able to call this function inside the contract.
So for example, if you try to call our external function, inside the contract.
So here by saying self dot x funk 123.
Then this will not compile since you're trying to call external function, which can only be called from outside the contract.
On the other hand,
we can call this internal function inside here, so by saying self dot int func will pass in some random number one and two.
And this will compile.
since we're calling our internal function.
Now notice that this function is returning two outputs.
So I'm going to show you how to capture the output over here.
So first we'll need to declare some variables to capture the outputs.
The first output is a UNT.
So I'll just say aye.
is of type unit 256 and the initial value is 1.
The second output for the init function is a boolean.
So you'll capture the output in a variable named be type boolean And we need to set the initial value for this variable.
So for now, we'll just say false.
And to capture the output from this function, we'll type in parentheses.
And then the first output is a unit.
The second output is a Boolean.
And then put a equal sign.
So that's how you capture our output from another function.
All right, that's now talk about mutability.
And means does the function write to the blockchain?
So I'll write it here.
Does it write blockchain?
The four types of mutabilities are peer, view, non-payable, and payable.
Peer view are read-only functions.
The difference between peer and view is that peer does not read any state variables or any environment variables.
And environment variables are variables like message.center, message.value, block that timestamp.
Non-payable and payable declares whether a function can receive either or not.
Non as the name suggests, means that the function does not accept any.
On the other hand payable means that the function can accept it.
Let's see an example of a peer function.
So I'll declare as external and I'll declare as peer and we'll name it peerfunk.
And for the input, we'll just pass in a UNT, and it will output Boolean.
And we'll just return x greater than 2.
So why is this appear function?
well it's because it doesn't write anything to the blockchain and it does not read any state variables or environment variables.
Let's see an example of a view function in contrast.
So we'll say external and then declare We'll name it view font and for the input, we'll just also pass in a Uint.
For the output, we'll return a Uint and an address.
And we'll return x plus.
a state variable called self.num, which I'll define later.
And we'll also return the color of this function by saying message dot sender.
And back up top will declare a state variable num public Uint 256.
So let's go over the difference between peer and view function.
This is a view function because it does not write to the blockchain,
so it is a read-only function,
but it is not a peer function since it is reading a state variable called num and also accessing an environment variable called message.center.
These two are read-only functions,
so let's take a look at a function that writes to the So let's first declare a state variable to write to.
So back at top, I'll say message is equal to public string at most 10 characters.
We're going to scroll back down.
and we will define a function as external and it's not going to be a peer or a view function
since it's going to be right into the blockchain and I'll define it as write something
And it's going to take in the message string at most 10 characters and it returns nothing so I'll just put a semicolon.
We'll update the state variable message by saying self.message is equal to underscore message.
So this is a function that is not view or peer function, since it writes to the blockchain.
And it is also a non payable function, since it is not declared as payable.
And this means that you won't be able to send an eater to this function.
So let's now take a look at a function that is payable, which means that this function can accept either.
So you'll say external, and then say, we'll name it receive Eater.
The amount of Eater that was sent will be stored in a variable called message.value.
So for this example we'll store the value that was sent in a state variable.
value by saying self dot value is equal to message dot value.
And we haven't defined this state variable yet.
So I'll scroll up and then define value public human 256.
I'm to scroll back down.
So what this function does is when you call this function, we see beta and send some eater.
The amount of eater that was sent will be stored in a variable called message.value.
And then we'll store that value in a state variable called value.
So if you send one eater, this value will be equal to one eater.
If you send two eater, this value will be equal to two eater.
All right.
So that was an example of a payable function.
For the last example, I want to show you an example of a payable construct.
which means that you can send either when the contract is deployed.
So we'll say external and then also payable.
And then we'll define the constructor with def two underscores in it to underscore semicolon.
The account that created this contract will be stored in the variable called message.center.
So we'll store it in a variable called owner.
So we'll say self.owner is equal to message.center.
And since we haven't declared a state variable yet, I'll declare here by saying owner is equal to address.
And for this example,
I'll just capture the amount of ether that was sent when this contract was deployed by saying self,
the value is equal to message dot value.
Let's make sure that this contract compiles.
So I'm going to open my terminal and then type by per source from top by.
the contract compile.
So now see a demo in remix.
So I'm going to copy this code, open remix and then activate Viper.
Then create a Viper file called funk.by and then paste the code.
I'll compile this contract by clicking on the Viper icon, clicking on the new compiler, and then hit compile.
Once it's successfully compiled, we'll deploy the contract by clicking on this icon, and we'll deploy this contract, sending one.
and then hitting deploy.
Once the contract is deployed, I'm going to scroll down and you'll see the contract that was deployed.
So I'm going to click on the arrow.
And let's call some functions.
So I'm going to scroll down.
The owner state variable will be the address that deploy this contract.
So in this case, the owner will be this account over here and Remember that we sent one meter.
So let's check the value by clicking on this state variable called belly.
And we can see that one ether was set.
Now notice that we declared our internal function here.
So we shouldn't be able to call this function from outside of this contract.
And you can see here that we don't have any function called init func.
Alright, so that was an example of functions in Hyper.
For each function, you'll have to declare the visibility and the mutability.
Visibility declares who can call the function.
Can it be called from outside the contract, then you'll have to declare as external.
or if you're only going to call the function from within the contract then you'll have to declare as internal.
Mutability means does the function write to the blockchain.
Peer and view functions do not write to the blockchain, they are read-only functions.
The difference between the two is that view functions read from state and environment variables.
You can also declare a function to be payable and this means that that function will be able to accept either.
I hope you found this video

فتح المزيد من الميزات

قم بتثبيت ملحق Trancy لفتح المزيد من الميزات، بما في ذلك ترجمة العناوين الذكية، تعريفات الكلمات الذكية، تحليل القواعد النحوية الذكية، التحدث الذكي، وغيرها.

feature cover

متوافق مع منصات الفيديو الرئيسية

يوفر Trancy دعمًا للعناوين المزدوجة في منصات مثل يوتيوب، نتفليكس، يوديمي، ديزني بلس، تيد، إيدكس، كيهان، كورسيرا، بالإضافة إلى ترجمة الكلمات/الجمل الذكية، وترجمة النصوص الغامرة بالكامل، وميزات أخرى لصفحات الويب العادية. إنه مساعد شامل لتعلم اللغة.

جميع متصفحات المنصة

يدعم Trancy جميع متصفحات المنصة، بما في ذلك ملحق متصفح Safari لنظام iOS.

أوضاع عرض متعددة

يدعم وضع المسرح، وضع القراءة، وضع المزج، وأوضاع عرض أخرى لتجربة ثنائية لغوية شاملة.

أوضاع تدريب متعددة

يدعم تدريب الجملة، والتقييم الشفهي، والاختيار من متعدد، والاستماع والكتابة، وأوضاع تدريب أخرى.

ملخص الفيديو الذكي

استخدم OpenAI للملخصات الذكية للفيديوهات والتمسك بالمحتوى الرئيسي بسرعة.

العناوين الذكية

قم بإنشاء عناوين يوتيوب ذكية ودقيقة في 3-5 دقائق فقط.

تعريفات الكلمات الذكية

انقر على الكلمات في العناوين للبحث عن تعريفات، باستخدام تعريفات ذكية مدعومة بالذكاء الاصطناعي.

تحليل القواعد النحوية الذكية

تحليل قواعد الجمل لفهم معاني الجمل بسرعة واجتياز نقاط القواعد النحوية الصعبة.

المزيد من ميزات الويب

بالإضافة إلى العناوين المزدوجة في الفيديو، يوفر Trancy أيضًا ترجمة الكلمات وترجمة النصوص الكاملة لصفحات الويب.

جاهز للبدء

جرب Trancy اليوم واستمتع بميزاته الفريدة بنفسك

تحميل