C Functions

Building blocks of C programming

🔧 What are C Functions?

Functions are reusable blocks of code that perform specific tasks. They help organize your program, avoid repetition, and make code easier to understand and maintain.


// Simple function example
#include <stdio.h>

void sayHello() {
    printf("Hello, World!\n");
}

int main() {
    sayHello();  // Call the function
    return 0;
}
                                    

Types of Functions

đŸšĢ

Void Functions

Functions that don't return a value

void printMessage() {
    printf("Hello!\n");
}
â†Šī¸

Return Functions

Functions that return a value

int addNumbers(int a, int b) {
    return a + b;
}
📚

Library Functions

Built-in functions from libraries

printf("Text");
scanf("%d", #);
strlen(string);
👤

User-Defined

Functions you create yourself

int square(int x) {
    return x * x;
}

🔹 Function Declaration and Definition

Functions must be declared before use and defined with their complete implementation for proper program structure. Declaration establishes the function signature including return type, name, and parameters, informing the compiler about function existence. Definition provides the actual code that executes when functions are called. Separating declaration and definition enables flexible code organization, allows forward references, and supports modular programming practices.

#include <stdio.h>

// Function declaration (prototype)
int multiply(int x, int y);

int main() {
    int result = multiply(5, 3);
    printf("Result: %d\n", result);
    return 0;
}

// Function definition
int multiply(int x, int y) {
    return x * y;
}

Output:

Result: 15

🔹 Function with No Parameters

Simple functions without parameters provide reusable code blocks for tasks requiring no input data. These functions perform fixed operations, display information, or execute predetermined logic every time called. Use functions with no parameters when operations remain constant across calls or when input comes from other sources like user input or file reading. This pattern encourages code organization and enables easy modification of program behavior by updating single function definitions.

#include <stdio.h>

void displayMenu() {
    printf("=== MAIN MENU ===\n");
    printf("1. Start Game\n");
    printf("2. Settings\n");
    printf("3. Exit\n");
}

int main() {
    displayMenu();
    return 0;
}

Output:

=== MAIN MENU ===
1. Start Game
2. Settings
3. Exit

🔹 Function with Return Value

Functions that calculate and return results provide output values enabling complex program logic and data processing. Return statements pass values back to calling code, enabling functions to communicate results of calculations or operations. Single return values suffice for most functions, though pointers enable returning multiple values through modification. Well-designed functions with return values enable building complex algorithms through composition of simpler functions.

#include <stdio.h>

int findMax(int a, int b) {
    if (a > b) {
        return a;
    } else {
        return b;
    }
}

int main() {
    int num1 = 15, num2 = 23;
    int maximum = findMax(num1, num2);
    printf("Maximum is: %d\n", maximum);
    return 0;
}

Output:

Maximum is: 23

🔹 Function Benefits

Functions provide essential benefits for writing organized, maintainable, and efficient C programs. Functions enable code reuse by writing logic once and using it multiple times, improve organization by grouping related code, facilitate debugging by isolating functionality, and promote modularity enabling team development. Functions reduce code duplication, make programs easier to understand and modify, and enable testing individual components independently for higher code quality.

Advantages of Functions:

  • Reusability: Write once, use multiple times
  • Organization: Break large programs into smaller parts
  • Debugging: Easier to find and fix errors
  • Readability: Makes code easier to understand
  • Maintenance: Easier to update and modify
// Without functions - repetitive code
printf("Welcome to our store!\n");
printf("Thank you for shopping!\n");
printf("Welcome to our store!\n");
printf("Thank you for shopping!\n");

// With functions - clean and reusable
void welcomeMessage() {
    printf("Welcome to our store!\n");
    printf("Thank you for shopping!\n");
}

// Call function multiple times
welcomeMessage();
welcomeMessage();

🧠 Test Your Knowledge

What keyword is used for functions that don't return a value?