ctf Lesson 5 54 min read

CTF: Parameters & Return Values

27 challenges — Week 4

Parameters & Return Values Challenges

27 challenges Week 4 Week 4 (C Control Flow, Functions, and Writing Real Programs)

Function parameter passing, return value handling, and function design exercises. Covers pass-by-value, reference parameters, cumulative algorithms, and practical function-based problem solving in C.


Difficulty Breakdown

Level Count
Beginner 18
Intermediate 9

Challenges

1. Average Of 3

Difficulty: Beginner Time: ~3 min Type: function implementation

Write a function that takes three integer parameters and returns their average as a double.

Concepts: function parameters (pass-by-value), return types, type promotion (int to double), floating-point division forcing with 3.0

Practice on CodeStepByStep


2. Binary To Decimal

Difficulty: Intermediate Time: ~10 min Type: function implementation

Write a function that takes an integer representing a binary number (e.g., 101100) and converts it to its decimal equivalent (e.g., 44).

Concepts: function parameters, modulo operator for digit extraction, accumulator algorithm, while loops, return values

Practice on CodeStepByStep


3. Bound Range

Difficulty: Beginner Time: ~4 min Type: function implementation

Write a function that takes an age (integer) and clamps it to the valid range [0, 110]. Returns 0 if age is negative, 110 if age exceeds 110, otherwise returns age unchanged.

Concepts: function parameters, conditional logic (if/else), comparison operators, return values, math library functions (min, max)

Practice on CodeStepByStep


4. Box Of Stars

Difficulty: Beginner Time: ~5 min Type: function implementation

Write a function that takes width and height parameters and prints a rectangular box of asterisks (*).

Concepts: function parameters, nested loops, printf for output, void return type

Practice on CodeStepByStep


5. Calculate Displacement

Difficulty: Beginner Time: ~4 min Type: function implementation

Write a function that calculates displacement using the kinematic equation: displacement = v0t + 0.5a*t^2, where v0 is initial velocity, a is acceleration, and t is time.

Concepts: function parameters (doubles), return type (double), math.h library (pow function), arithmetic operators, physics formula implementation

Practice on CodeStepByStep


6. Circle Area

Difficulty: Beginner Time: ~3 min Type: function implementation

Write a function that takes a radius (double) and returns the area of a circle using the formula A = π*r².

Concepts: function parameters (double), return type (double), math constants (M_PI), arithmetic operators, geometry formula implementation

Practice on CodeStepByStep


7. Coin Flip

Difficulty: Intermediate Time: ~12 min Type: function implementation

Write a function that simulates a coin flip until k consecutive flips of a given side are achieved. Returns the total number of flips.

Concepts: function parameters (int, char), while loops, random number generation (rand()), counter variables, conditional logic

Practice on CodeStepByStep


8. Compute Distance

Difficulty: Intermediate Time: ~5 min Type: function implementation

Write a function that computes the Euclidean distance between two 2D points using the formula: distance = sqrt((x2-x1)² + (y2-y1)²).

Concepts: function parameters (4 doubles), return type (double), sqrt() from math.h, pow() or arithmetic squaring, distance formula

Practice on CodeStepByStep


9. Days In Month

Difficulty: Beginner Time: ~4 min Type: function implementation

Write a function that takes a month number (1-12) and returns the number of days in that month. Ignore leap years; February always has 28 days.

Concepts: function parameters, if/else or switch statements, return values, month-to-days mapping

Practice on CodeStepByStep


10. Decimal To Binary

Difficulty: Intermediate Time: ~10 min Type: function implementation

Write a function that takes a decimal integer and converts it to an integer with binary-looking digits (e.g., 44 becomes 101100).

Concepts: function parameters, modulo operator (%), integer division, accumulator algorithm, while loops

Practice on CodeStepByStep


11. Digit Count

Difficulty: Beginner Time: ~6 min Type: function implementation

Write a function that takes an integer and returns the number of digits it contains. Negative numbers are handled by counting digits of absolute value.

Concepts: function parameters, while loops, integer division, counter variables, handling negative numbers

Practice on CodeStepByStep


12. Factor Count

Difficulty: Beginner Time: ~5 min Type: function implementation

Write a function that takes a positive integer and returns the count of its positive factors (divisors).

Concepts: function parameters, for loops, modulo operator for divisibility testing, counter/accumulator pattern, return values

Practice on CodeStepByStep


13. Factorial

Difficulty: Beginner Time: ~5 min Type: function implementation

Write a function that takes a non-negative integer n and returns its factorial (n! = n × (n-1) × … × 1). By definition, 0! = 1.

Concepts: function parameters, for loops, accumulator/product pattern, return values, edge case handling (0! = 1)

Practice on CodeStepByStep


14. Fitness Goal

Difficulty: Intermediate Time: ~12 min Type: function implementation

