Hello Evelynn,

welcome in the forum. You will have to look at the problem more "statistically" and less from the `R`

point of view. If you remember *Bayes Theorem*, you know that

P(A|B) = \frac{P(B|A) \cdot P(A)}{P(B)}

holds true for A and B not independent (otherwise P(B \cap A) = P(B) \cdot P(A) and the whole thing is trivial). Since there is no requirement on MC simulation, you can "hard code" the corresponding PDF of the sum of two dice (P(B)) as well as the conditional probability to obtain a sum S given dice throw D1 (P(S|D1)).

The joined probability density function of the sum of two die throws is just given with the discrete probability mass function P(S = 2) = \frac{1}{36}, P(S = 3) = \frac{2}{36}, \dots. For any given sum S on the other hand, there is only one possible value of D2 given D1 to obtain S. Hence, the conditional probability P(S|D1) is either \frac{1}{6} or 0, depending on the event D1 and its range (e.g. if D1 = 4 the probability of S to be 11 or greater i 0, since there is no 7 on a 6-sided die - but if S = 7, D2 has to be 3 but the probability of D2 to be 3 is just \frac{1}{6}).

With this, you can code your function to return a conditional probability given the input of S and D1:

```
prob_function <- function(D,S){
# Vector of P(S)
prob_S <- c(1:6,5:1)/36
# Check, if the sum is possible
if ((S - D) %in% 1:6){
# Conditional probability of D given S
(1/6 * 1/6) / prob_S[[(S - 1)]]
} else {
cat('No valid input.')
}
}
prob_function(D = 5, S = 5)
#> No valid input.
prob_function(D = 1, S = 3)
#> [1] 0.5
```

^{Created on 2022-10-09 with reprex v2.0.2}

The function contains a sanity check just to be sure the input is actually possible (otherwise the results would make no sense (e.g. there is no P(S = 13) or P(S = 8|D = 1)).

I hope this makes it clear four you, how to write a custom function but also the logic behind the calculations in this example.

Kind regards