The purpose of this article is to explain what is function? why it is necessary? the types of function, categories of function, and how to create a function with examples.
What is Function?
The goal of functions in C is to improve reusability, understandability, and to keep track of them.
Need of Function
Rather than create a complex program, we divide it into smaller and smaller segments, and each segment accomplishes a specific task. Modules and functions are the same things in C. It is a problem-solving technique based on the Divide and Conquers Technique. The modules follow a hierarchy top-down.
Table of Content
Characteristics or Rule of Function
- You can define a prototype function inside or outside of the main function.
- If it is define in main( ), then it will Local.
- If it is define outside main( ), then it will Global.
- Function calling always done with in any other function.
- Both the formal argument and the actual argument can have the same name.
- There is no need to include the variable name in the prototype.
- It will save any variables in the calling function for use at a later time through the return statement.
- There is at least one function in a C program.
- There can only be one function in a program; this is the main() function.
- You may use as many Functions as you want.
- You can call a function several times.
- There is only one value a function can return at a time.
- If a value of a formal argument changed in the called function, this change does not reflect in calling function because of the scope.
- Each function of C returns a value, by default that value is an integer.
Types of Function
Generally, there are two types of functions:
- System Defined Functions and
- User Defined Functions
System Defined Function
- printf( ), scanf( ), etc. are all part of the system define function.
- When you include the header file, these functions become available.
- For Example:
- The printf() function sends formatted output to the screen (displays input on the screen).
- conio.h includes the scanf() function to read character, string, numeric data from the keyboard.
- There are numerous other library functions under “stdio.h”, such as scanf(), fprintf(), getchar(), and etc.
User Defined Function
- The user defined functions are those created by the user.
- You can define a multitude of functions according to the complexity and needs of the program.
Features of User Defined Functions
- Hence, the program will be simpler to understand, maintain, and debug.
- Easily reused codes for other programs.
How to create user defined functions?
Basically, there are three phases:
Phase 1: Function Declaration / Prototype
- The prototype of a function simply specifies the name, parameters, and return type of the function.
- A function body isn’t part of it.
Syntax
returnType functionName(type1 argument1, type2 argument2,…);
Phase 2: Function Calling
- In this case, we are calling the actual function.
- By calling the function, the user can take control of the program.
Syntax
functionName(argument1, argument2, …);
Phase 3: Function Definition
- A function definition contains all the code required to complete the task.
Syntax
returnType functionName(type1 argument1, type2 argument2, …) { // body of the function }
- With a call, the control of the program moves to the definition. The compiler then executes the code in the body of the function.
This is how it works:
- C program does not execute the statement in a function until the function is called.
- The program may pass arguments to the called function when it calls the function.
- A called function is a function that is used when it is called.
- The function often uses data passed to it from the calling function.
- Calling functions pass data to called functions by specifying arguments in their argument lists.
- You cannot send data with an argument list.
- It is only copy data / copy value / copy variable that pass from the calling function.
- The called function then performs its tasks.
Program
#include<stdio.h> #include<conio.h> int greater(int a,int b); // function declaration void main() { int i,j,answer; clrscr(); i=10; j=12; answer=greater(i,j); // function call // i,j are actual argument printf("%d",answer); getch(); } int greater(int a,int b) // function declaration { (a, b are formal argument) if(a>b) return a; else return b; }
Output
12
Description of an Example
- Greater is the name of the function.
- The required data type is int.
- a, b are the variables to pass on.
- Using an argument, each function can perform a specific task given a value.
Methods of Function Calling
- In order to pass arguments or to call the function, we have two options:
Call by Value
- This method passes the value of the variable as a parameter to the function.
- It is not possible to modify the actual value of the parameter by giving it a formal value.
- It is not enough to allocate the same memory to the actual and formal parameters because the values of the actual and formal parameters are different.
Program
#include<stdio.h> #include<conio.h> void change(int); void main() { int a; a=10; clrscr(); printf("%d\n",a); change(a); printf("%d",a); getch(); } void change(int a) { a=20; printf("%d\n",a); }
Output
10 20 10
Call by Reference
- Due to passing a reference (address), we modify the original value, as in call by reference.
- As a consequence, the value changed inside the function reflects inside and outside the function.
Program #include<stdio.h> #include<conio.h> void change(int *); void main() { int a; a=10; clrscr(); printf("%d\n",a); change(&a); printf("%d",a); getch(); } void change(int *a) { *a=20; printf("%d\n",*a); }
Output 10 20 20
Category of Function
In general, there are four main categories: Functions with NO Argument and NO Return Value, Functions with Argument and NO Return Value, Functions with NO Argument and Return Value, Functions with Argument and Return Value.
Function with NO Argument and NO Return Value
- There are no arguments to the called function.
- Not able to get any value from the calling function.
- There is no return value.
- Neither the calling nor the called function exchanges data.
Program #include<stdio.h> #include<conio.h> void main() { void show(); void display(); clrscr(); printf("In main function\n"); show(); display(); printf("Back in main\n"); getch(); } void show() { printf("In show function\n"); } void display() { printf("In display function\n"); }
Output In main function In show function In display function Back in main
Function with Argument and NO Return Value
- Arguments are part of a function.
- Functions can pass values to functions they call, but the calling function does not receive any values.
- There is data transfer from the calling function to the called function, but no data transfer from the called function to the calling function.
- It is generally the called function that prints the output.
Program #include<stdio.h> #include<conio.h> void sum(int,int); void main() { int x,y; clrscr(); printf("Enter two no:\n"); scanf("%d%d",&x,&y); sum(x,y); getch(); } void sum(int a,int b) { int c; c=a+b; printf("\n Sum is %d", c); }
Output Enter two no: 2 3 Sum is 5
Function with NO Argument and Return Value
- Gets no value from the calling function.
- Returns a value to the program that called it.
Program #include<stdio.h> #include<conio.h> int sum(); void main() { int n; n=sum(); clrscr(); printf("Sum=%d",n); getch(); } int sum() { int a=10,b=20; return a+b; }
Output Sum=30
Function with Argument and Return Value
- The calling function passes arguments to the called function.
- A called function returns its value to the calling function.
- Most often used in programming because it can communicate in both directions.
- Later, our program will be able to use the data returned by the function.
Program #include<stdio.h> #include<conio.h> int sum(int,int); void main() { int n; clrscr(); n=sum(10,20); printf("Sum=%d",n); getch(); } int sum(int x,int y) { return x+y; }
Output Sum=30
Conclusion
In order to reuse the function, you will need to create it in a program.