What are C Function Call Guide- Best Embedded C Programming Course

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

Free C Programming Course · Chapter 6 · Functions

C Function Call Guide

How a C function call really behaves: as an expression, as a statement, why void results cannot be used, and what happens inside the CPU while it runs. Part of our free C programming course for embedded systems.

4
Ways to use a call
1
Runnable demo
10
FAQ answers

Every useful firmware project is built from small functions that call each other, so understanding a C function call properly is the first real step from writing toy programs to writing embedded C programming code that you can trust. In this lecture of our free C programming course you will learn exactly where a call may appear in your code, what happens to the value it returns, and which forms are illegal. If you were searching for a free C tutorial that explains the idea with embedded examples instead of textbook trivia, you are in the right place.

What You Will Learn

By the end of this lecture

  • Use the value returned by a function as an operand inside any expression.
  • Call a function as a stand-alone statement and know what happens to a discarded result.
  • Explain why a function declared void can never supply a value.
  • Recognise why a call can never sit on the left side of an assignment.
  • Trace the exact order of execution when one function calls another.
  • Read real compiler diagnostics for each of these mistakes using gcc.
  • Apply the habits that keep a C function call safe in embedded C programming.

Prerequisites

You should already be comfortable with variables, data types, operators and the if and loop statements from the earlier chapters of this free embedded C course. You should also know what a function definition looks like: a return type, a name, a parameter list and a body in braces. A Linux PC, or WSL, with gcc installed is enough to run every example, because the programs only simulate hardware values with plain integers.

What Is a C Function Call?

A function definition on its own does nothing. It is only a recipe stored in memory. A C function call is the moment you ask the CPU to follow that recipe: you write the function name followed by parentheses that contain the arguments, and control jumps into the function body. When the body finishes, execution comes back to the exact place after the call and the program carries on.

Two facts follow directly from this and are worth memorising before we go further:

  • A function that is defined but never called never runs. Its code just sits in flash. Modern compilers will even warn you about it when the function is static.
  • A function that is called ten times runs its body ten times, with fresh copies of its parameters and local variables each time.
Anatomy of a function call
to_millivolts(2048)
function name+argument list=one call expression
call expression→has the function’s return type→and the returned value

Using a C Function Call as an Expression

When a function returns a value, the call itself is an expression whose type is the function’s return type and whose value is whatever the function returned. That means you can place it anywhere the language expects a value of that type. Four common positions are shown below.

Where the call sitsExampleWhat happens to the result
Right side of an assignmentint mv = to_millivolts(counts);Stored in a variable
Operand in arithmetic2 * to_millivolts(1000)Multiplied, then used further
Inside a conditionif (to_millivolts(4095) > 3000)Compared to decide a branch
Argument of another callclamp_percent(mv / 33)Passed straight into the next function

Nested calls in embedded C programming

Because every call is an expression, you can nest calls: the inner call is evaluated first and its result becomes the argument of the outer one. Firmware does this constantly. A raw ADC count goes through a conversion function, then a scaling function, then a limiting function, all written on one line.

One subtle rule is worth knowing early. When you write several calls in the same expression, for example f() + g(), the C language does not promise which of the two runs first. If both functions print or change shared state, the result depends on the compiler. Keep functions with side effects in separate statements when order matters.

A C function call as a statement

Place a semicolon after a call and it becomes a complete statement. This is the normal way to use functions that do work but have nothing to report, such as one that toggles an LED or writes a log line. It is also legal for a function that does return something: the value is simply thrown away.

blink_log(2);          /* void function used as a statement */
to_millivolts(500);    /* value returned, then discarded    */

Discarding is legal but often a bug in disguise. If a function returns an error code and you throw it away, a failed operation looks exactly like a successful one. When you really mean to ignore a result, say so explicitly with a cast to void, as in (void)to_millivolts(500);. Safety-oriented coding standards used in automotive and industrial firmware, such as MISRA C, ask for the returned value to be used for exactly this reason.

Why a void function call cannot supply a value

A function declared with the return type void promises to return nothing. Using such a call where a value is needed is meaningless, so the compiler rejects it. The following line is invalid for that reason:

