C Programming Tutorial
About Lesson

C Functions

In this tutorial, you will learn about the C Library functions and return statement with the help of examples.


Function

  • Function is a block of code which is used to performs a specific task.
  • Functions are used to provide modularity & code reusability to a program.
  • The Function is also known as procedure or subroutine in other programming languages.
  • Every C program has at least one function, which is main().
  • Dividing a complex problem into smaller chunks makes our program easy to understand and reusable.

Why do we need Functions?

  • It divides the program into separate well-defined functions so that each function can be written and tested separately.
  • Understanding, coding and testing becomes very easy as they are separated.
  • Writing functions avoids rewriting the same code over and over.
  • As the functions are divided the workload can also be divided among the programmers.

Advantages of Functions

  • It makes the program clear and easy to understand.
  • Single Functions can be tested easily for errors.
  • It saves time from typing th same function again and again.
  • It helps to modify the program easily without changing the structure of a program.
  • Function can be called multiple times as per requirement.

Types of Function

There are two types of functions in C programming:

  1. Standard Library Functions : are the functions which are Predefined in C Language.
  2. User-defined Function : are the functions which are Created by users.

Standard library functions

  • Library functions are the built-in functions in C programming.
  • Library functions are those type of functions which are already defined, compiled and stored in different header files of standard C library.
  • Users can use library functions by calling the functions directly; they don’t need to write the functions themselves.

For example :

Some common library functions in C programming are printf()scanf(), etc.

In order to use mathematical functions such as sqrt() and abs(), we need to
include the header file <math.h>.


Example : Standard Library function

#include <stdio.h>
// declaring "cmath" header file to use mathematical functions
#include <math.h>
int main() {
  int number;
  double squareRoot;
  number = 9;
  // sqrt() is a library function to calculate the square root
  squareRoot = sqrt(number);
  printf("Square root of %d is %lf ",number,squareRoot);
  return 0;
}

Output

Square root of 9 = 3.000000

In this program, the sqrt() library function is used to calculate the square root of a number.

The function declaration of sqrt() is defined in the math.h header file. That’s why we need to use the code #include <math.h> to use the sqrt() function.

Visit standard library functions in C programming to learn more.


Return Statement

The Return statement is used to terminate the execution of a function and transfer program control back to the calling function.

It can also specify a value to be returned by the function.

Syntax :

void Number() {
  // statement
}
  • This means the function is not returning any value.
  • It’s also possible to return a value from a function.
  • For this, we need to specify the returnType of the function during function declaration.
  • Then, the return statement can be used to return a value from a function.

For example,

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

Here, we have the data type int instead of void. This means that the function returns an int value.

The code return (a + b); returns the sum of the two parameters as the function value.

The return statement denotes that the function has ended. Any code after return inside the function is not executed.

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!