Write a function that prompts the user to enter daily exercise minutes until k consecutive days of increasing activity are achieved. Returns the total days tracked.

Concepts: function parameters, while loops, user input (scanf), consecutive sequence tracking, state variables (prev, count, days)

Practice on CodeStepByStep


15. Investment

Difficulty: Intermediate Time: ~20 min Type: program implementation

Write a multi-function program that calculates compound interest for two investors over a period of years and assigns a quality rating based on returns.

Concepts: multi-function program design, parameter passing, return values, compound interest formula, input/output

Practice on CodeStepByStep


16. Is Multiple

Difficulty: Beginner Time: ~3 min Type: function implementation

Write a function that takes two integers and returns 1 if the first is a multiple of the second, 0 otherwise. Handles zero edge cases.

Concepts: function parameters, modulo operator for divisibility, boolean logic (return 0 or 1), return values, edge cases (0, negative numbers)

Practice on CodeStepByStep


17. Is Prime Number

Difficulty: Intermediate Time: ~8 min Type: function implementation

Write a function that takes an integer and returns 1 if it is prime, 0 otherwise. By definition, primes are integers > 1.

Concepts: function parameters, for loops, modulo operator for divisibility, sqrt optimization (checking up to sqrt(n)), edge case handling

Practice on CodeStepByStep


18. Parameter Mystery2

Difficulty: Beginner Time: ~5 min Type: function tracing

Given a function with parameters in different order than typical, trace its output with specific input values.

Concepts: parameter passing, function tracing, order of operations, debugging and code comprehension

Practice on CodeStepByStep


19. Print Pay

Difficulty: Beginner Time: ~6 min Type: function implementation

Write a function that computes and prints an employee’s pay. Regular pay is hourly_rate * hours for up to 8 hours. Overtime (hours > 8) is paid at 1.5x the hourly rate.

Concepts: function parameters (int and double), conditional logic (if/else for overtime), arithmetic calculations, printf with currency formatting, void return type

Practice on CodeStepByStep


20. Quadratic

Difficulty: Intermediate Time: ~10 min Type: function implementation

Write a function that solves a quadratic equation ax² + bx + c = 0 using the quadratic formula, outputting the two roots via pointer parameters.

Concepts: function parameters (value and pointer), pointer dereferencing (* operator), quadratic formula, discriminant calculation, sqrt from math.h

Practice on CodeStepByStep


21. Receipt Results

Difficulty: Beginner Time: ~5 min Type: function implementation

Write a function that takes a subtotal amount and prints a receipt showing subtotal, tax (8%), tip (15%), and total.

Concepts: function parameters, arithmetic calculations (percentages), printf with currency formatting (%.2f), void return type, string concatenation in printf

Practice on CodeStepByStep


22. Show Twos

Difficulty: Beginner Time: ~6 min Type: function implementation

Write a function that takes an integer and prints all factors of 2 it contains, showing the factorization (e.g., 18 → ‘2 * 9’).

Concepts: function parameters, while loops, modulo for divisibility, printf for output, factor extraction

Practice on CodeStepByStep


23. Sum Of Digits

Difficulty: Beginner Time: ~6 min Type: function implementation

Write a function that takes an integer and returns the sum of its digits. For negative numbers, ignore the sign and sum digits of absolute value.

Concepts: function parameters, while loops, modulo for digit extraction, accumulator pattern, handling negative numbers

Practice on CodeStepByStep


24. Sum Of Range

Difficulty: Beginner Time: ~5 min Type: function implementation

Write a function that takes two integers (min and max) and returns the sum of all integers from min to max inclusive. If min > max, return 0.

Concepts: function parameters, for loops, accumulator pattern, conditional logic for edge cases, return values

Practice on CodeStepByStep


25. Three Consecutive

Difficulty: Intermediate Time: ~7 min Type: function implementation

Write a function that takes three integers and returns 1 if they form three consecutive numbers, 0 otherwise. Order doesn’t matter (e.g., 2, 4, 3 is valid).

Concepts: function parameters, finding min/max/mid of three values, conditional logic, return boolean values

Practice on CodeStepByStep


26. Triangle

Difficulty: Beginner Time: ~6 min Type: function implementation

Write a function that takes a size parameter and prints a right-aligned right triangle of asterisks. For example, size 5 prints 5 rows with 1, 2, 3, 4, 5 asterisks.

Concepts: function parameters, nested loops, printf for output, row-by-row construction, void return type

Practice on CodeStepByStep


27. Xkcd Dating Range

Difficulty: Beginner Time: ~5 min Type: function implementation

Write a function that computes the acceptable dating age range using the XKCD formula: min_age = age/2 + 7, max_age = (age - 7) * 2. Output via pointer parameters.

Concepts: function parameters (value and pointer), pointer dereferencing, arithmetic calculations, void return with output via pointers, fun application of formulas

Practice on CodeStepByStep