Introduction to C Programming
Operators in C Part 1
Operators in C Part 2
Precedence and Associativity Of Operators in C
If else Statements in C
Switch Statement in C
Loops in C
While and Do-While loops in C
Nested loops in C
Loop Control Statements in C
1D Arrays in C
2D Arrays in C
Pattern Printing in 2D arrays
Functions in C
Pointers in C
ASCII Encoding and Typecasting in C
Functions in C
A function is a reusable piece of code that can be used any number of times in the program.
By using a function, we do not have to write the same code again and again. Instead, we just use
the function. For example, you have a lengthy calculation that you have to perform various times
in your program. Functions reduce the overhead to a great extent!
Advantages of using functions -
- Makes the code reusable.
- Dividing the program into functions increases the readability.
- Makes it easy for the programmer. No need to write the same code again and again.
- Makes the program short.
- Makes the program clean and easy to debug.
Like in real life, we perform many functions such as doing homework, reading a book
or watching movies etc. In programming, we can make anything a function. For example -
a function for adding two numbers or just printing Hello World on the screen. We can make
a function as simple or as complicated as we want.
- Function Declaration
- Function Definition
- Calling of a function
Jump to specific sections
Function Declaration
Functions are declared outside the main function. The function declaration includes -
- Return type- The type of value returned by the function i.e. int, float, etc. If the function does not return a value, the return type should be void. Note that a function can return only one value.
- Function Name- It is the name we give to our function so that we can use it later. Like we give name to our variables. A function name must be a valid identifier and not a reserved keyword like void, continue, etc.
- Parameters- It refers to the variables we pass to the function during function call so that they can be used by the function. You can pass zero or more parameters to a function.
The syntax of function declaration is as follows :
return_type function_name(parameters);
For example -
#include <stdio.h>
void hello();
int add(int a, int b);
void main()
{
// Statements
}
hello() is a function that takes no parameters and has a return type void i.e
it does not return anything.
The add() function takes two integers a and b as parameters and also returns an integer value.
Note that function definition is optional and you can skip it.
Note that function definition is optional and you can skip it.
Function definition
It refers to the body of the function. It consists of the statements written inside a function.
For example -
For example -
#include <stdio.h>
void hello()
{
printf("Hello from inside a function !");
}
int add(int a, int b)
{
int sum = a + b;
printf("Sum calculated in a function");
return sum;
}
void main()
{
// Statements
}
Here, we have defined the hello() function which prints a hello function and does not
return any value. The add function adds two numbers. The return statement is used to return
the sum to the calling function.
Calling of a function
We have already defined two functions but on running the above program, we do not see
any output. This is because we haven't called the functions yet. Calling a function
means using it or invoking it. After calling a function, you can see its output or access
the value returned by it. Calling a function is done inside the main() function. Let's
see an example.
#include <stdio.h>
void hello()
{
printf("Hello from inside a function ! \n");
}
int add(int a, int b)
{
int sum = a + b;
printf("Sum calculated in a function \n");
return sum;
}
void main()
{
printf("Calling the hello function \n");
hello();
printf("\n");
printf("Calling the add function \n");
int result = add(4,5);
printf("Sum of 4 and 5 = %d",result);
}
Output
Calling the hello function
Hello from inside a function !
Calling the add function
Sum calculated in a function
Sum of 4 and 5 = 9
When the hello() function is called, the statements inside hello() function are executed.
Then the program returns to the main() function where it calls the add() function. The add() function takes two numbers
4 and 5 as a and b and calculates their sum. After that, it returns the sum to the
main() function. The main() function gets this sum by storing it in a result variable.
Finally, it prints the result.