Browse Tutorials
Functions in C Programming

Functions in C Programming

11 Nov 2023
Intermediate
845 Views
12 min read
Learn via Video Course & by Doing Hands-on Labs

C Programming For Beginners Free Course

Functions in C Programming: An Overview

Functions are a very important programming construct. Every programming language from C to Python employs the use of functions. Understanding the concept of functions means covering a big portion of programming. In this C tutorial, we will understand functions in C and its crucial aspects like function parameters, prototypes, etc. For a more in-depth understanding, consider our C Certification Course.

What is a function in C?

A function in C is a block of code written by the programmer to perform a specific task. E.g. function to calculate the area of a square. A function executes only when it is called. It is an important aspect of code reusability and understandability. You can divide your program into as many functions as you want to solve a difficult task.

There are two types of functions in C.

  1. Standard Library Functions: Inbuilt functions in C
  2. User-defined Functions: Created by users
In this tutorial, we will look at User-defined functions in C.

Function Declaration in C

Syntax

returnType functionName (parameter1, parameter2,...) {
 // function body 
}
  • returnType signifies the type of value returned by the function. e.g. int, void.
  • functionName specifies the name of the function. You must give a function name relevant to the task the function performs. It increases understandability. e.g. area()
  • The parentheses () after functionName contains parameters. They are like values given by the programmer to be used by the function to perform its task. The () can be empty i.e. no parameters.
  • function body in the {} contains the code to perform the given task.

Example

// function declaration
void welcomeFunction() {
 printf("Welcome to ScholarHat\n");
}

Function Call in C

As mentioned above, a function executes only when it is called. When you create/declare a function, it gets saved for use in the future.

Syntax

functionName (parameter1, parameter2,...);

Example

We will call the welcomeFunction() created above in the main() function
int main() {
 
 // function call 
 welcomeFunction(); 

}
  • You can even call a function as many times as you want.

    Example

    
    #include <stdio.h>
    
    void welcomeFunction() {
     printf("Welcome to ScholarHat\n");
    }
    
    int main() {
     // Function calls
     welcomeFunction();
     welcomeFunction();
     welcomeFunction();
    
     return 0;
    }
     

    Output

    Welcome to ScholarHat
    Welcome to ScholarHat
    Welcome to ScholarHat
    

Function Definition in C

The body of the function is known as the function definition.

Example

// function declaration
void welcomeFunction() {
 printf("Welcome to ScholarHat\n"); //function definition}

Function Parameters in C

As said above, parameters are the values given by the programmer during function declaration to be used by the function to perform its task. They are variables of the function specified in parentheses ().

Syntax

returnType functionName (parameter1, parameter2,...) {
 // function body 
}

There can be as many parameters in the () as you want in the function declaration. They must be separated by commas ,. The values of the function parameters are passed during a function call.

  • The parameters in the function call are known as arguments or actual parameters.
  • The parameters in the function declaration are known as parameters or formal parameters

Example


#include <stdio.h>

void welcomeFunction(const char *value){
 printf("Welcome \t to \t %s\n", value);
}

int main() {
 // Function calls
 welcomeFunction("ScholarHat");
 welcomeFunction("ScholarHat");
 welcomeFunction("ScholarHat");

 return 0;
}
 

In the above C code, the welcomeFunction() takes a string parameter. In the main() function, we have called the welcomeFunction() three times with the string parameter value or argumentScholarHat. Therefore the function executes three times.

Output

Welcome to ScholarHat
Welcome to ScholarHat
Welcome to ScholarHat

Remember: The arguments in the function call must always match with the corresponding parameters in the function declaration.

Function Prototype in C

We saw the function declaration and function definition above. Did you notice that the user-defined function is always defined above the main() function? In other words, the user-defined function is always defined above the function call. What if we create our function after the function call?

In C++, if a user-defined function, such as welcomeFunction() is declared after the function call, an error will occur. Therefore, we always have to declare the function in C above the function call. But, we can separate the function declaration and function definition in C. Here comes the role of thefunction prototype in C.

Function Prototype in C is just the function declaration without the function definition above the function call.

Syntax

returnType functionName(dataType1, dataType2, ...);

Example


#include <stdio.h>

void welcomeFunction(const char *value); //function prototype

int main() {
 // Function call above function declaration
 welcomeFunction("ScholarHat");
 return 0;
}

// Function definition
void welcomeFunction(const char *value) {
 printf("Welcome \t to \t %s\n", value);
}

Here, the function prototype, void welcomeFunction(string); informs the compiler about the function name, welcomeFunction(), and its parameter of type string, value. Therefore, we can call the welcomeFunction() from the main() function before its definition.

Output

Welcome to ScholarHat

return statement in C

You all might be familiar with the return statement in C as you have already read in the Jump Statements in C tutorial.

We saw returnType in the function declaration syntax. We read that it signifies the type of the value returned by the function. In the above examples we have given the return type as void which means the function doesn't return any value.

If you want your function to return a value, you must mention the required returnType in the function declaration and use the return statement in the function definition. The return keyword denotes that the function has ended. Any code after return inside the function is not executed.

Example


#include <stdio.h>
int returnFunction(int a, int b) {
 return a + b;
}

int main() {
 // Function call
 printf("%d", returnFunction(20, 40));
 return 0;
}

In the above C code, the returnFunction() adds the values of a and b and returns it to the main() function from where it is called. As the returnFunction() returns an integer value the returnType of the function is int.

Output

60
Summary
We saw in this blog post almost all the aspects of functions in C with examples. Now, you need to do hands-on to familiarize yourselves with such an important programming construct. For practicals and hands-on experience, you can consider our C Certification program.
Share Article
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Accept cookies & close this