Functions in C with Examples

This Tutorial Explains Functions in C with Example(s).

What is Function in C?
A function is a block of code that performs a specific task. Functions are often used to perform similar tasks on different data sets. There are two types of functions in C, user-defined functions and library functions.

  • User-defined functions are the functions which are written by the user and included in the program.
  • Library functions are the inbuilt functions which are already present in the C library (like printf(), scanf() etc).

General Syntax of a C Function:

type function_name(formal_arguments)	/* function definition header */
{
    statements;		/*function body*/
    return statement;
}

Like variables, every function also has a type, meaning that what type of value function returns to its calling function. For example, if function returns an integer, it is of type int; if it returns a float, it is of type float; and so on. Remember that if a function returns nothing to its calling function, its type is void.

The function name should be descriptive, stating clearly what the specific task function does. The function name is followed by a pair of parenthesis, within which specify a comma separated list of arguments with their number and types in the order corresponding to arguments from its calling function, and use void if the function does not take any arguments.

The function header contains the function type, function name, and any arguments in parenthesis. The function header is immediately followed by a pair of braces containing instructions on how the function performs a specific task.

advertisement
advertisement

Let’s look at an example now.

#include <stdio.h>
void display(void);	/* function declaration */
 
int main(void)
{
    printf("main: going to call display...\n");
    display();	/* call to display() */
    return 0;
}
 
void display(void)
{
    printf("i am display(): I display massage!\n");
    return;
}

When a function returns nothing i.e. function is of type void, we can use return statement as ‘return;’ i.e. return keyword is not followed by any value.

Let’s take one more example,

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
/* sum2ints.c -- program sums up two integers */
#include <stdio.h>
int sum2ints(int, int);	/* declaration or function prototype */
 
int main(void)
{
    int u = 5, v = 10;
 
    printf("sum of %d and %d is %d\n", u, v, sum2ints(u, v));
    return 0;
}
 
/* sum2ints() sums up two integers and returns their sum to calling fun. */
int sum2ints(int x, int y) /* x, y are formal arguments */
{
    return x + y;
}

Here’s output,

sum of 5 and 10 is 15

In the above program, function sum2ints() takes two integers, sum them up and return their sum to calling function to be displayed to the user.

advertisement

Example 2:

One of the most basic function types is the mathematical function. These functions take one or more numeric arguments and perform a calculation on them, returning a numeric result. For example, the sqrt() function takes a single argument (the number to be square-rooted) and returns its square root:

#include <stdio.h>
#include <math.h>
 
int main(void)
{
    double num = 25;
    double root = sqrt(num); //root will be 5.0
 
    printf("The square root of %f is %f\n", num, root);
 
    return 0;
}

Output:
The square root of 25.000000 is 5.000000

Another common type of function in C is string function. These functions operate on null-terminated strings (arrays of characters) and usually take two arguments: the first is the string to be operated on, and the second is an integer specifying the maximum length of the resulting string (including the null terminator).

advertisement

Advantage of Functions in C:

  • Functions can be written once and then reused in other parts of the program without having to be rewritten. This makes code more reliable and easier to read and maintain.
  • Functions can make code more modular. Modular code is easier to understand and debug because it is divided into smaller, more manageable pieces.
  • Functions can promote code reuse and portability. Code that is written in a modular fashion using functions can be easily reused in other programs or ported to other platforms with minimal changes.
  • It will improve the quality and performance by reducing the amount of code that needs to be executed.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.