C – Functions

A function in C Language is an organized block of reusable code, which avoids repeating tasks. If you want to do a task repeatedly in code, just make a function, set the task in it, and call the function multiple times whenever you need it.

Before moving further, we’ve prepared a video tutorial to understand what functions are in C:

Video: Functions in C

Video: Functions in C (Hindi)

In this lesson, we will see:

  1. How to create and call a function
  2. Create a function by separating the function definition and declaration
  3. Create a Function with a parameter
  4. Create a Function with multiple parameters

Create and call a Function (Function Declaration & Definition)

To create a function in C language, set the name of the function followed by parentheses. Here, void is the return type. Use void if the function does not return a value:

void demoFunction() {
  // write the code
}

A function in C language gets executed when it is called:

demoFunction();

Let us now see an example to create a function:

#include <stdio.h>

// Demo function
void demoFunction() {
  printf("This is a demo function.\n");
}

int main() {

  // Calling the function
  demoFunction();
  
  return 0;
}

Output

This is a demo function.

Function Declaration and Definition (separate)

In the above example, we saw how to create and call a function. We defined and declared the function as well:

void demoFunction() { // function declaration
  // function body i.e. the definition
}

As shown above, a function has two parts:

  • Function Declaration
  • Function Definition

The following is the function declaration:

void demoFunction();

The following is the function definition:

void demoFunction() {
  printf("This is a demo function.\n");
}

We can also write the above function by separating function definition and declaration. Let us see an example:

#include <stdio.h>

// Function Declaration
void demoFunction();

int main() {

  // Calling the function
  demoFunction();
  
  return 0;
}

// Function Definition
void demoFunction() {
  printf("This is a demo function.\n");
}

Output

This is a demo function.

Function Parameters

Set the parameters in a C function after the name of the function. For example:

void demoFunction(int rank) { }

To call a function with a parameter, set the value of the variable while calling, for example:

demoFunction(5)

Therefore, we have passed the rank with the value 5 while calling above.

Let us now see an example:

#include <stdio.h>

// Function with a parameter
void detailsFunc(int rank) {
  printf("Rank = %d",rank);
}

int main() {

  // Calling the function
  detailsFunc(5);
  
  return 0;
}

Output

Rank = 5

Multiple Parameters

We can also set more than one parameter in a function. For example:

void demoFunction(char player[], int rank, int points) { }

To call a function with multiple parameters, set the value of the variable while calling, for example:

demoFunction("Amit", 1, 100)

We have passed the following multiple parameters while calling above:

  • player: Amit
  • rank: 1
  • points: 100

Let us now see an example:

#include <stdio.h>

// Function with multiple parameters
void detailsFunc(char player[], int rank, int points) {
  printf("Player = %s",player);
  printf("\nRank = %d",rank);
  printf("\nPoints = %d\n\n",points);
}

int main() {

  // Calling the function
  detailsFunc("Amit", 1, 100);
  detailsFunc("David", 2, 90);
  
  return 0;
}

Output

Player = Amit
Rank = 1
Points = 100

Player = David
Rank = 2
Points = 90

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:


For Videos, Join Our YouTube Channel: Join Now


Read More:

C - User Input
C Recursion
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment