We have learned about function and its properties. In this tutorial, we will learn how to create user-defined functions and how to use them in C Programming with the help of examples.
- How to create a user defined function?
- How to call a function?
- Function Declaration
- When do you need a function declaration?
- Function Definition
- Function Call
- Points to Remember
- Learn more about function
Quick links
A function is a group of statements (a block or piece of code) wrapped with curly braces {--} that together perform a specific task is called function.
C allows you to define functions according to your need. These functions are known as user-defined functions. For example: Suppose, you want to find sum of two integers, you need to define a function for adding two integers. The function name should be meaningful like sum(); or add();
Syntax for creating user defined function:
return_type function_name(Arguments list);
{
// Code or group of statements to be executed
}
Now let's see the below example, how to create a user defined function?
/* Example of user defined function or function definition*/
int sum(int num1, int num2)
{
int s;
s = num1 + num2;
return s;
}
Above example is a definition of a function, to use this function we need to call it by passing values as an arguments with the function name.
Let's have a look at the below example how to call a function?
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter two numbers:\n"); // Taking input of two numbers by user
scanf("%d,%d", &a, &b);
c = sum(a, b); // function call by passing value
prinf("Sum is %d", c);
}
Let's re-write the above program in a singal file:
/* Program two add two integers input given by user */
#include <stdio.h>
#include <conio.h>
/*Function declaration OR function prototype*/
int sum(int a, int b);
void main()
{
int a, b, sum;
clrscr();
printf("Enter two numbers: ");
scanf("%d, %d\n", &a, &b);
/* Function call by passing value */
sum = sum(a, b);
printf("sum is %d", sum);
getch();
}
/* Creating user defined function OR function definition*/
int sum(int n1, int n2)
{
int s;
s = n1 +n2;
return s;
}
Output
Enter two numbers: 10, 20
sum is 30
Function Declaration:
A function declaration tell the compiler about the function's name, function parameters, and return type. It doesn't contain function body. Function declaration is also known as function prototype or function signature.
A function declaration gives information to the compiler that the function may later be used in the program.
Syntax of function declaration:
return_type function_name(Arguments list);
For Example:
This statement taken from the above example written just before the void main();
/* Function declaration or function prototype */
int sum(int a, int b);
Since this function is adding two integer therefore, It should have some meaningful name like sum(), need two integer variables like int a and int b, and the result of two integers will be an integer. Hence, Function should return a value. Therefore, its return type should be an int as you can see in the above example. But do remember It doesn't contain function body.
Note: While providing argument list during function declaration, you can avoid the argument name, For Example: int sum (int, int); instead of int sum (int a, int b);
When do you need a function declaration?
When you define a function before the main() function in your program then you don’t need to do function declaration. Let's understand with a simple example.
But if you are defining a function after the main() then you need to declare the function first, else you will get compilation error. Let's see the above Example:
Function Definition:
Block of code written inside the curly braces is called function definition or body of the function.
For Example:
/*Function definition or function body*/
int sum(int n1, int n2)
{
int result;
result = n1 +n2;
return result;
}
Function Call:
To execute the codes of function body, the user defined function need to be invoked (call).
In C language, to call a function you simply need to pass the required parameters along with the function name, and if the function return a value you can store the return value.
The parentheses may or may not contain argument list, its depending on the function definition.
For Example:
/* Function call by passing value */
sum = sum(a, b);
See the above example where a function sum(a, b); is called inside the main()
Remember: The function can be called from anywhere in the program. It can be called from the main() function or from any other function if the program is using more than one function. The function that calls another function is called the “Calling function”.
Every C program has at least one function, which is main() and it is called by the operating system. main() function is the entry point of any C program. It is the point at which execution of program is started. When a C program is executed, the execution control goes directly to the main() function.
Points to Remember:
- Any program must have at least one function. If a program contains only one function, it must be main().
- Function is an operation, once defined can be used many times.
- Every C program has a function called main(); that is called by the operating system when a user run a program.
- Function consume memory only when it is invoked and released from RAM as soon as it finished its job.
- Every function has a name that must be unique.
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.