FUNCTIONS
Function
• A function is a block of reusable
code designed to perform a
specific task
• There’s 2 types of functions in c.
• 1) Built-in function
• 2) User define function
User define has 2 parts in it.
i) With return
ii) Without return
Function
• Benefits:
• Code reusability
• Improved Readability and
Maintainability,
• Reduced Redundancy
Function
• Built-in functions:
• built-in functions (also known as
library functions) are pre-defined
functions provided by the C
Standard Library.
• These functions perform specific,
commonly-needed operations,
such as handling input and output,
manipulating strings
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
printf("Length: %lun", strlen(str));
printf("First character: %cn", str[0]);
return 0;
}
Function
• User define functions:
• User-defined function is a function
created by the programmer to
perform a specific task.
• Unlike built-in functions, user-
defined functions are custom
functions
// Function declaration
int add(int a, int b);
int main()
{
int result = add(5, 3); // Function call
printf("Result: %d", result);
return 0;
}
// Function definition
int add(int a, int b)
{
return a + b;
}
Function
• User define functions:
• With return:
• functions perform a task and return
a result to the caller.
• type specifies the type of value the
function will return
• In this example “add()” return an
int value which is sum of a and b
int add(int a, int b)
{
return a + b;
}
int main()
{
int result = add(3, 4);
printf("Sum: %d", result);
return 0;
}
Function
• User define functions:
• With out return:
• perform a task but do not return
any value
• Return type for such functions is
void, indicating that no value
returned.
void greet() {
printf("Hello, World!n");
}
int main() {
greet(); // Calls greet() to print the
message
return 0;
}
Function
A) Write a C Program that
defines a function to add
first n numbers.
#include <stdio.h>
int addFirstN(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum += i;
}
return sum;
}
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Sum of first %d numbers: %dn", n,
addFirstN(n));
return 0;
}
Function
B) Write a function in the
program to return 1 if
number is prime
otherwise return 0.
#include <stdio.h>
int isPrime(int num) {
if (num <= 1) return 0;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return 0;
}
return 1;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPrime(num))
printf("%d is a prime number.n", num);
else
printf("%d is not a prime number.n", num);
return 0;
}
Function
C) Program to Find
Factorial of a Number
Using Recursion.
#include <stdio.h>
int factorial(int n) {
if (n <= 1) return 1; // Base case
return n * factorial(n - 1); // Recursive call
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is: %dn", num,
factorial(num));
return 0;
}
Function
D) Program Using Global
Variable and Static
Variable.
#include <stdio.h>
int globalCounter = 0; // Global variable
void increment() {
static int staticCounter = 0; // Static variable
globalCounter++;
staticCounter++;
printf("Global Counter: %d, Static Counter:
%dn", globalCounter, staticCounter);
}
int main() {
for (int i = 0; i < 3; i++) {
increment();
}
return 0;
}
Thank you

C Programming: Built-in & User-defined Functions(With Example)

  • 1.
  • 2.
    Function • A functionis a block of reusable code designed to perform a specific task • There’s 2 types of functions in c. • 1) Built-in function • 2) User define function User define has 2 parts in it. i) With return ii) Without return
  • 3.
    Function • Benefits: • Codereusability • Improved Readability and Maintainability, • Reduced Redundancy
  • 4.
    Function • Built-in functions: •built-in functions (also known as library functions) are pre-defined functions provided by the C Standard Library. • These functions perform specific, commonly-needed operations, such as handling input and output, manipulating strings #include <stdio.h> #include <string.h> int main() { char str[] = "Hello, World!"; printf("Length: %lun", strlen(str)); printf("First character: %cn", str[0]); return 0; }
  • 5.
    Function • User definefunctions: • User-defined function is a function created by the programmer to perform a specific task. • Unlike built-in functions, user- defined functions are custom functions // Function declaration int add(int a, int b); int main() { int result = add(5, 3); // Function call printf("Result: %d", result); return 0; } // Function definition int add(int a, int b) { return a + b; }
  • 6.
    Function • User definefunctions: • With return: • functions perform a task and return a result to the caller. • type specifies the type of value the function will return • In this example “add()” return an int value which is sum of a and b int add(int a, int b) { return a + b; } int main() { int result = add(3, 4); printf("Sum: %d", result); return 0; }
  • 7.
    Function • User definefunctions: • With out return: • perform a task but do not return any value • Return type for such functions is void, indicating that no value returned. void greet() { printf("Hello, World!n"); } int main() { greet(); // Calls greet() to print the message return 0; }
  • 8.
    Function A) Write aC Program that defines a function to add first n numbers. #include <stdio.h> int addFirstN(int n) { int sum = 0; for (int i = 1; i <= n; i++) { sum += i; } return sum; } int main() { int n; printf("Enter a number: "); scanf("%d", &n); printf("Sum of first %d numbers: %dn", n, addFirstN(n)); return 0; }
  • 9.
    Function B) Write afunction in the program to return 1 if number is prime otherwise return 0. #include <stdio.h> int isPrime(int num) { if (num <= 1) return 0; for (int i = 2; i * i <= num; i++) { if (num % i == 0) return 0; } return 1; } int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (isPrime(num)) printf("%d is a prime number.n", num); else printf("%d is not a prime number.n", num); return 0; }
  • 10.
    Function C) Program toFind Factorial of a Number Using Recursion. #include <stdio.h> int factorial(int n) { if (n <= 1) return 1; // Base case return n * factorial(n - 1); // Recursive call } int main() { int num; printf("Enter a number: "); scanf("%d", &num); printf("Factorial of %d is: %dn", num, factorial(num)); return 0; }
  • 11.
    Function D) Program UsingGlobal Variable and Static Variable. #include <stdio.h> int globalCounter = 0; // Global variable void increment() { static int staticCounter = 0; // Static variable globalCounter++; staticCounter++; printf("Global Counter: %d, Static Counter: %dn", globalCounter, staticCounter); } int main() { for (int i = 0; i < 3; i++) { increment(); } return 0; }
  • 12.