B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP

C Functions

User-defined Functions: Functions in C are blocks of code designed to perform a particular task. User-defined functions help organize and reuse code by breaking programs into smaller modules.

यूज़र-डिफाइंड फंक्शन्स: C में फंक्शन कोड का एक ब्लॉक होता है जो किसी खास काम को करता है। ये छोटे-छोटे हिस्से बनाकर कोड को साफ और दोबारा इस्तेमाल करने लायक बनाता है।

Types of Functions: Built-in functions (like printf(), scanf()) and User-defined functions (created by programmers).

फंक्शन के प्रकार: बिल्ट-इन फंक्शन (जैसे printf(), scanf()) और यूज़र-डिफाइंड फंक्शन्स (जो हम खुद बनाते हैं)।

Return Types: Functions can return different data types like int, float, char, or void (no return).

रिटर्न टाइप: फंक्शन अलग-अलग डेटा टाइप रिटर्न कर सकते हैं जैसे int, float, char, या void (कोई रिटर्न नहीं)।

1. void sum() - No parameters, no return
#include <stdio.h>

void sum() {
  int a = 5, b = 10;
  printf("Sum = %d\n", a + b);
}

int main() {
  sum();
  return 0;
}

Output: Sum = 15

This function prints sum of 5 and 10. No input parameters, no return.

यह फंक्शन 5 और 10 का योग प्रिंट करता है। इसमें कोई इनपुट पैरामीटर या रिटर्न नहीं है।

2. void sum(int a, int b) - Parameters, no return
#include <stdio.h>

void sum(int a, int b) {
  printf("Sum = %d\n", a + b);
}

int main() {
  sum(7, 8);
  return 0;
}

Output: Sum = 15

Function takes two inputs and prints their sum, but does not return any value.

फंक्शन दो मान लेता है और उनका योग प्रिंट करता है, लेकिन कोई मान रिटर्न नहीं करता।

3. int sum() - No parameters, returns int
#include <stdio.h>

int sum() {
  int a = 3, b = 4;
  return a + b;
}

int main() {
  int result = sum();
  printf("Sum = %d\n", result);
  return 0;
}

Output: Sum = 7

Function returns the sum of two fixed numbers.

फंक्शन दो तयशुदा नंबरों का योग रिटर्न करता है।

4. int sum(int a, int b) - Parameters and returns int
#include <stdio.h>

int sum(int a, int b) {
  return a + b;
}

int main() {
  int result = sum(12, 13);
  printf("Sum = %d\n", result);
  return 0;
}

Output: Sum = 25

Function adds two inputs and returns the sum.

फंक्शन दो इनपुट को जोड़कर योग रिटर्न करता है।

5. Function to calculate percentage (no params, returns float)
#include <stdio.h>

float calculatePercentage() {
  int total = 500;
  int obtained = 425;
  return (obtained * 100.0) / total;
}

int main() {
  float percent = calculatePercentage();
  printf("Percentage = %.2f%%\n", percent);
  return 0;
}

Output: Percentage = 85.00%

Calculates percentage of obtained marks out of total.

कुल नंबरों में से प्राप्त नंबरों का प्रतिशत निकालता है।

6. Function with parameters and return float (percentage calculation)
#include <stdio.h>

float calculatePercentage(int total, int obtained) {
  return (obtained * 100.0) / total;
}

int main() {
  int total, obtained;
  printf("Enter total marks: ");
  scanf("%d", &total);
  printf("Enter obtained marks: ");
  scanf("%d", &obtained);

  float percent = calculatePercentage(total, obtained);
  printf("Percentage = %.2f%%\n", percent);
  return 0;
}

Output (example input):

Enter total marks: 600
Enter obtained marks: 480
Percentage = 80.00%

Calculates percentage based on user input using function with parameters.

यूज़र इनपुट लेकर प्रतिशत निकालता है, जिसमें पैरामीटर वाले फंक्शन का उपयोग होता है।

7. void function with scanf()
#include <stdio.h>

void greetUser() {
  char name[50];
  printf("Enter your name: ");
  scanf("%49s", name);
  printf("Hello, %s!\n", name);
}

int main() {
  greetUser();
  return 0;
}

Output (example input):

Enter your name: Aryan
Hello, Aryan!

Function asks for user input and greets by name.

फंक्शन यूज़र से नाम पूछता है और उसे ग्रीट करता है।

8. Function returning character
#include <stdio.h>

char getGrade(int marks) {
  if (marks >= 90) return 'A';
  else if (marks >= 75) return 'B';
  else if (marks >= 60) return 'C';
  else return 'F';
}

int main() {
  int score = 85;
  char grade = getGrade(score);
  printf("Grade: %c\n", grade);
  return 0;
}

Output: Grade: B

Returns grade based on marks.

नंबरों के आधार पर ग्रेड देता है।

9. Recursive function to calculate factorial
#include <stdio.h>

int factorial(int n) {
  if (n == 0) return 1;
  else return n * factorial(n - 1);
}

int main() {
  int num = 5;
  printf("Factorial of %d is %d\n", num, factorial(num));
  return 0;
}

Output: Factorial of 5 is 120

Calculates factorial recursively.

फैक्टोरियल को रिकर्सिव तरीके से निकालता है।

10. Function with default parameter behavior (simulate)
#include <stdio.h>

int multiply(int a, int b) {
  return a * b;
}

int multiplyDefault(int a) {
  return multiply(a, 2); // default b=2
}

int main() {
  printf("Multiply 5 * 3 = %d\n", multiply(5, 3));
  printf("Multiply 5 * default(2) = %d\n", multiplyDefault(5));
  return 0;
}

Output:

Multiply 5 * 3 = 15
Multiply 5 * default(2) = 10

Simulates default parameter by calling function with fixed value.

डिफॉल्ट पैरामीटर जैसा व्यवहार दिखाने के लिए फिक्स्ड वैल्यू के साथ कॉल करता है।