int v = blink_log(2);   /* error: void value not ignored as it ought to be */

The error message from gcc is quite literal: you tried to use a value that does not exist. If you meet this error, either the function should have a real return type, or your code should not be treating it as if it did.

A C Function Call Cannot Be an Lvalue

The left side of an assignment must be something that denotes a storage location, such as a variable or an array element. The result of a C function call is just a temporary value; it has no address you can store into. So code like the next line is always invalid:

to_millivolts(10) = 5;  /* error: lvalue required as left operand of assignment */

Think of it as trying to write into the number seven. You can store seven into a variable, but you cannot store something into seven itself. (A function that returns a pointer can be used as *get_ptr() = 5; because the dereference creates a location, but that is a topic for the pointer chapters.)

How Execution Flows During a Function Call

Understanding the flow makes the rules above feel natural. When the program reaches a call, the following happens, in order. The exact instructions depend on the processor, but the idea is the same everywhere.

Life of one function call
1

Evaluate arguments

Each argument expression is computed and copied to the place the callee expects it.

2

Save return point

The address of the next instruction is remembered so the CPU can come back.

3

Run the body

Parameters and locals live in a fresh stack frame while the code executes.

4

Return

Execution ends at a return statement or the closing brace of the body.

5

Resume caller

The returned value, if any, replaces the call inside the caller’s expression.

On an ARM Cortex-M part that follows the standard ARM procedure call convention, the first four integer arguments travel in registers r0 to r3, the BL instruction stores the return address in the link register, and a returned integer comes back in r0. On a small 8-bit microcontroller the registers differ, but the same five stages exist. This is why arguments are copies: the callee works on its own copies and cannot change the caller’s variables unless you pass pointers.

Where does a function finish?

A function finishes in exactly one of two ways: control reaches the closing brace of the body, or a return statement is executed. The next lecture explores return in depth. For now, remember that a function with a non-void type must hand back a value on every path, otherwise the caller receives garbage.

Complete Example: Every Call Form in One Program

The following program simulates a 12-bit ADC with a 3300 mV reference. It uses a value-returning call in an assignment, as a nested argument, in a condition, in arithmetic, and as a statement. Save it as ep_call_basics.c.

#include <stdio.h>

static int clamp_percent(int raw)
{
    if (raw < 0)
        return 0;
    if (raw > 100)
        return 100;
    return raw;
}

static int to_millivolts(int adc_counts)
{
    return (adc_counts * 3300) / 4095;
}

static void blink_log(int times)
{
    for (int i = 0; i < times; i++)
        printf("LED toggle %d\n", i + 1);
}

int main(void)
{
    int counts = 2048;
    int mv  = to_millivolts(counts);
    int pct = clamp_percent(mv / 33);

    printf("counts=%d mv=%d pct=%d\n", counts, mv, pct);

    if (to_millivolts(4095) > 3000)
        puts("Battery full");

    printf("double reading = %d mV\n", 2 * to_millivolts(1000));

    blink_log(2);
    to_millivolts(500);
    return 0;
}

Build and run the function call demo

$ gcc -Wall -Wextra -o ep_call_basics ep_call_basics.c
$ ./ep_call_basics
counts=2048 mv=1650 pct=50
Battery full
double reading = 1610 mV
LED toggle 1
LED toggle 2

Walk through the output. The first line shows the nested pair: to_millivolts(2048) gives 1650, and dividing that by 33 gives 50, which clamp_percent leaves unchanged. The second line proves the call works inside an if. The third shows the arithmetic form, where 1000 counts become 805 mV and doubling gives 1610. The last two lines come from the statement-style call. Notice that the final call to to_millivolts(500) prints nothing, because its result was discarded.

Reading the Compiler Errors

Break the rules on purpose once; it is the fastest way to remember them. The table lists what gcc 13 reports for each mistake covered in this lecture.

MistakeTypical gcc message
Using a void call as a valueerror: void value not ignored as it ought to be
Call on the left of =error: lvalue required as left operand of assignment
Static function never calledwarning: 'never_called' defined but not used [-Wunused-function]

