Use Return Statement to return value to caller - C Function

C examples for Function:Function Definition

Introduction

A function can return a value.

The following function returns an int value.

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

Return statement exits the function and return the specified value to the caller.

Demo Code

#include <stdio.h>

int getSum(int a, int b) {
  return a + b;/*from   w  ww  .j a  v a 2s .c  om*/
}


int main(void) {

   printf("%d", getSum(5, 10)); /* "15" */
}

Result

The return statement can be used in a void function to exit the function.

void dummy(void) {
  return;
}

return statement for main is optional.

int main(void) {
  return 0; /* optional */
}

Related Tutorials