Function
Function
Understanding
Functions in C Programming Language
Function is basically a set of statements that takes inputs, perform some computation and produces
output.
Syntax:
Return_type function_name(set_of_inputs);
A function in C is like a small machine inside a big factory. This machine (function) performs a specific
task whenever needed. Instead of writing the same code multiple times, we write it once inside a
function and call it whenever required.
1. Reusability: Once the function is defined, it can be reused over and over again.
2. Abstraction: Hiding the complex things. Ex: if you are just using the function in your
program then you don’t have to worry about how it works inside! Ex: Scanf and printf
functions.
3. Easy to Debug – If there's an error, we only need to fix the function instead of searching
through the whole program.
4. Better Organization – Functions make the program neat and easy to understand.
• As you know how to declare a variable with data type, similarly function declaration also called as
function prototype known as function declaration to declaring the properties of a function to the
compiler.
• Ex : int fun(int, float) ;
Properties :
• Function definition consists of block of code which is capable of performing some specific task.
• SEE ABOVE EXAMPLE
• Function calling in C is the process of invoking a function to execute its task. It can be done using
call by value (passes a copy) or call by reference (passes the actual variable's address).
• See Above Example.
Types of Functions in C
1. Call by Value
2. Call by reference
1. Call by Value: Here values of actual parameters will be copied to parameters and these two
different parameters store values in different locations.
1. Function with arguments and return value int add (int, int); Yes
2. Function with arguments but no return value void greet (int num ); Yes
3. Function without arguments but with return value int getNumber (); No
Output:
Sum = 30
A function that takes input but does not return any value. Instead, it performs some operation directly.
Output:
No arguments (())
Returns value (return 100;)
Output:
Number = 100
A function that does not take input and does not return any value. It simply performs an action.
Output:
Hello, World!
Write a C program that swaps two numbers using a function with call by value. Ensure that the
swapped numbers are not reflected in the main function.
Explanation: In call by value, the actual values passed to the function are copied into the
function parameters. Any changes made inside the function will not affect the original variables in
the calling function.
Write a C program to calculate the area of a rectangle. The function should take the length and
width as arguments.
Explanation: Here, you pass the length and width of the rectangle to the function, which
calculates the area.
Write a C program to find the maximum of two numbers using a function with call by value.
Write a C program that checks whether a number is even or odd using call by value.
Write a C program that calculates the sum of digits of a given number using call by value.
Advanced Concepts in C Functions
2. Now, let's dive deeper into Passing by Reference, Returning by Reference, Function Pointers
and, Function Pointer as Argument in C.
1. Passing by Reference in C
In C, passing by reference means passing the memory address (pointer) of a variable to a function
instead of its value. This allows the function to modify the original variable directly.
Code Example
Explanation:
Output:
✅ Useful for modifying original values, like arrays, structures, and linked lists.
2. Returning by Reference in C
C does not support returning local variables by reference because they get destroyed when the function
exits. However, you can return a reference using static variables or dynamically allocated memory.
Explanation:
• static int num remains in memory even after the function exits.
Output:
Value = 100
⚠️ Avoid returning local variables (they get deleted when the function exits).
A function pointer is a pointer that stores the address of a function instead of a variable.
Code Example
Explanation:
• int (*funcPtr)(int, int); → Declares a function pointer that takes two int arguments and returns int.
Output:
Sum = 30
Code Example
Explanation:
• We pass either add or subtract, and compute () calls the respective function.
Output:
Result = 15
Result = 5
✅ Useful for implementing callback functions (e.g., sorting algorithms, event handlers).
Passing by Reference Passing memory address to modify original value swap(int *a, int *b)
int (*funcPtr)(int,
Function Pointer Pointer storing a function's address
int);
Function Pointer as
Passing a function pointer to another function compute(add, x, y);
Argument
Final Thoughts
✅ Passing by reference is useful for modifying original values (e.g., arrays, structures).
✅ Returning a reference helps preserve values across function calls.
✅ Function pointers enable dynamic function selection, callbacks, and efficient code design.
A storage class in C tells where a function or variable is stored, how long it stays in memory, and
where it can be accessed from.
There are five storage classes in C, but only three can be used for functions:
1. static
2. extern
3. inline
auto and register are only for variables, not for functions!
• If the Wi-Fi is open, anyone in the house can connect and use it.
Code Example
#include <stdio.h>
int main()
{
sharedFunction(); // Calls the function from file2.c
return 0;
}
File 2: file2.c (Another File)
#include <stdio.h>
A static function can only be used in the same file where it is declared.
It cannot be accessed from another file.
This is useful when we want to hide a function so that other parts of the program can’t use it
directly.
Example:
• If you lock the door, only you can eat the chocolates.
• No one from outside can enter and take them!
• This is like a static function—it can only be used in the same file where it is written.
Code Example
An inline function is a suggestion to the compiler to replace the function call with the actual code.
This makes the program run faster, but increases code size.
It is useful for small functions that are called many times.
• Instead of writing your full name every time, you can use a stamp .
• This is like an inline function—it saves time by copy-pasting the function’s code instead of
calling it again and again.
Code Example
#include <stdio.h>
int main()
{
printf("Square of 5: %d\n", square(5)); // The function body is directly inserted here
return 0;
}
Instead of calling square(5), the compiler replaces it with 5 * 5, making it faster.
auto and register cannot be used for functions, so they are not important here.
static No (Private to the file) Until program ends When you want to hide a function
Final Thoughts: