0% found this document useful (0 votes)
6 views

Functions

The document provides an overview of functions in C programming, detailing their components, syntax, types, and benefits. It explains function declaration, definition, and calling, along with examples of standard and user-defined functions. Additionally, it covers parameter passing methods, recursion, and provides examples such as calculating factorials and Fibonacci numbers.

Uploaded by

ragavik430
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Functions

The document provides an overview of functions in C programming, detailing their components, syntax, types, and benefits. It explains function declaration, definition, and calling, along with examples of standard and user-defined functions. Additionally, it covers parameter passing methods, recursion, and provides examples such as calculating factorials and Fibonacci numbers.

Uploaded by

ragavik430
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Functions

In C, a function is a block of code that performs a specific task.


It is a self-contained unit that can be called to execute its code
when needed.
Functions allow you to break down a program into smaller,
manageable, and reusable components, which helps improve
code organization, readability, and maintainability.

January 23, 2025 1


Components of a Function

• A C function generally consists of the following components:


• Return Type: The type of value the function will return. If the
function does not return any value, it uses the void return
type.
• Function Name: A unique identifier used to call the function.
• Parameters (Optional): A list of inputs to the function. These
are passed when calling the function.
• Function Body: The block of code that defines what the
function does.

January 23, 2025 2


Syntax of a Function

• return_type function_name(parameter_list) {
// body of the function
// code to be executed
}
• return_type: The type of the value returned by the function
(e.g., int, float, void).
• function_name: The name of the function.
• parameter_list: The list of parameters the function takes.
Parameters are optional and can be left empty

January 23, 2025 3


Example of simple Function

• #include <stdio.h>
• // Function declaration (prototype)
• int add(int, int);
• int main() {
• int result = add(5, 3); // Function call
• printf("Result: %d\n", result);
• return 0;
•}
• // Function definition
• int add(int a, int b) {
• return a + b;
•}
January 23, 2025 4
Functions
• Function Declaration (Prototype):
Before using a function in main(), we declare its signature at the top of the
program:
int add(int, int);
This tells the compiler that the function add will take two integers as
parameters and return an integer.
• Function Definition:
The function add is defined after the main() function. It takes two integers
(a and b), adds them, and returns the sum:
int add(int a, int b) {
return a + b;
}
• Function Call:
In the main() function, we call the add function with the arguments 5 and 3:
int result = add(5, 3);
• Return Value:
The function returns the sum, which is stored in the variable result.
January 23, 2025 5
Functions

• Types of Functions
• Standard Functions: Functions that come predefined in the C
library, such as printf(), scanf(), strlen(), etc.
• User-Defined Functions: Functions created by the
programmer to perform specific tasks, like the add function
above.

January 23, 2025 6


Function Return Types

• void: A function with a void return type does not return any
value.
void printMessage() {
printf("Hello, World!\n");
}
• Non-void: Functions can return values like int, float, char, etc.
int multiply(int x, int y) {
return x * y;
}

January 23, 2025 7


Function Parameters
• Functions can take zero or more parameters. Parameters allow
you to pass data to the function.
• No parameters:
void greet() {
printf("Hello, User!\n");
}
• With parameters:
void greetUser(char name[]) {
printf("Hello, %s!\n", name);
}
• Multiple parameters:
int sum(int a, int b) {
return a + b;
} January 23, 2025 8
Functions
• Benefits of Using Functions
• Code Reusability: Functions allow you to reuse code without
rewriting it. You can call the same function from different
parts of the program.
• Modularity: Functions divide a large program into smaller,
manageable pieces.
• Maintainability: If you need to update the logic of a specific
task, you can change the function instead of modifying the
whole program.
• Debugging: Functions make it easier to isolate problems and
debug specific parts of your program.

January 23, 2025 9


Functions
• This example demonstrates a simple function that prints a
message but does not take any input or return any value.
• #include <stdio.h>
// Function declaration
void greet() {
printf("Hello, Welcome to C programming!\n");
}
int main() {
// Function call
greet();
return 0;
}

January 23, 2025 10


Function- Finding Factorial
#include <stdio.h>
int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i; // Multiply fact by i in each iteration
}
return fact;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
int result = factorial(num); // Call to the factorial function
printf("Factorial of %d is %d\n", num, result);
return 0;
}

January 23, 2025 11


Function- checking prime or not
• int isPrime(int n) {
• if (n <= 1) {
• return 0; // 0 and 1 are not prime numbers
• }
• // Check for divisibility from 2 to n-1
• for (int i = 2; i <= n-1; i++) {
• if (n % i == 0) {
• return 0; // n is divisible by i, so it's not a prime number
• }
• }
• return 1; // n is prime if no divisors were found
•}

January 23, 2025 12


Function- checking prime or not
• int main() {
• int num;
• // Input from the user
• printf("Enter a number: ");
• scanf("%d", &num);
• // Check if the number is prime
• if (isPrime(num)) {
• printf("%d is a prime number.\n", num);
• } else {
• printf("%d is not a prime number.\n", num);
• }
• return 0;
•}

January 23, 2025 13


Function- checking leap year or not
• #include <stdio.h>

• int isLeapYear(int year) {
• // A year is a leap year if:
• // 1. It is divisible by 4, but not divisible by 100, or
• // 2. It is divisible by 400.
• if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
• return 1; // It's a leap year
• } else {
• return 0; // It's not a leap year
• }
•}

January 23, 2025 14


Function- checking leap year or not
• int main() {
• int year;

• // Input year from the user
• printf("Enter a year: ");
• scanf("%d", &year);

• // Check if the year is a leap year
• if (isLeapYear(year)) {
• printf("%d is a leap year.\n", year);
• } else {
• printf("%d is not a leap year.\n", year);
• }

• return 0;
• }

January 23, 2025 15


Methods of Parameter Passing
• In C programming, there are two primary methods of
parameter passing to functions:
• 1. Pass by Value
• In pass by value, the actual parameter (the argument used in
the function call) is copied to the formal parameter (the
parameter variable defined in the function).
• This means that changes made to the parameter inside the
function do not affect the original argument outside the
function.
• Whatever changes made to formal arguments do not reflect
on actual arguments outside the function.

January 23, 2025 16


Methods of Parameter Passing
#include <stdio.h>
void add(int a, int b) {
a = a + b; // Modifies the local copy of 'a'
printf("Inside function: %d\n", a); // Prints the modified value of
'a'
}
int main() {
int x = 5, y = 3;
add(x, y); // Passes a copy of 'x' and 'y'
printf("Outside function: %d\n", x); // 'x' remains unchanged
return 0;
}

January 23, 2025 17


Methods of Parameter Passing
• output
Inside function: 8
Outside function: 5

• Explanation: In this case, the values of x and y are passed to


the function add(). Inside the function, a is modified, but the
original value of x in main() remains unchanged.

January 23, 2025 18


Methods of Parameter Passing
• 2. Pass by Reference (Using Pointers)
• In pass by reference, instead of passing the actual values of
the arguments, the memory addresses (pointers) of the
variables are passed to the function. This allows the function
to directly modify the values of the arguments.
• This is achieved by passing the address of the variables (using
the address-of operator &).

January 23, 2025 19


Methods of Parameter Passing
#include <stdio.h>
void add(int *a, int *b) {
*a = *a + *b; // Dereferencing the pointer to modify the original
value
printf("Inside function: %d\n", *a); // Prints the modified value of
*a
}
int main() {
int x = 5, y = 3;
add(&x, &y); // Passes the address of 'x' and 'y'
printf("Outside function: %d\n", x); // 'x' is modified
return 0;
}

January 23, 2025 20


Methods of Parameter Passing
Output:
Inside function: 8
Outside function: 8
• Explanation: In this case, the function add() receives pointers to x
and y, and by dereferencing those pointers (*a and *b), it modifies
the actual values of x and y in main().

Summary:
Pass by Value: The function works with a copy of the argument, and
changes to the parameter do not affect the original argument.
Pass by Reference: The function works with the actual memory
address of the argument, so changes to the parameter affect the
original argument.

January 23, 2025 21


Recursive Functions
Recursion is a programming technique where a function calls
itself in order to solve a problem.
A recursive function typically breaks down a problem into
smaller subproblems and solves them in a similar manner.
Recursion is especially useful for problems that have a
repetitive structure or can be divided into smaller identical
problems.

January 23, 2025 22


Recursive Functions
• How Recursion Works
In a recursive function has
• Base Case: This is the condition that terminates the
recursion. Without it, the recursion would go on indefinitely.
• Recursive Case: This is the part of the function that calls itself
with modified arguments, moving the problem towards the
base case.

January 23, 2025 23


Recursive Functions
• Structure of a Recursive Function in C
A recursive function generally has the following structure:
return_type function_name(parameters) {
// Base case: return something without calling the function again
if (base_condition) {
return value;
}
// Recursive case: call the function with modified arguments
else {
return function_name(modified_parameters);
}
}

January 23, 2025 24


Recursive Functions
• Example of a Recursive Function: Factorial
A common example of recursion is calculating the factorial of a
number. The factorial of a number n is the product of all
positive integers from 1 to n, and it can be defined as:
factorial(n)=n×factorial(n−1)
Base case: factorial(0)=1

January 23, 2025 25


Recursive Functions
#include <stdio.h>
int factorial(int n) {
// Base case
if (n == 0) {
return 1; // factorial(0) is 1
}
// Recursive case
else {
return n * factorial(n - 1); // n * factorial of n-1
}
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}

January 23, 2025 26


Recursive Functions
• How the Recursion Works in the Factorial Example:
• factorial(5) calls factorial(4)
• factorial(4) calls factorial(3)
• factorial(3) calls factorial(2)
• factorial(2) calls factorial(1)
• factorial(1) calls factorial(0)
• factorial(0) reaches the base case and returns 1.
• Then, each recursive call returns:
• factorial(1) returns 1×1=1
• factorial(2) returns 2×1=2
• factorial(3) returns 3×2=6
• factorial(4) returns 4×6=24
• factorial(5) returns 5×24=120

January 23, 2025 27


Recursive Functions
The Fibonacci series is another classic example
of recursion. The Fibonacci sequence is defined
as:
F(0)=0
F(1)=1
F(n)=F(n−1)+F(n−2) for n≥2

January 23, 2025 28


Recursive Functions
• #include <stdio.h>
• // Function to compute the nth Fibonacci number
• int fibonacci(int n) {
• // Base cases
• if (n == 0) {
• return 0; // Fibonacci(0) = 0
• }
• if (n == 1) {
• return 1; // Fibonacci(1) = 1
• }
• // Recursive case
• else {
• return fibonacci(n - 1) + fibonacci(n - 2); // Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-
2)
• }
• }

January 23, 2025 29


Recursive Functions
int main() {
int num = 6;
printf("Fibonacci number at position %d is %d\n", num,
fibonacci(num));
return 0;
}
Output:
Fibonacci number at position 6 is 8

January 23, 2025 30

You might also like