Skip to main content

Command Palette

Search for a command to run...

Introduction to Functions in C – Breaking Programs into Pieces

Published
4 min read
D

Heya! 👋 I love helping people, and one of the best ways I do this is by sharing my knowledge and experiences. My journey reflects the power of growth and transformation, and I’m here to document and share it with you.

I started as a pharmacist, practicing at a tertiary hospital in the Northern Region of Ghana. There, I saw firsthand the challenges in healthcare delivery and became fascinated by how technology could offer solutions. This sparked my interest in digital health, a field I believe holds the key to revolutionizing healthcare.

Determined to contribute, I taught myself programming, mastering tools like HTML, CSS, JavaScript, React, PHP, and more. But I craved deeper knowledge and practical experience. That’s when I joined the ALX Software Engineering program, which became a turning point. Spending over 70 hours a week learning, coding, and collaborating, I transitioned fully into tech.

Today, I am a Software Engineer and Digital Health Solutions Architect, building and contributing to innovative digital health solutions. I combine my healthcare expertise with technical skills to create impactful tools that solve real-world problems in health delivery.

Imposter syndrome has been part of my journey, but I’ve learned to embrace it as a sign of growth. Livestreaming my learning process, receiving feedback, and building in public have been crucial in overcoming self-doubt. Each experience has strengthened my belief in showing up, staying consistent, and growing through challenges.

Through this platform, I document my lessons, challenges, and successes to inspire and guide others—whether you’re transitioning careers, exploring digital health, or diving into software development.

I believe in accountability and the value of shared growth. Your feedback keeps me grounded and motivated to continue this journey. Let’s connect, learn, and grow together! 🚀

So far, all our code has lived inside main().

That works for small programs.

But what happens when your program grows?

It becomes:

  • Hard to read

  • Hard to debug

  • Hard to manage

This is where functions come in.

Functions help us organize our code into smaller, reusable pieces.

1. What Is a Function?

A function is:

A block of code that performs a specific task.

Instead of writing everything inside main(), we can create smaller units that handle specific jobs.

Think of it like this:

Instead of writing the same instructions again and again, you write them once and reuse them.

That is the power of functions.

2. Why Do We Need Functions?

Imagine this inside main():

  • Print a square

  • Calculate a sum

  • Check if a number is even

  • Print another square

If everything is inside main(), the code becomes messy.

Functions allow us to:

  • Reuse code

  • Reduce repetition

  • Make programs easier to understand

  • Debug more easily

Professional programs always use functions.

3. Basic Structure of a Function

Here is the general format:

return_type function_name(parameters) {
    // code
}

Let’s understand each part.

Return Type

This tells C what type of value the function sends back.

Examples:

  • int

  • float

  • char

  • void (returns nothing)

Function Name

The name you choose for the function.

Example:
add
printSquare
isEven

Choose meaningful names.

Parameters

These are inputs to the function.

They allow the function to receive values.

4. Example: A Simple Function

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

Let’s break it down:

  • The function name is add.

  • It takes two integers: a and b.

  • It returns their sum.

  • The return type is int.

5. Calling a Function

Defining a function is not enough.

We must call it.

Example:

#include <stdio.h>

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

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

What happens here?

  1. main() runs.

  2. It calls add(5, 3).

  3. The values 5 and 3 go into a and b.

  4. The function calculates 5 + 3.

  5. It returns 8.

  6. result stores 8.

  7. We print it.

That is function execution flow.

6. Void Functions (Functions That Return Nothing)

Not all functions need to return a value.

If a function only performs an action (like printing), we use void.

Example:

void greet() {
    printf("Hello!\n");
}

And call it like this:

greet();

Since it returns nothing, we do not store anything.

7. Functions With Parameters But No Return

Example:

void printNumber(int n) {
    printf("%d\n", n);
}

Call it like:

printNumber(10);

The function receives the value and prints it.

8. Important Concept: Execution Flow

When a function is called:

  • The program pauses main().

  • It jumps to the function.

  • It executes the function.

  • It returns to main().

Then execution continues.

Functions do not run automatically.
They only run when called.

9. Common Beginner Mistakes

  1. Forgetting the return statement in non-void functions

  2. Using the wrong return type

  3. Forgetting to call the function

  4. Confusing parameters with arguments

Parameters → variables in the function definition
Arguments → actual values passed when calling

Example:

add(5, 3);

5 and 3 are arguments.

10. Practice Exercises

  1. Write a function that multiplies two numbers.

  2. Write a function that prints 5 stars.

  3. Write a function that checks if a number is even (returns 1 if even, 0 if odd).

  4. Call each function from main().

Final Thoughts

Functions allow us to:

  • Break large programs into smaller pieces

  • Reuse logic

  • Keep code organized

  • Write cleaner programs

If nested loops taught you to think in layers, functions teach you to think in structure.

In the next lesson, we will go deeper into:

  • Function declarations (prototypes)

  • How the compiler reads your code

  • Variable scope (local vs global)

And that is where you start understanding how C really works behind the scenes.

More from this blog

D

Dr. Ehoneah Obed

78 posts

Software engineer writing about systems: in code, in learning, in life. I reverse-engineer complex problems into frameworks. Pharmacist → SWE → Founder.