Functions:

  • In the C programming language, functions are blocks of code that achieve specific tasks or operations. Functions allow you to break down a program into smaller, more manageable parts, making the code more modular and easier to maintain.
  • When we need to do that task, we call it in main function because we know that in our C++ code, prosecution starts from main function and ends when main function ends.


Main Function:

functions.c
int main() {
  printf("Hi there!");
  return 0;
}

Declaring a Function:

return_type funcName ( params ) {...}

In this example;

  • returnType shows the data type of value, function is returning.
  • funcName is the name of function.
  • (params) are the parameters which we pass to function
  • Body of the function is written inside curly braces.

Defining a void Function:

Void functions do not return anything. If we want to do calculation or print the results etc, we use function which do not return anything.

functions.c
void print(){
  printf("Hi there!");
}

Calling a void Function inside main function:

To see output of a function, we need to call it in our main function. To call a function in main function, we write the name of the function, followed by parenthesis.

functions.c
void print() {
  printf("Hi there!");
}
  
int main() {
  print(); 
  return 0;
}

Functions which return a value:

We can have functions which return some value. We can store this value in a variable or we can directly print it on screen.

functions.c
int print() {
  int num1 = 3, num2 = 2;
  return num1 + num2;
}
  
int main() {
  int num = print();
  printf("%d", num); 
  return 0;
}

Function Parameters and Arguments:

  • Function parameters are variables, which we pass to function to perform an operation.
  • Arguments are the data values which we pass in parenthesis when we call a function.

functions.c
int print(int a, int b) {
  return a + b;
}
  
int main() {
  int num = print(2, 4);
  printf("%d", num); 
  return 0;
}

Passing Arguments by Reference:

Passing agruments by reference means that we will not pass variable's values. We will pass reference to variabes. When we want top pass values by reference, we use pointers.

functions.c
#include<stdio.h>

int sum(int *a, int *b){
  int sum = *a+*b;
  return sum;
}
int main(){
  int num1=2, num2=3;
  //calling function with arguments 2 and 3.
  int add = sum( &num1, &num2 );
  printf("%d", add);
  return 0;
}

Passing Arrays as Arguments:

We can also pass an array as an argument to a function.

functions.c
#include<stdio.h>

void printMarks(int marks[5]){
  for(int i=0; i<5; i++){
    printf("%d", marks[i]);
  }
}
int main(){
  int marks[5] = {10,20,30,40,50};
  printMarks(marks);
  return 0;
}

Recursion:

Recursion means calling the function within the same function until the provided condition is true. This is done to break the one complicated problem into many simpler problems and then solving the simpler problems.

functions.c
#include <stdio.h>

// Recursive function to calculate factorial
int factorial(int n) {
  // Base case: if n is 0 or 1, factorial is 1
  if (n == 0 || n == 1) {
    return 1;
  } 
  else {
    // Recursive case: n! = n * (n-1)!
    return n * factorial(n - 1);
    }
}

int main() {
  int num;
  printf("Enter a positive integer : ");
  scanf("%d", &num);

  if (num < 0) {
      printf("Number is negative.\n")
  } else {
      int result = factorial(num);
      printf("Factorial of %d is %d", num, result);
  }

  return 0;
}

Function Overloading:

Function overloading allows us to define different functions with the same name but,
  • Overloading functions must have parameters with different data types, OR
  • Number of parameters must be different, OR
  • If number of parameters is same, their sequence must be different.
Return type of overloading functions may or may not be different.
Funtions with atleast one feature given above or having all the three features are said to be overloaded. When function is called, compiler determines which version of function to invoke.


functions.c
#include <stdio.h>

int sum(int a, int b){
  return a+b;
}
float sum(float a, float b, int c){
  return a+b;
}
int main(){
  //funtion with data type int is invoked
  int intSum = sum(5, 10);  
  printf("Integer sum = %d", intSum);
  //funtion with data type float is invoked
  float floatSum = sum(5.0, 10.0, 20);  
  printf("Float sum = %f", floatSum);
  //sum has all three features of overloading
}