C Program To Calculate The Sum Of Series Using A Predefined Function

Functions in C are declared once and can be invoked from various parts in a program. They receive inputs via parameters and provide outputs through 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:

  • Declares necessary variables
  • Prints output messages
  • Calls the sum function
  • Stores and prints the result

Here, the main function is designed to receive user input, call the sum function to perform calculations, and then present the result.

Declaring the sum Function

The primary function sum is defined in the following section. Declaring functions provides the compiler with prior knowledge of a function's existence before it is actually implemented.

The declaration specifies:

  • Return type (int)
  • Name of the Function (sum)
  • Parameters (int n)

Keep in mind that there is no function body specified at this point. The function's definition will be provided further down, following the main function.

Calling the Function from main

Within the main function, the software requests input from the user for a value, which is then stored in a variable called n. Subsequently, this value is transmitted to the sum function via a function invocation.

The function call shifts program flow from the main function to the sum function. The value returned by the function is stored in the result variable and then displayed as the ultimate sum.

Defining the Function

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

  • Initialize accumulator variable s to 0
  • Run a loop from 1 to n
  • Add each number to s
  • Return the sum

Upon completion of the sum function execution, the flow of control reverts back to the main function from where it was called.

Using Functions for Code Reuse

Creating personalized functions is crucial for organized coding in C programming. It allows for the development of reusable and segmented code. After defining functions such as sum, they can be utilized across the codebase without the need to duplicate the algorithm.

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 duplicating computational steps across the codebase, programmers should consistently recognize common logic and encapsulate it within functions. This approach promotes modular programming, where intricate processes are divided into smaller, manageable components. The C language was specifically crafted to leverage functions for enhancing code organization based on this principle.

Example:

Let's consider a C program that computes the total of a series utilizing a built-in function.

Example

#include <stdio.h>



// Function declaration

int sum(int n);



int main() {

    int n, result;

    

    printf("Enter the value of n: ");

    scanf("%d", &n);

    

    // Calling the sum function

    result = sum(n);

    

    printf("Sum of the series = %d", result);



    return 0;

}



//Function to calculate the sum of series

int sum(int n) {

    int i, s = 0;

    for(i=1; i<=n; i++) {

        s += i; 

    }

    return s; 

}

Output:

Output

Enter the value of n: 5

Sum of the series = 15

Explanation:

The software defines a function named sum that calculates the sum of the first n natural numbers.

In main:

  • It prompts the user to enter a value for n.
  • The entered value is stored in the variable n.
  • The sum Function is called by passing n as the argument.
  • The result returned by sum is stored in the variable result.
  • This result is printed out.

For instance, if the user inputted n as 5, the sum method will compute the total of the initial 5 natural numbers, which is 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 illustrates the benefits of implementing custom functions in programming, enhancing program organization, manageability, and brevity by eliminating redundant code. Functions help in breaking down tasks logically, facilitating testing, modifications, and sharing functionality among different projects.

Input Required

This code uses input(). Please provide values below: