C Programs Tutorials | IT Developer
IT Developer

C Programming - C User Defined Functions



Share with a Friend

C Programming - C User Defined Functions

C User-Defined Functions

A user-defined function in C is a function that is defined by the user, as opposed to built-in functions that are part of the C standard library. User-defined functions allow programmers to break down a program into smaller, manageable, and reusable blocks of code.

Functions in C can be broadly categorized into two types:

  1. Functions without return values (void functions)
  2. Functions with return values

Syntax of a User-Defined Function

A function definition in C generally consists of the following components:

  1. Return Type: The type of value the function will return (e.g., int, float, char, void).
  2. Function Name: The identifier that will be used to call the function.
  3. Parameters (Optional): The input data for the function, defined inside parentheses.
  4. Function Body: The block of code that contains the statements that are executed when the function is called.

General Syntax:

C

return_type function_name(parameter_list) {

    // Function body

}

  • return_type: The data type of the value that will be returned by the function. Use void if the function does not return any value.
  • function_name: The name of the function (e.g., add, multiply).
  • parameter_list: A comma-separated list of parameters that are passed to the function.

Types of User-Defined Functions in C

  1. Functions with Return Values
    • These functions return a value after performing some operations.
    • The return type is something other than void.
  2. Functions without Return Values (void functions)
    • These functions do not return any value.
    • The return type is void.

Example 1: Function with Return Value

This is an example of a simple function that adds two integers and returns the result.

C

#include <stdio.h>

 

// Function declaration

int add(int, int);

 

int main() {

    int num1 = 10, num2 = 20;

    int result = add(num1, num2); // Calling the user-defined function

    printf("Sum: %d\n", result);   // Printing the result

    return 0;

}

 

// Function definition

int add(int a, int b) {

    return a + b;  // Returns the sum of a and b

}

Explanation:

  • Function Declaration: int add(int, int); tells the compiler that there will be a function add which takes two integers as arguments and returns an integer.
  • Function Definition: int add(int a, int b) { return a + b; } is where the actual logic of adding two numbers is implemented.
  • Function Call: Inside main(), the add() function is called with two arguments, and the result is stored in the variable result.

Example 2: Function without Return Value (Void Function)

This example shows a function that prints the sum of two numbers but does not return any value.

C

#include <stdio.h>

 

// Function declaration

void printSum(int, int);

 

int main() {

    int num1 = 10, num2 = 20;

    printSum(num1, num2);  // Calling the function that does not return anything

    return 0;

}

 

// Function definition

void printSum(int a, int b) {

    printf("Sum: %d\n", a + b);  // Printing the sum

}

Explanation:

  • Function Declaration: void printSum(int, int); specifies that the function printSum will take two integers and return no value.
  • Function Definition: void printSum(int a, int b) contains the logic to print the sum of a and b.
  • Function Call: Inside main(), the printSum() function is called with two arguments to print the sum.

Function Call Mechanism

When a function is called in C, the following steps occur:

  1. Function Call: The function is invoked with the arguments passed (if any).
  2. Function Execution: The function executes its code.
  3. Return Value: If the function has a return type, it returns a value to the caller. If it does not return a value, control is returned to the calling function once the function body completes.

Passing Arguments to Functions

In C, functions can be passed arguments in two ways:

  1. Call by Value: The function gets a copy of the actual argument value. Any changes made to the parameter inside the function do not affect the original value.
  2. Call by Reference: The function gets the address of the actual argument. Any changes made to the parameter will affect the original value.

Example 3: Call by Value

C

#include <stdio.h>

 

void square(int num) {

    num = num * num;  // Squaring the number inside the function

    printf("Squared: %d\n", num);

}

 

int main() {

    int value = 5;

    square(value);  // Passing the value by copy (call by value)

    printf("Original value: %d\n", value);  // The original value remains unchanged

    return 0;

}

Explanation:

  • The square function receives a copy of the value variable. Modifying num inside the function does not change the original value.

Example 4: Call by Reference

C

#include <stdio.h>

 

void square(int *num) {

    *num = *num * *num;  // Modifying the original value by dereferencing the pointer

}

 

int main() {

    int value = 5;

    square(&value);  // Passing the address of the value (call by reference)

    printf("Squared value: %d\n", value);  // The original value is changed

    return 0;

}

Explanation:

  • In this case, we pass the address of value to the square function. The function modifies the original value by dereferencing the pointer.

Recursive Functions

A recursive function is a function that calls itself in its own definition. Recursive functions are useful for solving problems that can be broken down into smaller sub-problems.

Example of a Recursive Function (Factorial)

C

#include <stdio.h>

 

// Function declaration

int factorial(int n);

 

int main() {

    int num = 5;

    int result = factorial(num);  // Calling the recursive function

    printf("Factorial of %d is: %d\n", num, result);

    return 0;

}

 

// Function definition (recursive)

int factorial(int n) {

    if (n == 0) {  // Base case

        return 1;

    } else {

        return n * factorial(n - 1);  // Recursive call

    }

}

Explanation:

  • The factorial function calls itself to calculate the factorial of n recursively.
  • Base Case: The recursion stops when n equals 0, returning 1.

Advantages of User-Defined Functions

  1. Code Reusability: Functions allow code to be written once and used multiple times.
  2. Modularity: Functions break down a program into smaller, manageable parts.
  3. Improved Readability: Functions help make code more readable and maintainable.
  4. Abstraction: Functions allow you to hide complex logic behind simple function calls.

Summary

  • User-Defined Functions in C are functions that allow you to perform specific tasks and operations.
  • They can return values or perform actions without returning anything.
  • Call by Value and Call by Reference are the two ways to pass arguments to a function.
  • Recursive Functions are useful for problems that require repetitive actions and self-referential logic.