Always compile with -Wall -Wextra. It costs nothing and catches many problems before they reach your board.

Common Mistakes with a C Function Call

  • Ignoring return codes. A failed read or write goes unnoticed because the call was used as a bare statement.
  • Using a void function in an expression. Usually a sign the function should return a status.
  • Assuming left-to-right order. Two calls with side effects in one expression can run in either order.
  • Forgetting the parentheses. Writing blink_log; names the function but never calls it. Some compilers warn, some stay silent.
  • Passing the wrong number of arguments. Once a prototype is visible, the compiler catches this; the next lecture covers why that matters.

Best Practices, Performance and Security

Best practices

  • Use the returned value, or cast it to (void) to show the choice was deliberate.
  • Keep each statement to one side-effecting call whenever order matters.
  • Give functions names that read naturally inside an expression, such as is_ready() or to_millivolts().

Performance considerations

Every call costs a few instructions for argument setup, the branch, the return, and stack space for the frame. On a fast Cortex-M4 this is negligible in most code, but inside an interrupt service routine or a tight sample loop it adds up. Small functions that sit in the same file can be marked static so the compiler is free to inline them. Do not hand-inline everything; measure first.

Security considerations

Unchecked results are a classic source of security bugs in embedded devices, for example ignoring the length returned by a receive function and then reading past a buffer. Treat the return value of any function that can fail as part of the data flow, not as decoration.

Real-World Uses

ApplicationTypical call pattern
Sensor firmwareRaw read, convert, filter, each a nested call
BootloadersStatement calls for hardware setup, value calls for image checks
Communication stacksCalls in conditions such as if (frame_valid(buf))
Motor controlTime-critical calls inside timer interrupt handlers

Summary and Key Takeaways

Remember these points

  • A value-returning C function call is an expression and can sit in assignments, arithmetic, conditions and argument lists.
  • Adding a semicolon turns any call into a statement; any returned value is discarded.
  • A void function has no value, so it cannot be used inside an expression.
  • A call can never be placed on the left side of an assignment.
  • A function body runs only when called, and runs again for every call.

Conclusion

A function call looks like a tiny piece of syntax, yet it decides how data flows through your whole program. Once you see it as an expression with a type and a value, rules that seemed arbitrary become obvious: void has no value, a temporary value has no address, and a function that is never invoked never executes. In the next lecture of this free embedded C course we look at how the compiler learns about a function before the call, through the function declaration, also called the prototype. If you want to keep going with a free C tutorial built for firmware work, continue to the next lecture.

Frequently Asked Questions

What is a C function call?

It is the expression that transfers control to a function body, passing arguments, and then resumes the caller with the returned value when the function finishes.

Can I use a function call inside an if condition?

Yes. If the function returns an integer type, the call is an expression like any other, and a non-zero result counts as true.

What happens to the return value if I ignore it?

It is discarded. The program stays valid, but you lose any error information the function reported.

Why can’t I assign the result of a void function?

Because a void function returns no value, so there is nothing to store. The compiler stops you with an error.

Why can’t a function call appear on the left of an equals sign?

The left side must be a storage location. A returned value is a temporary with no location of its own.

Is the order of two function calls in one expression guaranteed?

No. C leaves the order of evaluation of operands unspecified, so place calls with side effects in separate statements.

Does a function that is never called use flash memory?

It can, unless the linker or compiler removes it. Compilers often drop unused static functions and warn about them.

How are arguments passed on ARM Cortex-M?

Under the standard ARM calling convention the first four integer arguments go in registers r0 to r3, and an integer result comes back in r0.

Should I cast ignored results to void?

If you truly mean to ignore a result, yes. It documents your intent and satisfies coding standards that require return values to be used.

Is this lecture part of a free C programming course?

Yes. It belongs to the free C programming course on EmbeddedPathashala, written for students who want embedded C programming skills.

Keep Building Your Embedded C Skills

Continue with the next lecture to learn how function declarations tell the compiler about your functions before they are called. Every lesson in this free embedded C course is written for beginners who want firmware-ready skills.

💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →