Introduction to function in c
In the realm of programming, particularly in the C language, the concepts of functions and modular programming play a pivotal role in creating organized, efficient, and maintainable code. These fundamental building blocks empower developers to craft solutions that are both easy to understand and powerful. Functions, in essence, are self-contained blocks of code designed to perform specific tasks. The beauty of functions lies in their ability to break down complex operations into manageable chunks, enhancing both code readability and reusability. .
What is function and function prototypes?
Function in c programming, they are self-contained blocks of code that perform specific tasks. They are designed to break down complex operations into smaller, manageable segments, making code more organized and readable. Functions can take inputs (known as parameters), process them, and optionally return an output value.
A function prototype, also known as a function declaration, serves as a blueprint for a function before its actual implementation. It provides the compiler with essential information about the function’s name, return type, and parameters, enabling the compiler to understand how the function should be called and used in the code.
How function prototypes work with compiler?
The function prototypes work with compiler as the following ways:
- The function’s return type:
This denotes the kind of data that the function will yield once it has been executed.. For instance, a function might return an integer, a floating-point number, or even no value at all (void). - The function’s name:
This is the identifier used to call the function in the code. It should be chosen to reflect the task the function performs. - The function’s parameters:
If the function requires inputs to perform its task, the prototype specifies the number, type, and order of these parameters. Parameters act as placeholders for data that will be passed into the function when it’s called.
Syntax: –
return_type function_name(parameters) {
// Function body
// Statements
// …
// Return statement (if applicable)
return value;
}
Let’s break down the components of the syntax:
- return_type:
This specifies the data type of the value that the function will return after its execution. It can be any valid data type in C, such as int, float, char, or even void if the function doesn’t return any value. - function_name:
This is the name of the function, which you choose to identify and call the function in your code. The name should be unique and relevant to the task the function performs. - parameters:
These are the inputs that the function receives when it is called. Parameters are enclosed in parentheses () and can include one or more variables separated by commas. If the function doesn’t require any inputs, you can leave the parentheses empty or use void to indicate no parameters. - Function body:
This is the part of the function that contains the actual code to perform the desired task. It consists of statements and operations that execute when the function is called. - return value:
If the function has a return type other than void, you use the return statement to send a value back to the calling code. The value should match the specified return type.
Example: – Declaring a function
// Function prototype
int add(int num1, int num2);
// Function definition
int add(int num1, int num2) {
int sum = num1 + num2; // Calculate the sum
return sum; // Return the result
}
In this example, the function add takes two integer parameters (num1 and num2), calculates their sum, and returns the result as an integer.
Passing parameters to function
Passing parameters to a function in C allows you to provide input values that the function can use for its operations. Parameters enable you to make your functions more versatile and capable of handling different data without having to rewrite the same code multiple times. Here’s how you pass parameters to a function:
- Defining the Function:
- When defining a function, you specify the parameters that the function will receive. Parameters are enclosed in parentheses after the function’s name.
- Calling the Function:
- When you call the function, you provide the actual values (arguments) for the parameters you defined. These parameters are provided to the function.
Here’s the syntax for passing parameters to a function:
// Function prototype
return_type function_name(parameter_type parameter_name);
// Function call
return_type result = function_name(argument_value);
Let’s break down the components:
- return_type: The data type that the function will return.
- function_name: The name of the function you’re defining or calling.
- parameter_type: The data type of the parameter you’re defining.
- parameter_name: The name of the parameter you’re defining.
- argument_value: The actual value you’re passing to the function when you call it.
- result: The variable where you’ll store the return value of the function.
Here’s an example using a simple function that calculates the sum of two integers:
#include <stdio.h>
// Function prototype
int add(int num1, int num2);
// Function definition
int add(int num1, int num2) {
return num1 + num2;
}
int main() {
int a = 8, b = 9;
// Calling function (sum) & passing arguments
int sum = add(a, b);
// Displaying the result
printf("The sum of numbers is %d\n", a, b, sum);
return 0;
}
Output: -
The sum of numbers is 17
[program terminated]
In this example, the function add takes two integer parameters num1 and num2. When you call the function add(a, b), the values of a and b are passed as arguments to the function. The result of the function’s operation is then stored in the sum variable.

