بدايةً,  

التابع, الدالة, method, function

كلها اسماء لها نفس المعنى,

التابع هو عملية رياضية او منطقية معينة, لها مدخلات ولها مخرجات,

ميزة التابع الاساسية هي القابلية لاعادة الاستخدام مرارا وتكرارا, يعني بامكاننا عمل تابع يحسب مساحة دائرة, ومن ثم استخدامه عشرات المرات, فقط نعطيه مدخلات ويعطينا المخرجات, المدخلات تكون قيمة نصف القطر والمخرجات هي مساحة الدائرة.

وايضا الميزة الاخرى المهمة للتابع هي انه يسهّل تقسيم البرنامج الى اجزاء, بحيث يتشارك عدة مبرمجين على عمل البرنامج, يعني كل مبرمج يتولى كتابة عدد من التوابع ومن ثم في النهاية يتم جمع كل التوابع مع بعضها ببرنامج واحد.

 

مثال على تابع يقوم بطباعة جملة hello world:

public class Methods_in_java {
    
    public static void printHello()//function definition
    {//function body
System.out.println("hello world");
    }
  
    public static void main(String[] args) {
   
printHello();  //function call
printHello();  //function call
printHello();  //function call
  }

}

 

 برنامج الة حاسب بسيطة, باستخدام مبدأ التوابع methods :

import java.util.Scanner;

public class Methods_in_java {
    
    public static int sum(int num1,int num2)
    {
        int result=num1+num2;
        return result;
    }
   
    public static int subtract(int num1,int num2)
    {
        int result=num1-num2;
        return result;
    }
   
    public static int multiply(int num1,int num2)
    {
        int result=num1*num2;
        return result;
    }
   
    public static int divide(int num1,int num2)
    {
        int result=num1/num2;
        return result;
    }
  
    public static void main(String[] args) {
   
    //write simple calculater program using methods in java.
    Scanner input=new Scanner(System.in);
    System.out.println("welcome, please enter the first number: ");
    int num1=input.nextInt();
    System.out.println("please enter the second number: ");
    int num2=input.nextInt();
    System.out.println("choose operation you want to apply:\n1.summation\n2.subtraction\n3.multiplication\n4.division");
    int choice=input.nextInt();
    int result=0;
    if(choice==1)
    {
        result=sum(num1,num2);
    }
    else if(choice==2)
    {
        result=subtract(num1,num2);
    }
    else if(choice==3)
    {
        result=multiply(num1,num2);
    }
    else if(choice==4)
    {
        result=divide(num1,num2);
    }
    System.out.println("result is "+result);
    }

}

 

 

 

تابع method يقوم بترتيب 3 اعداد من الاصغر للاكبر ومن ثم طباعتهم:

//(Sort three numbers) Write a method with the following header to display three numbers in increasing order:
//public static void displaySortedNumbers( int num1, int num2, int num3)

//Write a test program that prompts the user to enter three numbers and invoke the method to display them in increasing order.

class Main {

public static void displaySortedNumbers(int a,int b, int c)
{
  int min, max, med;
if( a > b ){
 if( a > c ){
  max = a;
  if( b > c ){
   med = b;
   min = c;
  }else{
   med = c;
   min = b;
  }
 }else{
  med = a;
  max = c;
  min = b;
 }
}else{
 if( b > c ){
  max = b;
  if( a > c ){
   med = a;
   min = c;
  }else{
   med = c;
   min = a;
  }
 }else{
  med = b;
  max = c;
  min = a;
 }
}
System.out.println(min+"\n"+med+"\n"+max);
}

  public static void main(String[] args) {
    System.out.println("Hello world!");
    
    displaySortedNumbers(543, 334, 765);
  }
}

 

ابحث عن مسائل برمجة جافا | Java programming بالانجليزي

هل أعجبك المحتوى؟

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!

التعليقات
لا يوجد تعليقات
لاضافة سؤال او تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول