A function in a program contains a block of statements that is performing some particular task. Before we discuss more about the functions,
let's see how to -
Declare a function.
Define a function.
Call a function.
How a function is declared?
Before a function is called, we must declare it. Declaration of a function starts with the declaring its prototype. Declaring
the prototype of a function involves defining the following elements in the following order -
The return type of a function i.e. the type of value returned by a function.
The name of the function.
A pair of empty parenthesis(). followed by a semicolon;
The empty parenthesis signifies that we are not going to pass any value to function when it is called.
Let's see an example in which we are declaring a function named welcome() and this function has a void return type,
which means that this function
is not going to return any value. and this function will not be passed any value when it is called, hence it has empty parenthesis() and no type is declared in them.
Note : By default, every function in C language returns an int type of value.
How a function is defined?
Post the declaration of a function, we must define it before it is called. Definition of a function constitutes defining the work done by the function.
Definition of a function starts with the following elements in the following order -
The return type of a function i.e. the type of value returned by a function.
The name of the function.
A pair of parenthesis()
An opening brace{
A single or multiple block of statements contained in the function.
Finally, a closing brace}
Let's continue with our same example and define the function named welcome(which we have declared in the method declaration segment).
This function was declared with a void return type, hence, we will define this method with a void return type
and this function will print a welcome message to the user.
void welcome() /*function is defined*/
{
printf("Welcome to the program! \n");
}
Advertisement
How a function is called?
Once a function is declared and defined then we can easily call it by just using its name, followed by the pair of parenthesis() and a semicolon;
welcome(); /*function is called*/
Example of a function in C
Let's suppose a program has to display a welcome message whenever it is executed following by doing its routine work.
In order to display this welcome message, we could create a separate function in this program called welcome, which will be called
at the beginning of the program and display the welcome message to the user. Let's see how.
/* Function in C */
#include<stdio.h>
int main()
{
int welcome(); /*function prototype declaration*/
welcome(); /*function is called*/
int a,b;
a=2;
b=3;
int result = a + b;
printf("Result of 2 + 3 is : %d",result);
return 0;
}
int welcome() /*function is defined*/
{
printf("Welcome to the program! \n");
return 0;
}
Output-
Welcome to the program!
Result of 2 + 3 is : 5
Calling multiple functions in C
Let's suppose a program has two functions, one is called at the beginning of the program to notify that program has started and the other
one is called to notify when the program has finished its routine work.
Let's see how we do this.
/* Calling multiple functions in C */
#include<stdio.h>
int main()
{
void welcome(); /*function prototype declaration*/
void goodbye(); /*function prototype declaration*/
welcome(); /* welcome() function is called */
int a,b;
a=2;
b=3;
int result = a * b;
printf("Result of 2 x 3 is : %d \n",result);
goodbye(); /* goodbye() function is called */
return 0;
}
void welcome() /*welcome() function is defined*/
{
printf("Welcome to the program! \n");
}
void goodbye() /*goodbye() function is defined*/
{
printf("The program is going to end. Goodbye!");
}
Output-
Welcome to the program!
Result of 2 x 3 is : 6
The program is going to end. Goodbye!
We could even pass values to a function when it is called. For doing this, we will have to define the type of value that will be passed to a function(when it is called)
within the parenthesis() when declaring its prototype. For more on this, please continue with our next article.