Passing parameters: Pass by value: –
Passing parameters to a function in C can occur through different methods. One of the most common methods is known as “pass by value.” In this approach, when you pass a parameter to a function, a copy of the parameter’s value is made and provided to the function. This means any modifications made to the parameter within the function do not affect the original value outside the function.
How it’s work?
- The value of the parameter is copied and passed to the function.
- The function receives the copied value and performs operations using it.
- Any changes made to the copied value within the function do not impact the original value.
Example: – Suppose we have a simple function that aims to double an integer:
#include <stdio.h>
// Function prototype
int doubleValue(int num);
int main() {
int num = 5;
// Call the function and pass the value of 'num'
int doubled = doubleValue(num);
printf("Original value: %d\n", num); // Output: Original value: 5
printf("Doubled value: %d\n", doubled); // Output: Doubled value: 10
return 0;
}
// Function definition
int doubleValue(int num) {
num = num * 2; // Modify the parameter's copy
return num; // Return the modified value
}
Output: -
Original value: 5
Doubled value: 10
[program terminated]
In this example, the doubleValue function takes an integer parameter num. When the function is called with doubleValue(num), the value of num (which is 5) is copied and passed to the function. Inside the function, the copied value is modified to become 10, but the original value of num outside the function remains unchanged. Pass by value ensures that the original value of a variable is protected from unintended modifications within the function. However, it also means that any changes made to the parameter inside the function do not persist outside of it.
Write a program for addition of two number by using pass by value method: –
#include <stdio.h>
// Function prototype
int add(int num1, int num2);
int main() {
int a = 5, b = 7;
// Call the function and pass values of 'a' and 'b'
int result = add(a, b);
printf("The sum of %d and %d is %d\n", a, b, result);
return 0;
}
// Function definition
int add(int num1, int num2) {
return num1 + num2;
}
Output: -
The sum of 5 and 7 is 12
[program terminated]
Write a program for swapping of two number by using pass by value method: –
#include <stdio.h>
// Function prototype
void swap(int x, int y);
int main() {
int num1 = 35, num2 = 75;
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
// Call the function and pass values of 'num1' and 'num2'
swap(num1, num2);
return 0;
}
// Function definition
void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
printf("After swapping: num1 = %d, num2 = %d\n", x, y);
}
Output: -
Before swapping: num1 = 35, num2 = 75
After swapping: num1 = 75, num2 = 55
[program terminated]
Write a program to calculate factorial of a number by using pass by value method: –
#include <stdio.h>
// Function prototype
int factorial(int n);
int main() {
int num = 5;
// Call the function and pass the value of 'num'
int result = factorial(num);
printf("Factorial of %d is %d\n", num, result);
return 0;
}
// Function definition
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
Output: -
Factorial of 5 is 120
[program terminated]
Passing parameters: Pass by reference: –
Passing parameters by reference in C involves passing the memory address of a variable to a function, rather than just its value. This allows the function to directly modify the original variable’s value, even after the function call has finished. Pointer’s are used to achieve reference method.
How it’s work?
- Defining the Function:
- Instead of using the variable’s data type for the parameter, you use a pointer data type, typically data_type *.
- Calling the Function:
- When calling the function, you pass the memory address of the variable using the address-of operator &.
- Inside the Function:
- The parameter is now a pointer, so you can use the pointer to modify the value at the memory address it points to.
Example of passing parameters by reference in c:
#include <stdio.h>
// Function that modifies the value through pointers (pass by reference)
void modifyValue(int *ptr) {
*ptr = 10; // Modifying the value indirectly through the pointer
}
int main() {
int num = 5;
printf("Original value: %d\n", num);
// Passing the memory address of 'num' to the function
modifyValue(&num);
printf("Modified value: %d\n", num);
return 0;
}
Output: -
Original value: 5
Modified value: 10
[program terminated]
In this example, the modifyValue function takes a pointer to an integer as its parameter. Inside the function, by dereferencing the pointer with *ptr, we can access the memory location and modify the value directly. When we call modifyValue(&num), we’re passing the memory address of num to the function, allowing it to change the original value.
Write a program for swapping of two number by using pass by reference method: –
#include <stdio.h>
// Function to swap two numbers using pass by reference
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5, y = 10;
printf("Before swapping: x = %d, y = %d\n", x, y);
// Calling the swap function with addresses of x and y
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}
Output: -
Before swapping: x = 5, y = 10
After swapping: x = 10, y = 5
[program terminated]
Write a program for finding maximum between two numbers by using pass by reference method: –
#include <stdio.h>
// Function to find the maximum of two numbers using pass by reference
void findMax(int *a, int *b, int *max) {
if (*a > *b) {
*max = *a;
} else {
*max = *b;
}
}
int main() {
int num1 = 25, num2 = 18, maximum;
// Calling the findMax function with addresses of num1, num2, and maximum
findMax(&num1, &num2, &maximum);
printf("The maximum between %d and %d is %d\n", num1, num2, maximum);
return 0;
}
Output: -
The maximum between 25 and 18 is 25
[program terminated]
Write a program for calculating factorial by using pass by reference method: –
#include <stdio.h>
// Function to calculate factorial using pass by reference
void calculateFactorial(int n, int *result) {
*result = 1;
for (int i = 1; i <= n; i++) {
*result *= i;
}
}
int main() {
int num = 5, factorial;
// Calling the calculateFactorial function with the address of factorial
calculateFactorial(num, &factorial);
printf("The factorial of %d is %d\n", num, factorial);
return 0;
}
Output: -
The factorial of 5 is 120
[program terminated]
Recursion: understanding recursive functions and their implementation
Recursion is a powerful programming concept where a function calls itself in order to solve a problem. It’s a technique that’s often used to solve problems that can be broken down into smaller, similar subproblems. Recursive functions allow you to express complex problems in an elegant and concise manner. However, they need careful handling to avoid infinite loops or excessive memory consumption.
Understanding Recursive Functions:
In a recursive function, the problem is divided into smaller instances of the same problem. Each smaller instance is solved by calling the same function again, but with a different set of parameters. The recursion continues until a base case is reached – a simple problem that can be solved directly. At this point, the recursion stops and the function starts returning values back up the chain of function calls.
Anatomy of a Recursive Function
A recursive function consists of two essential components: the base case and the recursive case. The base case acts as a stopping point, preventing infinite recursion. The recursive case defines how the function calls itself, progressing toward the base case. It’s akin to solving a puzzle piece by piece, starting from the outer edge and working your way to the center.
Write a program for calculating factorial by using recursive function: –
#include <stdio.h>
// Recursive function to calculate factorial
int factorial(int n) {
// Base case: factorial of 0 is 1
if (n == 0) {
return 1;
}
// Recursive case: factorial(n) = n * factorial(n - 1)
return n * factorial(n - 1);
}
int main() {
int num = 5;
// Calling the recursive function to calculate factorial
int result = factorial(num);
printf("The factorial of %d is %d\n", num, result);
return 0;
}
Output: -
The factorial of 5 is 120
[program terminated]
Write a program for printing Fibonacci series by using recursive function: –
#include <stdio.h>
// Recursive function to calculate nth Fibonacci number
int fibonacci(int n) {
// Base cases: Fibonacci of 0 is 0, Fibonacci of 1 is 1
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
// Recursive case: fibonacci(n) = fibonacci(n - 1) + fibonacci(n - 2)
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int term = 9;
// Calling the recursive function to calculate Fibonacci term
int result = fibonacci(term);
printf("The %dth term of the Fibonacci sequence is %d\n", term, result);
return 0;
}
Output: -
The 9th term of the Fibonacci sequence is 34
[program terminated]
Write a program for printing pattern by using recursive function: –
#include <stdio.h>
// Recursive function to print a pattern
void printPattern(int n) {
if (n > 0) {
printf("* ");
printPattern(n - 1);
}
}
int main() {
int rows = 5;
// Calling the recursive function to print the pattern
for (int i = 1; i <= rows; i++) {
printPattern(i);
printf("\n");
}
return 0;
}
Output: -
*
* *
* * *
* * * *
[program terminated]
Read more about programming in c: –
Data type and Operator in C: – https://xcoode.co.in/c_programming/data-type-and-operators-in-c/
if else statement in c: – https://xcoode.co.in/c_programming/if-else-statement-in-c/
Loop’s in c: – https://xcoode.co.in/c_programming/loop-in-c/
Follow us on: –
Facebook page: – https://www.facebook.com/groups/294849166389180
Quora : – https://xcoode404.quora.com/