Introduction to function in C Language
Function is defined as the named group of statements which can be used to perform a specific task. A function can contain one or more valid C language statements.
When we call a function, all the statements written within the function are executed on after another.
Advantages of a function
- It is easy to locate and correct errors in a function.
- Function reduces complexity of a program.
- It is easy to maintain functions.
- It is easy to perform testing of a program.
- Functions are used to divide a bigger problem into smaller section, so it becomes easier to understand the program.
- A function can be used by many other functions so a programmer can build a new application using existing functions without any need to start from beginning.
- Portability of program increases by using functions.
- It makes a program simple to understand and use.
- By using functions, memory space can be efficiently used.
- Functions reduce redundancy i.e. it helps in avoiding repetition of code which saves time and space.
- A function can be used by more than one program.
- Execution speed of program increases because of using functions.
- User defined functions are very flexible so that they can be used to solve many complex problems in easy way.
Defining a function in C Language
A function must be defined before it can be called within a program.
The syntax of a function is
Return_Type specifies the data types of the value returned by the function. It can be any primary data type of C Language like int, float, char or any secondary data type of C Language like structure.
It should be void if function doesn’t return any value. The return type must be same as the data type of the value returned by the function.
Func_name refers to the name of function defined by the programmer. It should follow all the rules of a valid identifier of C language. Each C language program must contain one function named main() where the program actually starts execution.
Arguments refers to the set of variables declared within the pair of parenthesis after the function name. Each of the variables declared within parenthesis are known as function arguments.
These arguments can receive values from another function. The names of arguments must follow all the rules of a valid identifier of C Language. There should be no semicolon after the parenthesis.
Local Variables are declared and used within the function body only. These variables get automatically destroyed when the program control leaves the function.
Statement_Block refers to set of valid C language statements which would execute when we call the function.
return is a keyword of C Language. It is used when function returns some value. It appears with an expression with or without pair of parenthesis after it. We can use it without parenthesis and arguments to simply return the control of program to the calling function.
Expression is any valid expression of C language. The return type of function depends upon this expression. If this expression is of int type, the return type of function should be int. Similarly if this expression is of float type, the return type of function should be float and so on.
Components of a function
There are various components of function which include the following things.
i. Function prototype
Function prototype is the function declaration that specifies the return type and data types of the arguments. The main purpose of the function prototype is to prevent errors due to data type mismatches between the values passed to a function and types of values function is expecting through arguments.
In function prototype, we simply need to specify the name of the function along with the data types and names of arguments. The names of arguments are optional during prototype declaration. We need to put semicolon at the time of declaring function prototype. We can declare the function prototype as
void change(int a);
ii. Function Definition
Function definition is the way to specify the body of the function. It is basically a collection of statements that will execute when function is called. The body of function is enclosed within braces. The general syntax of function definition is:
Return_Type Func_name(type1 arg1,type2 arg2,…………..typeN argN)
Return_Type refers to the type of value returned by the function name.
type1,type2,…typeN refer to the data types of arguments passed to the function.
arg1, arg2…. argN refer to the names of arguments. The arguments must be separated by commas. All the arguments must be independently declared with the data type specifiers and name. While defining a function, we need not to put semicolon after the parenthesis given with the function name.
void show(int a)
iii. Function call
A function can be called by specifying its name followed by the required number of actual arguments separated by commas enclosed in parenthesis. If no arguments are required then there must be empty pair of parenthesis after function name. The syntax of function call statement is same as the function prototype. The function call statement must be followed by a semicolon.
To call function show() defined above, we can write the following statement
iv. Calling Function
A function that calls another function is known as calling function. In most of the programs main acts as a calling function as it calls other functions.
v. Called Function
A function that is invoked by other function is known as called function. Called function can be a library function like clrscr(), getch() or a user defined function.
TIPS and tricks
Why should we prototype a function?
A function prototype tells the compiler what kind of arguments a function is going to receive and what kind of return value a function is going to give back. This approach helps the compiler ensure that calls to a function are made correctly and that no erroneous type conversions take place.
Best Books of C