• Functions

    A function in programming is a task you the program to perform. It tells the computer what to do and how to do it when it runs a certain program. It's kind of like telling a robot what to do. For example, if you want the robot to make something for you, you would give it instructions or write down a recipe on how to make that thing. That's what functions are like: instructions for the computer on how to make something happen.

      • Defined Functions

      • D

        Example of function in d.
        import std.stdio : write;
        
        int addition(int a, int b) {
        	return a + b;
        }
        
        void main()
        {
        	write(addition(1, 2)); // 3
        }
        
      • Anonymous Functions

      • D

        Example of anonymous function in d.
        import std.stdio : write;
        
        void main()
        {
        	(int a, int b) {
        		write(a + b); // 3
        	}(1, 2);
        }
        
      • Variable Functions

      • D

        Example of variable function in d.
        import std.stdio : write;
        
        void main()
        {
        	auto addition = (int a, int b) { 
        		return a + b; 
        	};
        
        	write(addition(1, 2)); // 3
        }