How to resolve the algorithm Function definition step by step in the Prolog programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Function definition step by step in the Prolog programming language

Table of Contents

Problem Statement

A function is a body of code that returns a value. The value returned may depend on arguments provided to the function.

Write a definition of a function called "multiply" that takes two arguments and returns their product. (Argument types should be chosen so as not to distract from showing how functions are created and values returned).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Function definition step by step in the Prolog programming language

Source code in the prolog programming language

multiply(A, B, P) :- P is A * B.

go :-
  multiply(5, 2, P),
  format("The product is ~d.~n", [P]).

test_multiply :-
  multiply(5, 2, 10),  % this will pass
  multiply(3, 4, 11).  % this will not pass

:- use_module(library(function_expansion)).

user:function_expansion(multiply(A, B), P, P is A * B).  % "function" definition

go :-
  format("The product is ~d.~n", [multiply(5, 2)]).

go :-
  A is 5*2,
  format('The product is ~d.~n', [A]).

  

You may also check:How to resolve the algorithm Sudan function step by step in the Nim programming language
You may also check:How to resolve the algorithm Jump anywhere step by step in the Relation programming language
You may also check:How to resolve the algorithm Respond to an unknown method call step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Animation step by step in the LabVIEW programming language
You may also check:How to resolve the algorithm Substring/Top and tail step by step in the jq programming language