🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Function Arguments and Return Values
Lecture 14 of our free C programming course: learn how data goes into a C function and how a result comes back, with examples from embedded C programming.
Welcome to another lecture of our free C programming course on EmbeddedPathashala. In this free C tutorial we study the most useful kind of function you will ever write: one that takes input arguments and gives back a return value. If you want to learn embedded C programming, this pattern is everywhere. Reading a sensor, converting an ADC count to millivolts, computing a checksum and clamping a PWM duty cycle all use functions with arguments and a return value.
What You Will Learn
- The difference between actual arguments and formal arguments
- How the return statement sends a value back to the caller
- Why C copies argument values (call by value) and what that means for your variables
- How to write functions of any data type, not only int
- How this style of function is used in C programming for embedded systems
- Common mistakes and a short troubleshooting checklist
Prerequisites
You should already be comfortable with the basics from the earlier lectures of this free embedded C course: declaring variables, using operators, writing if and loop statements, and the idea of a function call and a function definition. If functions without arguments still feel new, revisit lectures 9 to 13 first.
Functions That Receive Data and Give Data Back
In earlier lectures we met functions that take nothing and return nothing, and functions that take values but return nothing. The fourth and most flexible category is the function with arguments and a return value. The caller hands over some inputs, the function computes something, and the answer travels back through the return statement so the caller can store it or use it inside a larger expression.
Think of a vending machine. You insert coins and press a button (the arguments), the machine does its internal work, and a product drops out (the return value). You do not care how the mechanism works; you care about what goes in and what comes out. A well written C function offers exactly that kind of contract.
General Shape of Such a Function
return_type function_name(type1 param1, type2 param2)
{
/* work using param1 and param2 */
return result; /* result must match return_type */
}
The type written before the function name is the type of the value that comes back. The list inside parentheses names every input and gives each one a type. The return statement ends the function immediately and delivers one value.
Actual Arguments and Formal Arguments
Two names confuse many beginners, so let us fix them clearly. The values or variables you write inside the parentheses at the call site are the actual arguments. The variables declared in the function header are the formal arguments, also called parameters. At the moment of the call, each actual argument is evaluated and its value is copied into the matching formal argument, left to right by position.
| Property | Actual argument | Formal argument |
|---|---|---|
| Where it appears | In the function call | In the function definition header |
| What it can be | Constant, variable or expression | Only a declared variable with a type |
| Lifetime | Belongs to the caller | Created at call, destroyed at return |
| Name must match? | No | No, only position and type matter |
Call by Value in One Sentence
C passes arguments by value. The function receives its own private copy, so assigning to a formal argument changes only the copy. This protects the caller from accidents, but it also means a function cannot modify the caller’s variable through a plain argument. We will see how pointers solve that later in this free C tutorial.
First Example: Power From Voltage and Current
#include <stdio.h>
int power_watts(int volts, int amps);
int main(void)
{
int v = 12;
int a = 5;
int p = power_watts(v, a);
printf("Load draws %d W\n", p);
printf("Double load draws %d W\n", power_watts(v, a * 2));
return 0;
}
int power_watts(int volts, int amps)
{
return volts * amps;
}
Walk through it slowly. The prototype before main tells the compiler the shape of power_watts. In main, the first call copies 12 and 5 into the formal arguments, multiplies them and returns 60. That value is stored in p. The second call uses the call itself inside printf, which is perfectly legal because a function call that returns a value is an expression. Note that the argument a * 2 is an expression, evaluated before the copy happens.
Load draws 60 W
Double load draws 120 W
Second Example: ADC Count to Millivolts
Here is a pattern straight from C programming for embedded systems. A 12 bit ADC gives a number from 0 to 4095, and the reference voltage is known. We want the voltage in millivolts without floating point, because many small microcontrollers have no floating point unit and software float is slow and large.
#include <stdio.h>
unsigned long adc_to_mv(unsigned int raw, unsigned int vref_mv)
{
return ((unsigned long)raw * vref_mv) / 4095UL;
}
int main(void)
{
unsigned int sample = 2048;
unsigned long mv = adc_to_mv(sample, 3300);
printf("raw=%u gives %lu mV\n", sample, mv);
return 0;
}
raw=2048 gives 1650 mV
The cast to unsigned long before the multiplication is deliberate. On a 16 bit int target, 4095 times 3300 would overflow an unsigned int. Choosing the return type and the intermediate type carefully is a daily habit in embedded C programming.
Return Types Other Than int
Many introductory examples use int everywhere for simplicity, and that is only a teaching shortcut. A function can return char, float, double, long, an unsigned type, a pointer or a structure. The returned expression is converted to the declared return type, so mismatches may silently lose information. Let us see a float returning function and a character returning one.
#include <stdio.h>
float celsius_to_fahrenheit(float c)
{
return (c * 9.0f / 5.0f) + 32.0f;
}
char grade_from_score(int score)
{
if (score >= 90) return 'A';
if (score >= 75) return 'B';
if (score >= 50) return 'C';
return 'F';
}
int main(void)
{
printf("36.6 C is %.1f F\n", celsius_to_fahrenheit(36.6f));
printf("Score 82 earns grade %c\n", grade_from_score(82));
return 0;
}
36.6 C is 97.9 F
Score 82 earns grade B
Multiple return statements
A function may contain several return statements. The first one executed ends the function. In grade_from_score above, the early returns remove the need for else branches and keep the logic flat.
Using the Returned Value
You have three choices when a function returns something. You can store it in a variable, use it directly inside an expression or another call, or ignore it. Ignoring is legal in C, but for functions that report errors it is a classic source of bugs. Compare the options:
| Style | Example | When to use |
|---|---|---|
| Store | int p = power_watts(v, a); | Value needed more than once |
| Inline in expression | total = power_watts(v, a) + 5; | Value needed once |
| Pass to another function | printf(“%d”, power_watts(v, a)); | Quick output or chaining |
| Ignore | power_watts(v, a); | Almost never for pure calculations |
Real World Uses in Embedded Systems
- Sensor drivers return a calibrated reading from a raw register value
- Protocol code returns a checksum or a status code after parsing a frame
- Motor control returns a clamped duty cycle between 0 and 100
- Timing helpers return elapsed ticks given two timestamps
Common Mistakes and Troubleshooting
Mistake 1: Falling off the end of a non void function without returning a value. The caller then reads an indeterminate result. Turn on compiler warnings with -Wall and the compiler will tell you.
Mistake 2: Expecting the caller’s variable to change after you modify a formal argument. Remember, only the copy changes.
Mistake 3: Using the value of a function declared void in an expression. The compiler rejects this, and that rejection is a feature.
Mistake 4: Passing the arguments in the wrong order when both have the same type, such as adc_to_mv(3300, sample). The compiler cannot catch this, so choose clear parameter names.
gcc -Wall -Wextra -o demo cp6_14_demo.c
./demo
Best Practices
- Give parameters meaningful names that describe units, for example vref_mv
- Keep a function focused on one job, so its return value has one clear meaning
- Prefer fixed width types such as uint16_t when you target microcontrollers
- Always declare the prototype before the first call
- Compile with warnings enabled and treat them as errors in team projects
Performance note
Each call copies arguments and jumps to the function. For tiny hot functions in an interrupt handler, compilers can inline them, so write readable functions first and measure before optimizing.
Summary and Key Takeaways
- A function with arguments and a return value accepts inputs and delivers one result
- Actual arguments are in the call, formal arguments are in the definition
- Values are copied, so the caller’s variables stay safe
- Return type can be any type, and the returned expression is converted to it
- Embedded programs use this pattern for conversions, checksums and drivers
In this lecture of the free embedded C course you learned how data enters and leaves a function. In the next lecture we study where function declarations should be placed and what the compiler assumes when you leave one out.
Frequently Asked Questions
What is the difference between actual and formal arguments in C?
Actual arguments are the values supplied in the function call. Formal arguments are the parameter variables in the function definition that receive copies of those values.
Can a C function return more than one value?
Not directly. You can return a structure or use pointer parameters, which later lectures of this free C programming course explain.
Does changing a parameter inside a function change the caller’s variable?
No. C uses call by value, so the function modifies only its own copy.
What happens if I forget the return statement?
For a non void function the result is indeterminate, and using it is a bug. Enable compiler warnings to catch it.
Is this free C tutorial useful for embedded C programming?
Yes. Every example is chosen so the same habits apply on microcontrollers.
Can the return value be ignored?
Yes, but ignoring status codes hides errors, so avoid it unless the value truly does not matter.
Continue the Free C Programming Course
Keep going through our free embedded C course and build real skills for firmware jobs.

2 Comments