Chapter 2: C-Programming

Introduction

1. Working with a Function

a. Define Function

A function is a block of code that performs a specific task. In C, functions help modularize programs and make code reusable and easier to maintain.

b. Syntax of Functions


return_type function_name(parameters)
{
    // body of the function
}
      

c. Types of Functions

  • Library Functions: Predefined functions provided by C libraries (e.g., printf(), scanf()).
  • User Defined Functions: Functions created by the programmer to perform custom tasks.

d. Components of Function

  • Function Prototype: Declaration of a function before its use.
  • Function Call: Statement that invokes the function.
  • Function Definition: Body of the function where actual statements are written.
  • Return Type: Data type of value returned by the function.

2. Categories of Function with Examples

i. Function with Return Type but No Arguments


int getNumber()
{
    return 10;
}
      

ii. Function with Return Type with Arguments


int add(int a, int b)
{
    return a + b;
}
      

iii. Function with No Return Type and No Arguments


void display()
{
    printf("Hello World");
}
      

iv. Function with No Return Type but with Arguments


void printNumber(int n)
{
    printf("Number is %d", n);
}
      

3. Storage Classes

  • Automatic: Default storage class for local variables.
    
        void demo()
        {
            int x = 10; // automatic by default
            printf("%d", x);
        }
            
  • External: Variables declared outside all functions and accessible globally.
    
        #include <stdio.h>
    
        int globalVar = 5; // external
    
        void show()
        {
            printf("%d", globalVar);
        }
            
  • Register: Variables stored in CPU registers for faster access.
    
        void fastAccess()
        {
            register int i;
            for(i = 0; i < 10; i++)
                printf("%d ", i);
        }
            
  • Static: Variables that retain their value between function calls.
    
        void counter()
        {
            static int count = 0;
            count++;
            printf("Count = %d\n", count);
        }
            

4. Recursive Function with Syntax and Example

A recursive function calls itself to solve smaller subproblems.

Syntax:


return_type function_name(parameters)
{
    if(condition)
        return result;
    else
        return function_name(updated_parameters);
}
      

Example 1: Factorial


#include <stdio.h>
int fact(int);
int main()
{
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    printf("Factorial of %d is %d\n", n, fact(n));
    return 0;
}

int fact(int n)
{
    if(n == 0 || n == 1)
        return 1;
    else
        return n * fact(n - 1);
}
      

Output:
unavailable

Example 2: Sum of natural numbers


#include <stdio.h>
int sum(int);
int main()
{
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    printf("Sum of first %d natural numbers is %d\n", n, sum(n));
    return 0;
}

int sum(int n)
{
    if(n == 0)
        return 0;
    else
        return n + sum(n - 1);
}
      

Output:
unavailable

5. Passing Array to a Function

Example: Sum of Array Elements


#include <stdio.h>
void addarray(int[], int);
int main() 
{
    int n;
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);
    
    int arr[n];
    printf("Enter %d elements:\n", n);
    for(int i = 0; i < n; i++) 
    {
        scanf("%d", &arr[i]);
    }
    addarray(arr, n); 
    return 0;
}

void addarray(int arr[], int size) 
{
    int sum = 0;
    for(int i = 0; i < size; i++) 
    {
        sum += arr[i];
    }
    printf("Sum of the array elements is: %d\n", sum);
}
      

Output:
unavailable

Questions and Answers


COMING SOON

Past NEB Questions


COMING SOON