DEV Community

ANKIT DABRAL
ANKIT DABRAL

Posted on

My First Practice with Functions in C++

Today I learned about functions in C++ and practiced using them to break problems into smaller, reusable pieces.

I worked through four exercises:

Sum of digits: Wrote a function that takes a number and returns the sum of its digits.

Calculate nCr: Used a factorial helper function to calculate the binomial coefficient.

Check for a prime: Created a function to check whether a number is prime.

Print primes up to N: Used a function to find and print all prime numbers in a range.

These exercises helped me practice passing values into functions, returning results, and calling helper functions from other functions. I’m also learning how separating a task from main() can make a program easier to follow.

One idea I’m taking forward: functions help organize a solution into clear steps. I’ll keep practicing and improve these solutions as I learn more.

Top comments (0)