# Does writing code for an assignment after seeing similar code online count as plagiarism?

In my compilers class assignment, we were asked to write some MIPS code. One of the problem involved implementing `fact()` function as a sub-problem. I wanted to implement the standard recursive version

``````int fact ( int n )
{
if ( n == 0 || n == 1 )    return 1;
return n * fact(n-1);
}
``````

As I am new to MIPS, I looked up here on how to implement recursive routines. Unfortunately, the link explained recursion using `fact()` function itself. As I didn't want to "copy" code, I just understood the concept of recursion and wrote the `fact` myself.

But my `fact` code seems very similar to the one I looked up to learn recursion in MIPS. Is it for the fact that I glanced at the code already or the fact that factorial is really a very simple function with hardly 10 lines in it? Am I guilty of plagiarism here?

Here is my code

``````fact:   addi \$sp, \$sp, -8       # decrement SP to make room for pushing input var and ret addr
sw \$ra, 4(\$sp)          # save \$ra
sw \$a0, 0(\$sp)          # save input on stack

li   \$v0, 1             # Set v0 to 1 initially.
beq  \$a0, 0, fact_ret   # if ( n == 0 )    return 1;
beq  \$a0, 1, fact_ret   # if (n == 1) return 1;

jal fact                # Recursive call fact(n-1)
lw  \$a0, 0(\$sp)
mult \$v0, \$a0           # n * fact(n-1)
mflo \$v0                # Save answer in v0
fact_ret:
lw \$ra, 4(\$sp)          # restore \$ra
addi \$sp, \$sp, 8        # restore \$sp
jr \$ra                  # return from function
``````
• There's only one standard way to write "Hello World" in any language (don't play tricky, please). For a function so simple, I'd be more concerned if your function looked nothing like a standardized example. Feb 10, 2015 at 19:17
• The best defense again plagiarism is attribution. You can always add a note to your assignment (e.g. in a comment) to say that you consulted a source, wrote the code separately, and the results ended up being similar. Then the professor will make the decision. If the professor is approachable, you could also ask about this during office hours. Feb 11, 2015 at 11:27