Functions in C

📘 C 👁 29 views 📅 Dec 22, 2025
⏱ Estimated reading time: 2 min

A function is a block of code that performs a specific task.
Functions help in modular programming, code reuse, and readability.


Types of Functions

  1. Library Functions

  2. User-Defined Functions


1. Library Functions

Predefined functions provided by C libraries.

Examples:

  • printf()

  • scanf()

  • strlen()

  • sqrt()


2. User-Defined Functions

Functions written by the programmer.


Structure of a Function

Function Declaration (Prototype)

return_type function_name(parameter_list);

Function Definition

return_type function_name(parameter_list) { statements; }

Function Call

function_name(arguments);

Example Program

#include int add(int a, int b) { return a + b; } int main() { int sum; sum = add(5, 3); printf("Sum = %d", sum); return 0; }

Components of a Function

  • Return type – Type of value returned

  • Function name – Identifier

  • Parameters – Values passed to function

  • Function body – Statements to execute


Types of User-Defined Functions

Based on arguments and return value:

  1. No arguments, no return value

  2. Arguments, no return value

  3. No arguments, return value

  4. Arguments and return value


Call by Value

  • Actual value is passed to function

  • Changes do not affect original variable


Recursion

A function calling itself.

int fact(int n) { if (n == 0) return 1; else return n * fact(n - 1); }

Advantages of Functions

  • Code reusability

  • Easy debugging

  • Better readability

  • Reduced program size


Summary

  • Functions divide a program into modules

  • main() is the starting function

  • Functions can return values

  • Recursion is supported


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes