#1
|
||||
|
||||
![]()
مرة أخرى الدالة والقيمة الراجعة (Return Value)
هذه ترجمة لأحد مشاركات الأستاذ / سيف عسى أن تنفعنا فى فهم الإعلان عن الدالة وإستدعائها : ================================================== ======== Function procedure performs a task and then returns control to the calling code. When it returns control, it also returns a value to the calling code. إجراء الدالة تنفذ مهمة ثم يعود التحكم إلى كود الاستدعاء.عندما يعود التحكم، فإنها أيضا ترجع قيمة إلى كود الاستدعاء.Each time the procedure is called, its statements run, starting with the first executable statement after the Function statement and ending with the first End Function, Exit Function, or Return statement encountered. في كل مرة يتم استدعاء الإجراء، جملها تنفذ ،بدءا من جملة التنفيذ الأولى بعد جملة الدالة وتنتهي مع أول End Function ، و Exit Function ،أو جملة Return.You can define a Function procedure in a module, class, or structure. It is Public by default, which means you can call it from anywhere in your application that has access to the module, class, or structure in which you defined it. يمكنك تعريف إجراءالدالة في وحدة نمطية module ، وتصنيف class ،أوبنية.Structure هى بصفة عامة افتراضياً،مما يعني أنه يمكنك إستدعاؤها من أي مكان في التطبيق الخاص بك للوصول إلى الوحدة النمطية module ، والتصنيف class ،أوالبنية.Structure الذي قمت بتعريفه عليه.A Function procedure can take arguments, such as constants, variables, or expressions, which are passed to it by the calling code. إجراءالدالة يمكن أن تأخذ قيم للمعاملات arguments ،مثل الثوابت constants والمتغيرات variables ،أوالتعبيرات expressions ،التي يتم تمريرها إليه بواسطة كود الاستدعاء.Declaration Syntax بناء جملة الإعلان The syntax for declaring a Function procedure is as follows: بناء جملة الإعلان عن إجراءالدالة يتم على النحو التالي:[modifiers] Function function name[(parameter list)] Asreturn type 'جمل إجراء الدالة.' Statements of the Function procedure. End Function الصفات التعريفية تمكن من تحديد مستوى الوصول والمعلومات المتعلقة بثقل التحميل، والهيمنة، والمشاركة،وظلها. لمزيد من المعلومات ، انظر Function Statement (Visual Basic).The modifiers can specify access level and information regarding overloading, overriding, sharing, and shadowing. For more information, see Function Statement (Visual Basic). You declare each parameter the same way you do for Sub Procedures. تقوم بتعريف كل معامل parameter بنفس الطريقة التي تفعلها للإجراءات الفرعية.Data Type نوع البيانات كل إجراء دالة له نوع بيانات، تماما كما لكل متغير. يتم تحديد نوع البيانات هذا من قبل كلمة As في إجراء الدالة ، وهو (نوع البيانات ) يحدد نوع البيانات للقيمة الراجعة للدالة إلى أمرالاستدعاء.المثال التالى يوضح هذا الإستدعاء .Every Function procedure has a data type, just as every variable does. This data type is specified by the As clause in the Function statement, and it determines the data type of the value the function returns to the calling code. The following sample declarations illustrate this. Copy Code لمزيد من المعلومات أنظر الأجزاء فىFunction Statement (Visual Basic)Function yesterday() As Date End Function Function findSqrt(ByVal radicand As Single) As Single End Function For more information, see "Parts" in Function Statement (Visual Basic). Returning Values إرجاع القيمThe value a Function procedure sends back to the calling code is called its return value. The procedure returns this value in one of two ways: قيمة إجراء الدالة يرسل مرة أخرى إلى كود الإستدعاء ويطلق عليه قيمتها الراجعة. الإجراء يقوم بإرجاع هذه القيمة في واحدة من طريقتين:1) It assigns a value to its own function name in one or more statements of the procedure. Control does not return to the calling program until an Exit Function or End Function statement is executed. The following example illustrates this. • يتم تعيين قيمة إلى دالة خاصة بها اسمها في واحد أوأكثرمن عبارات الإجراء.التحكم لا تعيد إلى برنامج الاستدعاء حتى يتم تنفيذ Exit Function أو عبارةEnd Function . المثال التالي يوضح هذا.Function function name[(parameterlist)] As returntype 'الجملة التالية لا تنقل التحكم مرة أخرى إلى كود الاستدعاء.' The following statement does not transfer control back to the calling code. function name = expression 'عندما يعود التحكم إلى كود الاستدعاء ، التعبير هي القيمة الراجعة.' When control returns to the calling code, expression is the return value. End Function وتستخدم عبارة الرجوع إلى تحديد القيمة الراجعة ، ويعود التحكم مباشرة إلى برنامج الاستدعاء .المثال التالي يوضح هذا.2) It uses the Return statement to specify the return value, and returns control immediately to the calling program. The following example illustrates this. Function function name[(parameter list)] As return type ميزة تعيين قيمة راجعة إلى اسم الدالة هو أن التحكم لايعود من الإجراء حتى يصادف Exit Function أو عبارة End Function. هذايسمح لك لتعيين قيمة أولية وضبطها في وقت لاحق إذا لزم الأمر.' The following statement immediately transfers control back to the calling code and returns the value of expression. 'الجملة التالية ينقل على الفور التحكم مرة أخرى إلى كود الإستدعاء وإرجاع قيمة التعبير. Return expression End Function The advantage of assigning the return value to the function name is that control does not return from the procedure until it encounters an Exit Function or End Function statement. This allows you to assign a preliminary value and adjust it later if necessary. For more information, see "Return Value" in Function Statement (Visual Basic). لمزيد من المعلومات أنظر"Return Value" in Function Statement (Visual Basic).Returning Arrays إعادة المصفوفة إذا كان إجراءالدالة يرجع نوع بيانات المصفوفة،لا يمكنك الوصول إلى العناصرالفردية للمصفوفة ضمن الدالة..إذا حاولت القيام بذلك،المترجم يفسر ذلك كاستدعاء الإعادة ( التكرار ) إلى هذا الإجراء.المثال التالي يوضح هذا.If the Function procedure returns an array data type, you cannot access the individual elements of the array within the function. If you attempt to do this, the compiler interprets it as a recursive call to the procedure. The following example illustrates this. Function allOnes(ByVal n As Integer) As Integer() فيالمثال السابق،يفسرالمترجم محاولة تعيين allOnes(i) = 1 كاستدعاء لـ allOnesعلى الجانب الأيسرمن عبارة التعيين . وهو يفسرالمحاولة Return allOnes() كاستدعاء من غير قيمة معامل .argument وكلا العبارتين توليد أخطاء المترجم.For i As Integer = 1 To n - 1 ' The following statement generates a COMPILER ERROR. ' العبارة التالية ينشأ عنها خطأ للمترجم allOnes(i) = 1 Next i ' The following statement generates a COMPILER ERROR. ' العبارة التالية ينشأ عنها خطأ للمترجم Return allOnes() End Function In the preceding example, the compiler interprets the attempted assignment allOnes(i) = 1 as a call to allOnes on the left side of an assignment statement. It interprets the attempted Return allOnes() as a call with no argument. Both statements generate compiler errors. Calling Syntax إستدعاء بناء الجملة استدعاء كإجراء الدالة متضمناً ذلك اسمها وقيمة كل المعاملات argumentsعلى الجانب الأيمن من عبارة التعيين أو في التعبير . يجب توفيرالقيم لجميع المعاملات التي ليست اختيارية ، ويجب عليك إحاطة قائمة الوسيطة بين قوسين. إذا لم يتم توفرالمعاملات ،يمكنك حذف الأقواس اختياريا.You invoke a Function procedure by including its name and arguments either on the right side of an assignment statement or in an expression. You must provide values for all arguments that are not optional, and you must enclose the argument list in parentheses. If no arguments are supplied, you can optionally omit the parentheses. The syntax for a call to a Function procedure is as follows: بناء الجملة من أجل الإستدعاء إلى إجراء الدالة على النحو التالي:lvalue = function name[(argument list)] عند استدعاء إجراء الدالة ، لم يكن متاح لك استخدام قيمتها الراجعة. إذا لم تقم بذلك ، يتم تنفيذ كافة إجراءات الدالة، ولكن يتم تجاهل القيمة الراجعة. وغالبا ما يستدعى الـMsgBox بهذه الطريقة.If ((function name[(argument list)] / 3) <= expression) Then When you call a Function procedure, you do not have to use its return value. If you do not, all the actions of the function are performed, but the return value is ignored. MsgBox is often called in this manner. Illustration of Declaration and Call توضيح لـ الإعلان والإستدعاءThe following Function procedure calculates the longest side, or hypotenuse, of a right triangle, given the values for the other two sides. إجراء الدالة التالية يحسب الضلع الأطول ، أو الوتر ، لمثلث قائم الزاوية، وبالنظر إلى القيم الأخرى للجانبين.Visual Basic Copy Code
Function hypotenuse(ByVal side1 As Single, ByVal side2 As Single) As Single Return Math.Sqrt((side1 ^ 2) + (side2 ^ 2)) End Function The following example shows a typical call to hypotenuse553b-4cb6-9. يظهر المثال التالي إستدعاء نموذجى لـ hypotenuse. Visual Basic Copy Code Dim test Length, test Hypotenuse As Single tes tHypotenuse = hypotenuse(test Length, 10.7) يوفر وقت تشغيل Visual Basic العديد من الدوال ، وتقع في مايكروسوفتVisual Basic Runtime Functions The Visual Basic runtime provides many functions, located in the Microsoft ms-help://MS.VSCC.v90/MS.msdnexpress.v90.en/dv_vbalr/html/1b9f632c-20a-ded117ead8c0.htm
__________________
يخطئ من يظن أن للذئاب ديناً
|
#2
|
|||
|
|||
![]()
طبعا نقل الموضوعات الى طلبات الاعضاء قضى عليها تماما
وطبعا نحن هنا فقط اقصد مصر اختلافنا في نوع القيمة الراجعة هل هو ما بعد as في سطر التعريف اما ما بعد as في تعريف المتغيرات داحل كود الدالة والى الان لم يتم الاتفاق واعتقد لن يتم
__________________
|
#3
|
||||
|
||||
![]()
فعلاً أستاذ / سيف لن نستطيع التوصل لحل تلك المشكلة
طالما لم يكن بين أيدينا المرجع الذى ينهى هذا الخلاف
__________________
يخطئ من يظن أن للذئاب ديناً
|
#4
|
||||
|
||||
![]()
ولكن هل يمكن أن نعرف السبب فى حدوث الخطأ فى هذا الكود . الذى أشار إليه الأستاذ تامر بسببه من قبل ولكن لم أستوعب أسباب هذا الخطأ
كود:
Dim A As Byte = 200 Dim B As Byte = 100 Dim C As Short C = A+ B MsgBox (C)
__________________
يخطئ من يظن أن للذئاب ديناً
|
#5
|
||||
|
||||
![]()
طريقة أخر لكتابة كود الدالة
كود:
__________________
يخطئ من يظن أن للذئاب ديناً
|
![]() |
العلامات المرجعية |
|
|