Unit-6: Functions

Fundamental Function Types

In C programming, functions are categorized into two main types:

 

1. Library functions: These are pre-built functions included in the C library, such as printf, scanf, and strcpy. Library functions serve specific purposes, like reading from standard input or writing to standard output, and they return values of specific data types.

 

2. User-defined functions: These functions are created by the programmer to perform specific tasks within the program. User-defined functions may return a value or nothing (void). To use a user-defined function, you must provide its definition and call it in the main program or other functions.

 

In both cases, functions can take zero or more arguments as input and can return a value of any data type, such as int, float, char, etc. Functions help break down complex problems into manageable tasks, promoting code modularity and reusability.

 

Declaration and Definition

 

In C programming, declaration and definition serve distinct purposes:

 

1. Declaration: This statement informs the compiler about the function's name, return type, and number of arguments. It does not provide the function's implementation but signals to the compiler that the function exists and can be called. Declarations can be placed in header files or the main program.

 

   Syntax:

     ```c

     return_type function_name(argument_type argument_name, argument_type argument_name, ...);

     ```

   - Example:

     ```c

     int max(int a, int b);

     ```

 

2.  Definition:  This statement contains the code specifying what the function does. Definitions can be placed in source files or the main program.

 

   -  Syntax:

     ```c

     return_type function_name(argument_type argument_name, argument_type argument_name, ...) {

       // Function body

     }

     ```

   -  Example:

     ```c

     int max(int a, int b) {

       if (a > b) {

         return a;

       } else {

         return b;

       }

     }

     ```

 

Functions must be declared multiple times if necessary but defined only once in the program. If declared in a header file, the function should be defined in a source file, ensuring consistency in return type, function name, and arguments.

 

Function Call

 

A function call is a statement that invokes a function and executes the code within its definition. During a function call, values are passed as arguments, and the function may return a value or perform actions.

 

-  Syntax:

  ```c

  function_name(argument_value, argument_value, ...);

  ```

 

-  Example:

  ```c

  #include <stdio.h>

 

  int max(int a, int b) {

    if (a > b) {

      return a;

    } else {

      return b;

    }

  }

 

  int main() {

    int x = 10, y = 20;

    int result = max(x, y);

    printf("The maximum of %d and %d is %d\n", x, y, result);

    return 0;

  }

  ```

 

In this example, the `max` function is called with `x` and `y` as arguments, and the result is printed in the main function.

 

Types of Functions

 

In C programming, functions can be categorized based on return type and the number and type of arguments. Common types include:

 

1.  Functions with no return type and no arguments:

   ```c

   void print_message() {

     printf("Hello, World!\n");

   }

   ```

 

2.  Functions with no return type and some arguments:

   ```c

   void print_sum(int a, int b) {

     int sum = a + b;

     printf("The sum of %d and %d is %d\n", a, b, sum);

   }

   ```

 

3.  Functions with a return type and no arguments:

   ```c

   int square(int x) {

     return x * x;

   }

   ```

 

4.  Functions with a return type and some arguments:

   ```c

   int max(int a, int b) {

     if (a > b) {

       return a;

     } else {

       return b;

     }

   }

   ```

 

These functions demonstrate the variety of tasks functions can perform in C programming. They enhance code modularity, readability, and reusability.

 

Parameter Passing

 

In C, when a function is called, there are three ways to pass parameters:

 

1.  Call by Value:  A copy of the argument value is passed to the function, and changes made to the parameter within the function do not affect the original argument.

 

   -  Example:

     ```c

     void swap(int a, int b) {

       int temp = a;

       a = b;

       b = temp;

     }

     ```

 

2.  Call by Reference:  A reference to the argument value is passed, allowing changes to the parameter within the function to be reflected in the original argument.

 

   -  Example:

     ```c

     void swap(int *a, int *b) {

       int temp = *a;

       *a = *b;

       *b = temp;

     }

     ```

 

3.  Call by Address:  Similar to call by reference, but using the address directly.

 

   -  Example:

     ```c

     void swap(int &a, int &b) {

       int temp = a;

       a = b;

       b = temp;

     }

     ```

 

It's crucial to choose the appropriate method based on the requirements of the program.

 

Scope of Variable

 

Variable scope defines where a variable can be accessed and its lifetime. Two types of variable scope exist:

 

1.  Local Scope:  Variables with local scope are defined within a function or block of code and can only be accessed within that specific block. They are destroyed when control exits the block.

 

2.  Global Scope:  Variables with global scope are defined outside any function or block and can be accessed throughout the program. They persist from program start to end.

 

Variables with local scope take precedence over global variables with the same name within their respective scopes.

 

Storage Classes

 

Storage classes specify the lifetime and visibility of a variable. Common storage classes include:

 

1.  Automatic Storage Class:  Variables declared inside a function with no specified storage class are automatically assigned the "automatic" storage class. They are stored on the stack and created and destroyed with each function call.

 

   -  Example:

     ```c

     void func() {

       int x = 10; // automatic variable

     }

     ```

 

2.  Register Storage Class:  Variables declared with the "register" storage class are stored in CPU registers, potentially improving performance. Access to registers is faster than memory access.

 

   -  Example:                            

     ```c

     void func() {

       register int x = 10; // register variable

     }

     ```

 

3.  Static Storage Class:  Variables declared with the "static" storage class have limited scope to the declaring function but retain their value between function calls.

 

   -  Example:

     ```c

     void func() {

       static int x = 10; // static variable

     }

     ```

 

4.  External Storage Class:  Variables declared outside any function without a specified storage class are automatically assigned the "external" storage class. These global variables are accessible by