Coding Challenge: The Swap Function

Write code that uses pass by reference to swap two variables.

Coding challenge: Swap

Congratulations on reaching the first coding challenge of the course!

In coding challenges, you have to write code yourself. Sometimes it will be a short function. Other times, it will be more complicated. Please take your time and don’t rush through these assignments. Try to solve them for a few days before checking the solution.

Problem statement

The task is to implement a swap function. It should receive two integer variables as arguments and swap their values.

Input

Two integer variables—a and b

Output

The function will not return anything, but it should swap the arguments. Moreover, the change should be visible inside the caller.

Sample input and output

Before swapping:

a = 5
b = 10

After swapping:

a = 10
b = 5

Implementation

Follow the TODO comments. If you are unfamiliar with TODO comments, programmers often use them to mark places in the code where they should make changes or improvements in the future. They serve as tags that you can search. Alternatively, any modern IDE will show you a nicely-formatted list of all TODO comments.

In assignments, they help learners find places where they should write their code.

Get hands-on with 1400+ tech skills courses.