Functions in C

Class12 NEB -Computer Science Exam for Computer Science

Posted by yanib on 2025-03-16 08:45:53 |

Share: Facebook | Twitter | Whatsapp | Linkedin Visits: 186


Functions in C

A function is a self-contained block of code that performs a specific task. It helps to break a program into smaller, manageable parts. Functions are used to perform repetitive tasks, improve code organization, and make the program easier to understand and maintain.

Classification of C Functions

  1. User-Defined Functions: These are functions that are defined by the user to perform a specific task.
  2. Library Functions: These are predefined functions that are already written, tested, and included in C libraries (like printf(), scanf()).

Types of Functions in C

1. Function with No Arguments and No Return Value

A function that doesn't take any input (arguments) and doesn't return anything.

c
#include <stdio.h> void addition() { int a, b, sum; printf("Enter two numbers: "); scanf("%d %d", &a, &b); sum = a + b; printf("\nThe sum is: %d", sum); } int main() { addition(); return 0; }

Output:

python
Enter two numbers: 45 90 The sum is: 135

2. Function with Arguments but No Return Value

This function takes arguments (input) but does not return any value.

c
#include <stdio.h> void addition(int a, int b) { int sum = a + b; printf("The sum is: %d", sum); } int main() { int a, b; printf("Enter two numbers: "); scanf("%d %d", &a, &b); addition(a, b); return 0; }

Output:

python
Enter two numbers: 50 40 The sum is: 90

3. Function with Arguments and Return Value

This function accepts arguments and returns a value.

c
#include <stdio.h> int addition(int a, int b) { int sum = a + b; return sum; } int main() { int a, b, sum; printf("Enter two numbers: "); scanf("%d %d", &a, &b); sum = addition(a, b); printf("The sum is: %d", sum); return 0; }

Output:

python
Enter two numbers: 78 56 The sum is: 134

Different Types of Function Calls

1. Function Call by Value (Pass Arguments by Value)

In this method, the actual value of the arguments is passed to the function.

c
#include <stdio.h> void swap(int x, int y) { int temp; temp = x; x = y; y = temp; printf("The values within the function are: %d %d\n", x, y); } int main() { int a = 99, b = 89; printf("Before function call, a and b are: %d %d\n", a, b); swap(a, b); printf("After function call, a and b are: %d %d\n", a, b); return 0; }

Output:

sql
Before function call, a and b are: 99 89 The values within the function are: 89 99 After function call, a and b are: 99 89

In call by value, the changes made inside the function do not affect the original variables.


2. Function Call by Reference (Pass Arguments by Address)

In this method, the address of the argument (using pointers) is passed, so changes made inside the function will affect the original variable.

c
#include <stdio.h> void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } int main() { int a = 99, b = 89; printf("Before swap, a and b are: %d %d\n", a, b); swap(&a, &b); printf("After swap, a and b are: %d %d\n", a, b); return 0; }

Output:

less
Before swap, a and b are: 99 89 After swap, a and b are: 89 99

In call by reference, the changes made to the variables inside the function reflect in the calling function as well because we are passing their addresses.


Recursive Function

A recursive function is a function that calls itself. Recursion is typically used to solve problems that can be broken down into smaller, similar problems.

Here’s an example of a recursive function to generate the Fibonacci series:

c
#include <stdio.h> int fib(int n) { if (n == 1) return 0; else if (n == 2) return 1; else return(fib(n-1) + fib(n-2)); } int main() { int terms, i; printf("How many terms do we need? "); scanf("%d", &terms); for (i = 1; i <= terms; i++) { printf("%d ", fib(i)); } return 0; }

Output:

arduino
How many terms do we need? 10 0 1 1 2 3 5 8 13 21 34

In this program, the Fibonacci series is calculated using a recursive function (fib), which calls itself with smaller values of n until the base case is reached.


Summary of Key Points

  • Functions are blocks of code that perform specific tasks.
  • User-Defined Functions allow programmers to create custom functions with specific names, arguments, and return values.
  • Library Functions are predefined functions in C (e.g., printf(), scanf()).
  • Functions can have no arguments, arguments but no return value, or arguments and return values.
  • Function Call by Value passes a copy of the argument's value.
  • Function Call by Reference passes the address of the argument, allowing the function to modify the original value.
  • Recursive Functions call themselves to break down tasks into smaller sub-tasks.

I hope this helps! Let me know if you need further clarifications or more examples!

Leave a Comment:
Lalyang Yuba Club
at 2025-03-17 23:13:16
Thank you for very useful post.