Chapter 12: Functions in C Language

Functions are one of the fundamental constructs in the C language, allowing code to be broken down into smaller, more manageable parts. Functions come in different types, depending on whether they accept parameters and return values. Here are four common types of functions:

  1. 1. No-parameter, no-return-value function
  2. 2. Parameter, no-return-value function
  3. 3. No-parameter, return-value function
  4. 4. Parameter, return-value function

1. No-parameter, no-return-value function

This type of function does not accept any parameters and does not return any values. It is typically used to perform an operation or output information.

Example Code:

#include <stdio.h>

// No-parameter, no-return-value function
void greet() {
    printf("Hello, welcome to the C language!\n");
}

int main() {
    greet();  // Call the no-parameter, no-return-value function
    return 0;
}

Explanation

  • <span>void greet()</span> is a no-parameter, no-return-value function, where <span>void</span> indicates no parameters and no return value.
  • <span>greet()</span> function is called in <span>main()</span>, outputting a welcome message.

2. Parameter, no-return-value function

This type of function accepts some parameters but does not return any values. It is typically used to modify the passed parameters or perform certain operations.

Example Code:

#include <stdio.h>

// Parameter, no-return-value function
void printSum(int a, int b) {
    printf("The sum is: %d\n", a + b);
}

int main() {
    int x = 5, y = 7;
    printSum(x, y);  // Call the parameter, no-return-value function
    return 0;
}

Explanation

  • <span>void printSum(int a, int b)</span> is a function with two integer parameters, accepting two parameters and printing their sum.
  • <span>main()</span> calls <span>printSum(x, y)</span>, passing <span>x</span> and <span>y</span> as parameters.

3. No-parameter, return-value function

This type of function does not accept parameters but returns a value. It is typically used to calculate certain values and return them.

Example Code:

#include <stdio.h>

// No-parameter, return-value function
int getRandomNumber() {
    return 42;  // Return a fixed value
}

int main() {
    int number = getRandomNumber();  // Call the no-parameter, return-value function
    printf("The random number is: %d\n", number);
    return 0;
}

Explanation

  • <span>int getRandomNumber()</span> is a no-parameter, return-value function that returns an integer value <span>42</span>.
  • <span>main()</span> calls <span>getRandomNumber()</span> and stores its return value in the <span>number</span> variable.

4. Parameter, return-value function

This type of function accepts parameters and returns a value. It is typically used to perform calculations and return results.

Example Code:

#include <stdio.h>

// Parameter, return-value function
int multiply(int a, int b) {
    return a * b;  // Return the product of two numbers
}

int main() {
    int x = 5, y = 7;
    int result = multiply(x, y);  // Call the parameter, return-value function
    printf("The result of multiplication is: %d\n", result);
    return 0;
}

Explanation

  • <span>int multiply(int a, int b)</span> is a function with two parameters that returns the product of the two parameters.
  • <span>main()</span> calls <span>multiply(x, y)</span> and stores the return value in the <span>result</span> variable.

Summary

  1. 1. No-parameter, no-return-value function: Does not accept any parameters and does not return any values, commonly used for printing output or performing certain operations.
  2. 2. Parameter, no-return-value function: Accepts some parameters, performs certain operations, but does not return any values.
  3. 3. No-parameter, return-value function: Does not accept any parameters but returns a value.
  4. 4. Parameter, return-value function: Accepts parameters and returns a computed result.

By using functions, code can be made more structured, modular, and easier to maintain and debug.

Leave a Comment