Functions in C Programming

Function call and return flow in C

A function is a named block of C code that performs one task. Functions reduce repetition, divide a program into smaller modules, accept arguments when needed and can return a value to the caller.

Function Syntax Top ↑

return_type function_name(parameter_list) {
    // statements
    return value;
}

Use void when a function does not return a value. A function can also have no parameters, for example void show_message(void).

Average of Two Numbers Top ↑

#include <stdio.h>

float average(float a, float b) {
    return (a + b) / 2.0f;
}

int main(void) {
    float result = average(10.0f, 11.0f);
    printf("Average = %.2f\n", result);
    return 0;
}

Output

Average = 10.50

Key point: the function receives two arguments and returns one float value.

Function Prototype Top ↑

If the function definition appears after main(), declare a prototype before it:

float average(float a, float b);

int main(void) {
    float result = average(10.0f, 11.0f);
}

float average(float a, float b) {
    return (a + b) / 2.0f;
}

Convert a Lowercase Letter to Uppercase Top ↑

The standard C library already provides toupper() in <ctype.h>. This user-defined version shows the character arithmetic directly. The older strupr() example is not part of ISO C.

#include <stdio.h>

char to_upper(char ch) {
    if (ch >= 'a' && ch <= 'z') {
        return (char)('A' + ch - 'a');
    }
    return ch;
}

int main(void) {
    char ch = 'g';
    printf("%c\n", to_upper(ch));
    return 0;
}

Passing a Pointer to a Function Top ↑

Pass an address when the function needs to modify the caller's object.

#include <stdio.h>

void add_bonus(double *salary) {
    *salary *= 1.15;
}

int main(void) {
    double salary = 1000.0;
    add_bonus(&salary);
    printf("Final salary = %.2f\n", salary);
    return 0;
}

Passing an Array to a Function Top ↑

When an array is passed to a function, also pass its element count instead of hard-coding the size.

#include <stdio.h>
#include <stddef.h>

int array_sum(const int values[], size_t count) {
    int sum = 0;

    for (size_t i = 0; i < count; i++) {
        sum += values[i];
    }

    return sum;
}

int main(void) {
    int values[] = {1, 2, 3, 4, 5};
    size_t count = sizeof values / sizeof values[0];

    printf("Sum = %d\n", array_sum(values, count));
    return 0;
}

Static Local Variable Top ↑

A normal local variable is created again for each call. A static local variable keeps its value between calls.

#include <stdio.h>

int running_sum(int value) {
    static int sum = 0;
    sum += value;
    return sum;
}

int main(void) {
    printf("%d\n", running_sum(5));
    printf("%d\n", running_sum(3));
    return 0;
}

Output

5
8
Key points: a function runs only when called; parameters are optional; returning a value is optional; functions may call other functions; use prototypes when a function is defined after its first use.

Practice basic C function questions →

Practice Questions Top ↑

  1. Write a function that returns the sum, difference, product or quotient of two numbers based on an operation choice.
  2. Write a function that joins two strings using a separator.
  3. Write a function that swaps two integer values using pointers.
  4. Write a function that checks whether a number is even or odd.
  5. Write a function that returns the square of an input number.
  6. Use a static local variable to maintain a running total until the user enters 0.




plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer