Functions in C programming language are building blocks of a C program. A function is a sequence of statements to perform a specific task and it can be called by other functions. A function may take some input and perform a logical unit of task and may return the response to calling function.
The function in C language is also known as method, subroutine or procedure.
- A C program is a set of function calling each other.
- We can use any number of functions in a C program.
- All C programs must contains at least one main() function.
How Functions work in C
- First of all main() function of C program gets called by Operating system.
- Execution of C program begins. Program's statements and expressions getting executed in top to bottom sequence.
- When control reaches a function call lets say myFunction(int val); it pauses the execution of current function and control goes inside the called function myFunction.
- Once execution of code inside myFunction body finishes control comes back to the calling function. It resumes the execution of calling function at the next statement following the function call of myFunction.
- At the point of any function call, control keeps on jumping between calling function and called function.
- C program terminates when execution of main function ends.
Advantage of Functions in C Programming
- A large C program can divide into set of functions, where each function owns the responsibility of performing one specific task.
- Splitting a C program into functions, makes it easy to maintain and improves understandability of very large C programs.
- We can call a function any number of times in a program from any place which avoid rewriting same code again and again at various program.
- We can add common utility functions in header files, So that It can be reused by other programs.
Function Declaration in C
A function declaration in C tells the compiler about function name, function parameters and return value of a function. The actual body of the function can be defined separately.
Like variable in C, we have to declare functions before their first use in program.
Syntax of Function Declaration
- Declaration of a function with no input parameters
void printMatrix(); - Declaration of a function with one input parameter and integer return type
int isEvenNumber(int num); - Declaration of a function with multiple input parameter and integer return type.
int getSum(int num1, int num2); - Declaration of a function with no input parameters and integer return type.
int getRandomNumber();
- Function declaration in C always ends with a semicolon.
- By default the return type of a function is integer(int) data type.
- Function declaration is also known as function prototype.
- Name of parameters are not compulsory in function declaration only their type is required. Hence following declaration is also valid.
int getSum(int, int); - If function definition is written before main function then function declaration is not required whereas, If function definition is written after main function then we must write function declaration before main function.
C program to show function definition before main
#include<stdio.h> /* * Function Definition before main * In this case function declaration is not required */ int getSum(int a, int b) { return a+b; } int main(){ int a, b, sum; printf("Enter two numbers\n"); scanf("%d %d", &a, &b); /* Calling getSum function */ sum = getSum(a, b); printf("SUM = %d", sum); return 0; }
Output
Enter two numbers 2 6 SUM = 8
C program to show function definition after main
#include<stdio.h> /* Function Definition after main In this case function declaration is required */ int getSum(int a, int b); int main(){ int a, b, sum; printf("Enter two numbers\n"); scanf("%d %d", &a, &b); /* Calling getSum function */ sum = getSum(a, b); printf("SUM = %d", sum); return 0; } int getSum(int a, int b) { return a+b; }
Output
Enter two numbers 6 7 SUM = 13
Function Definition in C
A function definition in C programming language consists of function name, function parameters, return value and function's body.
- First line is called as Function Header and it should be identical to function Declaration/Prototype except semicolon.
- Name of arguments are compulsory here unlike function declaration.
- Function's definition contains code to be executed when this function is called.
Syntax of Function Definition
body of a function
return (expression);
}
- return_type : The return_type is the data type of the value returned by the function. void data type is used when the function does not return any value. By default the return type of a function is integer(int).
- function_name : This is a C identifies representing the name of the function. Every function must have a unique name in a C program. Name of a function is used to call a function.
- type arg1, type arg2 ..... : It is a comma-separated list of data types and names of parameters passed by the calling function. The parameter list contains data type, order, and number of the parameters expected by a function at the time of calling it. Parameters field is optional, we can skip it if a function don't require any data from calling function.
- body of a function : The body of a function contains a set of statements that will be executed when this function is called. It may define it's own local variables and call other functions.
- return(expression) : This is used to send a value to calling function before termination of function. If the return type of function is void, you need not use this line. When control reaches return statement, it halts the execution of function immediately and returns the value of expression to calling function.
C Program to show Function Definition
#include<stdio.h> /* Function Defination */ int getAreaOfSquare(int side){ /* local variable declaration */ int area; area = side*side; /* Return statement */ return area; } int main(){ int side, area; printf("Enter side of square\n"); scanf("%d", &side); /* Calling getAreaOfSquare function */ area = getAreaOfSquare(side); printf("Area of Square = %d", area); return 0; }Above program finds the area of square using getAreaOfSquare function.
Output
Enter side of square 5 Area of Square = 25
Functions in Details
Click on the links below to check detailed description of functions in C programming.- All C programs must contain a main() function.
- The operating system calls main function of a program from where program's execution starts.
- One function can call another function.
- C functions can be called with or without arguments depending on function declaration.
- We can create any number of functions in a C program.
- C Functions can be invoked from anywhere within a C program.
- We can call a function any number of times in a C program.
- Function name should be unique in a C program.
- A function declaration in C tells the compiler about function name, function parameters and return value of a function.
- C function can return only one value to the calling function.
- C functions may or may not return values to calling functions depending on the return type defined in function declaration. Void data type is used when the function does not return any value. By default the return type of a function is integer(int).
- There are two types of functions in C, library functions and User defined functions.
- When a function terminates, program control is returned to the calling function from where it is called.
- Execution of a C program comes to an end when there is no functions or statements left to execute in main function body.