3.3 - Define and call functions

Functions in C are self-contained blocks of code that perform a specific task. They help in organizing code, improving readability, and facilitating reusability. By using functions, you can break down complex problems into smaller, manageable parts.
Function Components
Each function in C has several key components:
-
Function Declaration (or Prototype): This tells the compiler about the function’s name, return type, and parameters (if any). It is usually placed at the beginning of a program or in a header file.
-
Function Definition: This is where the actual code of the function resides. It includes the function’s name, return type, parameters, and the body of the function.
-
Function Call: This is where the function is executed in the program. When a function is called, the control of the program jumps to the function’s definition.
Defining a Function
A function is defined using the following syntax:
return_type function_name(parameter_list) {
// function body
}
- return_type: Specifies the type of value the function will return (e.g.,
int
,void
,float
, etc.). - function_name: The name of the function, which should be descriptive of what it does.
- parameter_list: A list of parameters that the function takes (optional). Each parameter includes a data type and a variable name.
Example of Function Definition
#include <stdio.h>
// Function declaration
int add(int a, int b);
// Function definition
int add(int a, int b) {
return a + b; // function body
}
In this example:
int add(int a, int b)
is a function that takes two integers as parameters and returns their sum.
Calling a Function
To call a function, simply use its name followed by parentheses. If the function takes parameters, pass the arguments inside the parentheses.
Example of Function Call
#include <stdio.h>
int add(int a, int b); // Function declaration
int main() {
int result;
// Function call
result = add(5, 10);
printf("The sum is: %d\n", result); // Output: The sum is: 15
return 0;
}
Function Return Values
Functions can return values using the return
statement. The type of value returned must match the declared return type of the function.
Example of Returning a Value
float divide(int a, int b) {
if (b != 0) {
return (float)a / b; // return the result of division
} else {
return 0; // return 0 if there is an attempt to divide by zero
}
}
Void Functions
If a function does not return any value, its return type is specified as void
. In this case, the function can still take parameters and execute a task.
Example of a Void Function
void print_message() {
printf("Hello, World!\n");
}
Function Overloading
Unlike C++, C does not support function overloading directly. Each function must have a unique name. However, you can achieve similar behavior by using different naming conventions.
Scope of Variables
Variables defined inside a function are local to that function and cannot be accessed from outside. This is known as local scope.
Example of Variable Scope
void exampleFunction() {
int localVar = 10; // local variable
printf("%d\n", localVar);
}
int main() {
// printf("%d\n", localVar); // This will cause a compilation error
exampleFunction();
return 0;
}
Default Arguments
C does not support default arguments in functions, unlike C++. You need to provide all arguments when calling the function.
Recursion
A function can call itself, which is known as recursion. Recursive functions must have a base case to prevent infinite recursion.
Example of a Recursive Function
int factorial(int n) {
if (n == 0) {
return 1; // base case
} else {
return n * factorial(n - 1); // recursive call
}
}
Function Pointer
In C, you can also use pointers to refer to functions, which allows for dynamic function calling and can be useful in callback mechanisms.
Example of Function Pointer
#include <stdio.h>
void greet() {
printf("Hello!\n");
}
int main() {
void (*func_ptr)() = greet; // function pointer
func_ptr(); // call the function using the pointer
return 0;
}
Summary
Functions are fundamental to writing organized and maintainable code in C. Understanding how to define, call, and manage functions is essential for programming effectively.
Functions allow for:
- Code Reusability: Functions can be reused across programs.
- Modularity: Breaking programs into smaller, manageable pieces.
- Ease of Debugging: Isolating sections of code for easier debugging.
By mastering functions, you’ll significantly improve your programming skills and enhance the quality of your code.