Understanding Functions in C: User-Defined Functions, Library Functions, and Parameter Passing

Markdown

View as Markdown

Functions are one of the most important features of the C programming language. They help organize programs into smaller, reusable blocks, making code easier to read, test, maintain, and debug. Instead of writing the same code multiple times, you can create a function once and call it whenever needed.

Learning Objectives

  • Understand the purpose of functions

  • Create and call user-defined functions

  • Use built-in library functions

  • Pass arguments to functions

  • Differentiate between call by value and call by reference

  • Improve code reusability and readability

What is a Function?

A function is a self-contained block of code designed to perform a specific task. Functions help divide a large program into manageable sections and reduce code duplication.

General syntax:

return_type function_name(parameters)

{

    // statements

}

User-Defined Functions

A user-defined function is created by the programmer to perform a specific task.

Example:

#include <stdio.h>

void greet()

{

    printf("Welcome to Team Academy!\n");

}

int main()

{

    greet();

    return 0;

}

Output

Welcome to Team Academy!

Library Functions

C provides many built-in functions through standard libraries.

Examples include:

Function

Header File

Purpose

printf()

stdio.h

Display output

scanf()

stdio.h

Read input

strlen()

string.h

Find string length

sqrt()

math.h

Calculate square root

Example:

#include <stdio.h>

#include <math.h>

int main()

{

    printf("%.2f", sqrt(25));

    return 0;

}

Output

5.00

Function Declaration

A function declaration tells the compiler about a function before it is used.

Example:

void greet();

This is also known as a function prototype.

Function Definition

The function definition contains the actual implementation.

Example:

void greet()

{

    printf("Welcome!");

}

Function Call

A function is executed by calling its name.

Example:

greet();

Passing Parameters

Functions can receive information through parameters.

Example:

#include <stdio.h>

void display(int number)

{

    printf("Number = %d", number);

}

int main()

{

    display(100);

    return 0;

}

Output

Number = 100

Call by Value

In call by value, a copy of the variable is passed to the function. Changes made inside the function do not affect the original variable.

Example:

#include <stdio.h>

void changeValue(int x)

{

    x = 50;

}

int main()

{

    int number = 10;

 

    changeValue(number);

    printf("%d", number);

    return 0;

}

Output

10

Call by Reference

Call by reference uses pointers to modify the original variable.

Example:

#include <stdio.h>

void changeValue(int *x)

{

    *x = 50;

}

int main()

{

    int number = 10;

    changeValue(&number);

    printf("%d", number);

    return 0;

}

Output

50

Advantages of Functions

  • Reduce code duplication

  • Improve readability

  • Simplify debugging

  • Increase reusability

  • Make programs easier to maintain

  • Support modular programming

Common Mistakes

  • Forgetting to declare a function before calling it

  • Passing incorrect data types

  • Missing return statements

  • Confusing call by value with call by reference

Best Practices

  • Use meaningful function names.

  • Keep functions focused on a single task.

  • Avoid writing excessively long functions.

  • Use function prototypes for better organization.

  • Document functions with comments where appropriate.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Was this article helpful?

Still need help?

Contact us