2
3
4
5
C Functions
A function is a reusable block of code that performs a
specific task.
6
Why Use Functions?
• Modularity: Break down complex problems into smaller,
manageable pieces.
• Reusability: Write once, use many times, saving time and
effort.
• Maintainability: Easier to debug and update code when
changes are needed.
7
8
Advantages of user-defined function
❖ The program will be easier to understand, maintain and debug.
❖ Reusable codes that can be used in other programs
❖ A large program can be divided into smaller modules. Hence, a large
project can be divided among many programmers.
9
Types of functions in C
There are two types of function in C programming:
•Standard library functions
•User-defined functions
10
Standard library functions
The standard library functions are built-in functions in C programming.
These functions are defined in header files. For example,
•The printf() is a standard library function to send formatted output to
the screen (display output on the screen). This function is defined in
the stdio.h header file.
Hence, to use the printf() function, we need to include
the stdio.h header file using #include <stdio.h>.
•The sqrt() function calculates the square root of a number. The function
is defined in the math.h header file.
11
User-defined function
• Users can also create functions as per specific need. Such
functions created by the user are known as user-defined
functions.
12
13
Function Syntax and Structure in C
14
Return Values
•Definition: The value that a function sends back after execution.
•Return Types: Can be any data type (int, float, etc.).
float calculateLift(float airDensity, float velocity, float area)
{
return 0.5 * airDensity * velocity * velocity * area;
}
15
Function Parameters
•Definition: Variables used to pass data into functions.
•Types:
• Actual Parameters: Values passed during the
function call.
• Formal Parameters: Variables defined in the
function declaration.
16
• Function prototype: Declaration of a function that informs the
compiler about the function's name, return type, and parameters.
• Calling a function: To invoke/call the function so that its code is
executed.
• Function definition: Provides the actual implementation of the function,
including the function body.
• Return Statement: Value that a function sends back after execution.
17
18
19
20
Parameter Passing
Parameter passing refers to the method of providing input
values to functions.
Types:
• Pass by Value
• Pass by Reference/Pointer
21
Pass by Value
• Copies the actual value of an argument into the formal parameter
of the function.
• Changes made to the parameter inside the function do not affect
the original variable.
Pass by Reference
• Passes the address of the variable, allowing the function to
modify the original variable.
• Changes made to the parameter affect the original variable.
22
Call/Pass by Value
23
Call/Pass by Reference
24
Output: 1.2
25
26
27
28
: Preserving State between Function Calls
29
#include <stdio.h>
void logFault(const char* faultMessage) {
// Static variable to keep track of the number of faults
detected
static int faultCount = 0;
// Increment the fault count
faultCount++;
// Log the fault message and the count of faults
printf("Fault #%d: %s\n", faultCount, faultMessage);
} Fault #1: Sensor failure
detected.
int main() { Fault #2: GPS signal lost.
// Simulating faults being detected in the system Fault #3: Low battery
logFault("Sensor failure detected."); warning.
logFault("GPS signal lost.");
logFault("Low battery warning.");
// Further logic can be implemented here...
return 0; 30
}
#include <stdio.h>
void counter() {
// Static variable retains its value between
function calls
static int count = 0;
count++;
printf("Function call count: %d\n", count);
}
int main() {
counter();
counter();
counter();
return 0;
}
31
32
Scenario based questions on avionics
software
• In an aircraft’s embedded system, why is it important to understand
the lifetime of static variables when dealing with sensor calibration
routines?
• A global variable controlling landing gear status is accessible from
multiple functions. Discuss the potential risks and how scope
management can reduce faults in avionics software.
33
34