C program to calculate the sum of series using a predefined Function

Introduction to Functions in C

C programming allows developers to write reusable blocks of code called functions. Functions support separating complicated problems into more manageable logical parts. Defining functions that perform specific tasks reduces duplication of code. We just need to call the Function whenever required instead of writing the same code repeatedly.

Functions in C are defined once and can be executed from multiple places in a program. They accept inputs through parameters and return outputs using the return statement.

Understanding the Program

Review this sample C program to understand how functions help calculate the series sum. First, the main() method is the startup function that executes initially. It has the core program logic that:

  1. Declares necessary variables
  2. Prints output messages
  3. Calls the sum() function
  4. Stores and prints the result

Here, the main() function aims to get the user input, invoke the sum() function for calculation and display the output.

Declaring the sum() Function

The key function sum() is declared next. Function declarations inform the compiler about the existence of a function before it is defined.

The declaration specifies:

  1. Return type (int)
  2. Name of the Function (sum)
  3. Parameters (int n)

Remember that no function body is defined here. The definition comes later below main().

Calling the Function from main()

Inside the main() method, the program prompts the user to enter n and stores it in variable n. This value is passed to sum() through a function call.

The function call transfers program control from main() to sum(). The return value stored in the result is printed out as the final sum.

Defining the Function

The actual sum() function definition appears after main(). It contains the logic to:

  1. Initialize accumulator variable s to 0
  2. Run a loop from 1 to n
  3. Add each number to s
  4. Return the sum

When the sum() execution ends, the control returns to the calling main() function.

Using Functions for Code Reuse

Defining custom functions is integral to structured programming in C. It enables reusable and modular code to be written. Once functions like sum() are created, they can be leveraged from anywhere without rewriting the logic.

Functions improve code in several ways:

  • Readability: Descriptive function names indicate the task being done. The user knows what sum() performs without looking at its code.
  • De-cluttering: The main code looks clean and concise after moving logic blocks into functions.
  • Troubleshooting: Fixing issues is easier by testing functions independently.
  • Sharing: Functions can be shared across files and projects very easily.
  • Memory: The program's memory footprint is reduced due to a single copy of the function code in memory.
  • Scaling: Adding more features becomes faster by reusing functions.
  • Libraries: Function sets can be created in files like a toolkit.

Instead of forcing repetition of computation steps everywhere, developers must always identify reusable logic and split it into functions. It drives modular programming by breaking complex tasks into bite-sized building blocks. C was designed for functions to optimize code structuring using this template.

Example:

Let us take a C program to calculate the sum of the series using a predefined function.

Output:

Enter the value of n: 5
Sum of the series = 15

Explanation:

The program declares a function sum() that will determine the total of the initial n natural numbers.

In main():

  1. It prompts the user to enter a value for n.
  2. The entered value is stored in the variable n.
  3. The sum() Function is called by passing n as the argument.
  4. The result returned by sum() is stored in the variable result.
  5. This result is printed out.

For example, if the user entered n as 5, the sum() function will calculate the sum of the first 5 natural numbers, i.e. 1 + 2 + 3 + 4 + 5 = 15

Conclusion:

Defining reusable functions like sum() is considered best practice in C programming. It makes code:

  • Modular: Separate blocks for logic.
  • Readable: Understand the purpose of the name.
  • Extensible: Add more functions as needed

It demonstrates how using custom functions makes programs structured, maintainable, and concise by avoiding code repetition. Functions divide tasks logically for easy testing, updating and reuse across projects.