C User-Defined Functions

C provides many built-in functions like printf(), scanf(), and strlen(). But in real programs, you need to create your own functions to solve your specific problems. These are called user-defined functions.

A user-defined function is a named block of code that you write to perform a specific task. You can call it as many times as needed without rewriting the same code.

Why Write Your Own Functions?

  • Avoid repetition: Write the code once, use it anywhere
  • Organize code: Break a large program into smaller, manageable pieces
  • Easier to debug: Fix one function instead of hunting through thousands of lines
  • Reusability: A function written today can be used in future programs

Three Parts of a User-Defined Function

  1. Function Declaration (Prototype): Tells the compiler the function exists before it is used
  2. Function Definition: The actual code that runs when the function is called
  3. Function Call: The line that executes the function

Basic Function Structure

return_type function_name(parameter_list)
{
    // body of the function
    // code to execute
    return value;   // optional if return_type is void
}

Function Parts Explained

PartMeaningExample
return_typeThe type of value the function sends backint, float, void
function_nameName you give to the functioncalculateArea
parameter_listInput values the function acceptsint length, int width
return statementSends a result back to the callerreturn area;

Type 1: Function with No Parameters and No Return Value

This type of function performs a task but neither receives input nor sends back any result.

#include <stdio.h>

// Function definition
void greet()
{
    printf("Welcome to C Programming!\n");
    printf("Enjoy learning.\n");
}

int main()
{
    greet();   // Function call
    greet();   // Called again — code runs twice
    return 0;
}

Output:

Welcome to C Programming!
Enjoy learning.
Welcome to C Programming!
Enjoy learning.

Type 2: Function with Parameters, No Return Value

This function accepts input values (parameters) but does not return a result.

#include <stdio.h>

void printSum(int a, int b)   // a and b are parameters
{
    int sum = a + b;
    printf("Sum of %d and %d = %d\n", a, b, sum);
}

int main()
{
    printSum(5, 3);    // Output: Sum of 5 and 3 = 8
    printSum(10, 20);  // Output: Sum of 10 and 20 = 30
    return 0;
}

Type 3: Function with No Parameters, With Return Value

This function gathers data or computes a value internally and sends it back.

#include <stdio.h>

int getYear()
{
    return 2025;   // returns a fixed value
}

int main()
{
    int year = getYear();   // receives the return value
    printf("Current year: %d\n", year);   // Output: Current year: 2025
    return 0;
}

Type 4: Function with Parameters and Return Value

This is the most common and useful type. It takes input, processes it, and returns a result.

#include <stdio.h>

// Function to calculate area of a rectangle
int calculateArea(int length, int width)
{
    int area = length * width;
    return area;
}

int main()
{
    int result = calculateArea(10, 5);   // call with arguments 10 and 5
    printf("Area = %d\n", result);       // Output: Area = 50

    printf("Area = %d\n", calculateArea(7, 3));   // Output: Area = 21

    return 0;
}

Function Declaration (Prototype)

When a function is defined after main(), the compiler does not know it exists yet when it encounters the call inside main(). A prototype informs the compiler about the function's name, return type, and parameters before it is defined.

#include <stdio.h>

// Prototype — tells compiler: this function exists
int square(int n);

int main()
{
    printf("Square of 4 = %d\n", square(4));   // Output: 16
    return 0;
}

// Definition — placed after main()
int square(int n)
{
    return n * n;
}

Without the prototype, the compiler would report an implicit declaration warning or error when it sees square(4) before the definition.

Parameters vs Arguments

These two terms describe the same values at different moments:

TermWhere It AppearsExample
ParameterIn the function definitionint add(int a, int b) — a and b are parameters
ArgumentIn the function calladd(5, 3) — 5 and 3 are arguments

Passing by Value

By default, C passes arguments by value — the function receives a copy of the argument. Changes made inside the function do not affect the original variable.

#include <stdio.h>

void doubleIt(int num)
{
    num = num * 2;   // changes only the local copy
    printf("Inside function: %d\n", num);
}

int main()
{
    int x = 10;
    doubleIt(x);
    printf("Outside function: %d\n", x);   // x is still 10
    return 0;
}

Output:

Inside function: 20
Outside function: 10

Visual Diagram: Function Call Flow


main():                          calculateArea(10, 5):
--------                         -------------------------
int result;                      int length = 10  (copy)
                                 int width  = 5   (copy)
result = calculateArea(10, 5)    int area = 10 * 5 = 50
         ^                       return 50;
         |                             |
         +-------- 50 ----------------+
         result = 50

main() receives 50 and stores it in 'result'

Multiple Return Points

A function can have more than one return statement. The function stops and returns as soon as it hits a return.

#include <stdio.h>

char grade(int marks)
{
    if (marks >= 90) return 'A';
    if (marks >= 75) return 'B';
    if (marks >= 60) return 'C';
    return 'F';
}

int main()
{
    printf("Grade: %c\n", grade(85));   // Output: Grade: B
    printf("Grade: %c\n", grade(55));   // Output: Grade: F
    return 0;
}

Summary

User-defined functions let you create reusable, organized blocks of code for specific tasks. A function has a return type, a name, an optional parameter list, and a body. Functions are classified by whether they accept parameters and whether they return a value — four combinations are possible. A function prototype declares the function before its definition so the compiler knows it exists. Arguments are the actual values passed during a function call, while parameters are the variables that receive them. By default, C passes arguments by value — the original variable is never changed inside the function unless a pointer is used.

Leave a Comment

Your email address will not be published. Required fields are marked *