Skip to content
Functions in C

🔹 Functions in C

A function is a code segment or sub-program defined separately with an identifier, so it can be reused whenever needed.

✨ Advantages of Functions

  • Reusability – Write once, use multiple times.
  • Modularity – Break complex problems into smaller parts.
  • Readability – Code becomes easier to understand and maintain.

📌 Two Forms of Functions

  • Definition – Specifies the code to be executed when invoked.
  • Invocation (Calling) – Requests the function to run its definition.

🔗 Relationship Between Definition & Invocation

  • No function can be invoked unless it is defined.
  • A defined function cannot run unless it is invoked.
  • Functions can be invoked inside other functions.
  • Invocation completely depends on definition.

💻 Example 1: Simple Function

void test() {
    printf("\nHi from Test");
}

void main() {
    printf("Start of Main");
    test(); // invocation
    printf("\nEnd of main");
}
    

Illustration: Function Definition & Invocation

Function Definition void test() { printf("Hi from Test"); } Function Invocation test(); Call

👥 Caller vs Called Function

  • Caller – The function that calls another function.
  • Called – The function being invoked.

💻 Example 2: Nested Function Calls

#include

void test2() {
    printf("\nHi of test2");
}

void test1() {
    printf("\nStart of test1");
    test2(); // test1 calls test2
    printf("\nEnd of test1");
}

void main() {
    printf("Start of main");
    test1(); // main calls test1
    printf("\nEnd of main");
}
    

Illustration: Caller and Called Functions

main() test1() test2() Caller Called
Functions and Return Values in C

🔹 Types of Functions in C

  • Pre-defined / Built-in / Library Functions – Already available in C libraries.
    Examples: printf(), scanf(), gets(), strcmp()
  • User-defined Functions – Created by the programmer to perform specific tasks.

📘 Structure of a Function Definition

A function definition contains two parts:

  • Header – Identifies the function.
  • Body – Contains executable statements.

🧩 Header Components

  • Datatype of return value
  • Name of the function
  • Parentheses with or without arguments

Function Header Structure

datatype funcName() { /* body of the function */ } Header → identifies function Body → executable part

🔁 Return Value

The return value is sent back to the calling function after execution. It can belong to any basic or user-defined datatype.

Syntax of Return Statement

return;
return val;
return var;
return expr;
  

Example: Function Returning a Value

int RetVal() {
    return 5;
}

void main() {
    int x = RetVal();
    printf("x=%d", x);
}
    

Example: Function Returning Expression

int RetExp() {
    return 5 + 7;
}

void main() {
    int x = RetExp();
    printf("x=%d", x);
}
    

Example: Function Returning Variable

int RetVar() {
    int a = 10;
    return a;
}

void main() {
    int x = RetVar();
    printf("x=%d", x);
}
    

🚫 Functions Without Return Value

Use the keyword void when a function does not return any value. The return; statement may be used to indicate the end of the function.

Example: Function Returning Nothing

void NoRet() {
    printf("\nHello from NoRet");
    return;
}

void main() {
    NoRet();
    return;
}
    

Return Flow Diagram

main() RetVal() Call Return

🧠 Example: printf() Returning a Value

void main() {
    int x;
    x = printf("Best Computer Institute");
    printf("\nx=%d", x);
    return;
}
  

📝 Practice Questions

  1. Define a function getNumber() that returns 100. Display it in main().
  2. Define a function getCharacter() that returns 'A'. Display it in main().
  3. Define a function getPI() that returns 3.1415 as float. Print it in main().
  4. Define a function getMessage() that returns "Hello, C!". Display it in main().
  5. Define a function calculateSum() that returns sum of 10 and 20. Print it in main().
  6. Define a function isEven() that returns 1 if 10 is even, else 0. Display result in main().
  7. Define a function getMax() that returns larger of 25 and 40. Display result in main().
  8. Define a function getSquare() that returns square of 5. Display result in main().
  9. Define a function calculateFactorial() that returns factorial of 5. Display result in main().
  10. Define a function reverseNumber() that returns reverse of 123 (→ 321). Display result in main().
Parameters and Function Types in C

🔹 Parameters or Arguments

Parameters (or arguments) are values passed by the caller function at the time of invocation.

Example: Function Accepting No Arguments

void NoArgs(void) {
    printf("I won't accept arguments");
    return;
}

void withArgs(int x) {
    printf("\nx=%d", x);
    return;
}

void main() {
    NoArgs();
    withArgs(5);
    return;
}
    

🧩 Types of Parameters

  • Formal Parameters – Appear within parentheses inside the function header. Must be variables.
  • Actual Parameters – Appear inside the function invocation. Can be numbers or variables.

Example: Formal vs Actual Parameters

void test(int x, float y) { // x, y are formal parameters
    printf("x=%d", x);
    printf("\ny=%f", y);
    return;
}

void main() {
    test(23, 5.46); // 23, 5.46 are actual parameters
    return;
}
    

Illustration: Formal vs Actual Parameters

main() Actual Args: 23, 5.46 test(int x, float y) Formal Args: x, y Argument Passing

🔢 Types of Function Definitions

There are four types of function definitions based on arguments and return values:

  1. No arguments, no return value
  2. No arguments, with return value
  3. With arguments, no return value
  4. With arguments, with return value

Example 1: No Arguments, No Return Value

void add(void) {
    int x, y;
    printf("Enter any two numbers:");
    scanf("%d%d", &x, &y);
    printf("sum = %d", x + y);
    return;
}

void main() {
    add();
    return;
}
    

Example 2: No Arguments, With Return Value

int add(void) {
    int x, y;
    printf("Enter any two numbers:");
    scanf("%d%d", &x, &y);
    return x + y;
}

void main() {
    int res = add();
    printf("Sum=%d", res);
    return;
}
    

Example 3: With Arguments, No Return Value

void add(int a, int b) {
    printf("Sum=%d", a + b);
    return;
}

void main() {
    int x, y;
    printf("Enter any two numbers:");
    scanf("%d%d", &x, &y);
    add(x, y);
    return;
}
    

Example 4: With Arguments, With Return Value

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

void main() {
    int x, y;
    printf("Enter any two numbers:");
    scanf("%d%d", &x, &y);
    int res = add(x, y);
    printf("Sum=%d", res);
    return;
}
    

Function Type Matrix

No Args, No Return No Args, With Return With Args, No Return With Args, With Return

📝 Practice Questions

  1. Write a function without arguments and without return value that prints "Hello, World!".
  2. Write a function with arguments and no return value that takes two integers and prints their sum.
  3. Write a function without arguments but with return value that returns the number 100.
  4. Write a function with arguments and return value that takes two numbers and returns their product.
  5. Calculate the square of a number using functions:
    • (a) Without arguments and without return value
    • (b) With arguments and with return value
  6. Define a function that takes a character and prints whether it is a vowel or not.
  7. Create a function that takes radius and returns area of circle.
  8. Write a function to find maximum of three numbers using arguments and return value.
  9. Define void input() to accept integer and int double_it(int) to return its double.
  10. Create a menu-driven program for arithmetic operations: Add, Subtract, Multiply, Divide.
  11. Explain argument passing in C (by value vs by reference) with example.
  12. Write a function to reverse a number using arguments and return result.
  13. Define and use a function to check if a number is prime and print all primes between 1–100.