C Programming • Arrays & Functions
C Programming / Functions: Types of functions

Functions: Types of functions

Notes 3 Arrays & Functions

Definition: A function is a named block of statements designed to perform a specific task. A program can call the function whenever that task is required.

Notes

Functions in C

Definition: A function is a named block of statements designed to perform a specific task. A program can call the function whenever that task is required.

💡 Example: A function named add() can perform the task of adding two numbers.

Why Do We Use Functions?

Functions divide a large program into smaller, manageable parts. This makes programs easier to understand, test, reuse and maintain.

💡 Example: A student result program can have separate functions such as inputMarks(), calculateTotal() and displayResult().

Basic Parts of a Function

A function generally involves a declaration/prototype, a definition, and a function call.

Part Purpose Example
Function Declaration Tells the compiler about the function. int add(int, int);
Function Definition Contains the actual statements of the function. int add(int a, int b) { ... }
Function Call Requests execution of the function. add(10, 20);

Function Declaration

Definition: A function declaration, also called a function prototype, tells the compiler the function's name, return type and parameters before the function is used.

💡 Example: int add(int, int); declares a function named add that returns an integer and accepts two integer parameters.

Function Definition

Definition: A function definition contains the statements that specify what the function actually does.

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

Function Call

Definition: A function call is an expression used to execute the function and perform its task.

💡 Example: result = add(10, 20);

General Function Syntax

return_type function_name(parameter_list)
{
    // statements
    return value;
}
💡 Example: int square(int n) means the function square accepts an integer and returns an integer.

Types of Functions in C

Functions can be classified based on whether they take arguments and whether they return a value.

Type Arguments Return Value
No arguments, no return value No No
Arguments, no return value Yes No
No arguments, returns a value No Yes
Arguments, returns a value Yes Yes

1. No Arguments and No Return Value

Definition: This type of function does not receive any values from the calling function and does not return a value. It simply performs a specific task.

💡 Example: A function can simply display a welcome message.
void welcome()
{
    printf("Welcome to C Programming!");
}

2. Arguments and No Return Value

Definition: This type of function receives values as arguments but does not return a value to the calling function.

💡 Example: A function can receive two numbers and directly display their sum.
void add(int a, int b)
{
    printf("Sum = %d", a + b);
}

3. No Arguments but Returns a Value

Definition: This type of function does not receive arguments but performs a task and returns a value to the caller.

💡 Example: A function can return a fixed integer value.
int getNumber()
{
    return 10;
}

4. Arguments and Returns a Value

Definition: This type of function receives one or more arguments, performs a task and returns the result to the caller.

💡 Example: A function can receive two numbers and return their sum.
int add(int a, int b)
{
    return a + b;
}

Most Common Type

The function type arguments + return value is very commonly used because data can be supplied to the function and the calculated result can be returned to the calling code.

💡 Example: int sum = add(10, 20); passes two values and receives the calculated result.

Library Functions

Definition: Library functions are predefined functions provided by C libraries. A programmer can use them without writing their complete implementation.

💡 Example: printf() and scanf() are commonly used library functions provided through standard headers.

User-Defined Functions

Definition: User-defined functions are functions created by the programmer to perform a specific task required by the program.

💡 Example: A programmer can create calculateAverage() to calculate the average marks of a student.

Library Functions vs User-Defined Functions

Library Functions User-Defined Functions
Provided by C libraries. Created by the programmer.
Already implemented. Implementation is written by the programmer.
Example: printf() Example: add()

Practical Example — Function to Add Two Numbers

Problem Statement

Write a C program that uses a user-defined function to add two integers and return the result.

Learning Outcomes

  • Understand function declaration, definition and function call.
  • Pass arguments to a function.
  • Return a calculated value from a function.

Hint

Create an add() function that accepts two integers and returns their sum.

Theory

A function can receive data through parameters and return a result to the calling function. This helps divide a program into smaller reusable tasks.

Program

#include <stdio.h>

int add(int a, int b);             // function declaration

int main()
{
    int x, y, result;

    printf("Enter two numbers: ");
    scanf("%d %d", &x, &y);

    result = add(x, y);            // function call

    printf("Sum = %d
", result);

    return 0;
}

int add(int a, int b)
{
    return a + b;                  // return the calculated sum
}

Expected Output

Enter two numbers: 10 15
Sum = 25

Note

The function add() receives two arguments and returns the result. The returned value is stored in result inside main().

Important Points for Exam

  • A function performs a specific task.
  • Functions help divide a large program into smaller parts.
  • A function may accept arguments and may return a value.
  • Function declaration tells the compiler about the function.
  • Function definition contains the actual implementation.
  • Function call executes the function.
  • Library functions are predefined; user-defined functions are created by the programmer.
🎯 Easy Rule:

Declaration → Definition → Call

Quick Revision

Function Type Arguments Return
No arguments, no return No No
Arguments, no return Yes No
No arguments, return No Yes
Arguments, return Yes Yes

Important Exam Questions

Short Answer Questions

  1. What is a function in C?
  2. Why are functions used in C?
  3. What is a function declaration?
  4. What is a function definition?
  5. What is a function call?
  6. What are library functions?
  7. What are user-defined functions?
  8. Write the four types of functions based on arguments and return value.

Long Answer Questions

  1. Define a function and explain its declaration, definition and call.
  2. Explain the different types of functions in C with suitable examples.
  3. Differentiate between library functions and user-defined functions.
  4. Write a C program using a user-defined function to add two numbers.
🎥 Recommended Learning

Watch a beginner-friendly explanation of functions and their types in C programming.

▶ Watch: Functions in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for function types will be provided here.

🧠 Mind Map

Use the mind map for quick revision of declaration, definition, call and the four function types.