Function in c
If a line of code is needed repeatedly in a program, then the function is used.
The function also provides a significant representation of a program.
A Function is of two types.
1. Built-in function
2. User-defined function
1. Built-in functions are those function which is already present in the header file.
Eg. "printf" and "scanf" functions are built-in functions present in "stdio.h" header file.
getch() and clrscr() are built-in functions present in conio.h header file.
2. User-defined functions are those function which is defined by the user. we can also include user-defined functions in the library file.
Elements of a user-defined function
There are three elements of a user-defined function.
1. Function declaration
2. Function calling
3. Function defination
Important factors to remember::
1. Return
type: it’s a value that is returned to the main function before a function completes its execution.
2. Parameter list: it’s a variable name declared in front of the function name under round braces to pass the variable values through function declaration and function definition.
3. Argument list: this is the value or expression passed through the function calling.
1. Function declaration:
Function declaration simply tells the compiler about the function name, its return type, and argument list( i.e., how many parameters are to be passed through the function).
A function declaration is ended with a semicolon (;) that is a terminating symbol.
A function can return only one value.
Syntax: [<return_type>] <function name>(<parameter list>);
Example::
a. int sum(int,int) or int sum(int a,int b);
Here int is the return type, and the sum is the function name, and (int) is the parameter passed through the function.
We have shown two methods of the function declaration. In the second method, the parameters are shown with the declaration of the variable. Both methods are valid
b. void sum();
Here the void is the return type that means nothing is returned. The sum is the function name, and no parameters are passed through the function.
c. void sum(int);
Here one parameter is passed through the function.
Important points:
1. by default, a function returns an integer value.
2. A single function can pass a minimum of zero arguments and a maximum of 253 arguments.
3. If we are not passing any argument, then we can write void under round braces.
Eg. int sum(void);
4. If a function is not returning a value, then write void.
Eg. void sum(int);
5. If a function is not returning as well as passing an argument, then write void.
6. Eg. void sum(void);
2. Function calling
After the declaration of the function, we need the body of the function where we write the block of statements to perform the task. That is called a function definition. The function definition is out of the main function. As we know, the main function is called by the operating system when the user runs the program. The main function is always the first code executed when the program starts. Thus, to operate the body of the function( function definition), The function has to be called from the main function.
While calling from the main function, we can pass the arguments through the function calling. After calling the function, the control goes to the function definition, i.e., the body of the function. After performing the task, when curly braces are closed, the control again gets back to the main function. If there is a value to be returned from the function definition to the main function, we can return the value. Only one value can be returned at a time.
Syntax:
1. When a function is not returning a value and passing arguments.
<function name>(<argument list>);
Eg: sum(10,20);
2. When function is returning a value and passing arguments
return_type=<function name>(<argument list>);
int=sum(10,20);
3. When function is returning a value and not passing the arguments
Return_type=<function name>();
int=sum();
Or
Return_type=<function name>();
int=sum();
3. Function definition
It’s a block of statements where the actual task is performed. a function definition is written outside the main function.
Function definition consists of two parts
a. Function header
b. Actual code/function bod
a. Function header
It consists of:
1. Return type
2. Function name
3. Parameter list
Syntax: [<return type>]<function name>(<parameter list>)
Example: int add(int a, int b);
Note: In function declaration variable name is not necessary but in a function definition, the variable name should be declared.
Ex: 1. int add(int,int); // function declaration
int add(int a, int b) // function definition
here function declaration and definition is valid
2. int add(int x,int y); // function declaration
It consists of:
1. Local variable declaration
2. Function statement
3. A return statement
Demo program
quiz: repeating a sentence using function
#include<stdio.h> void repeat(); // function declaration int main() { int i; for(i=0;i<=3;i++) { repeat(); //function calling } printf("The end"); return 0; } void repeat() //function defination { printf("Life is a gift\n"); } Output: Life is a gift
Life is a gift Life is a gift Life is a gift The end |
Explanation:
In the program a simple function is declared which has no parameters passed and has the void return type. Hence, it will return nothing.
In the main function using for loop, a function is called 4 times. hence, 4 times the sentence “ Life is a gift” is printed.
After completing the loop next statement in the main is “The end” hence, it will get print.
When the for loop gets started, in the first iteration the control will go to the function definition, after completing the statement inside the function the control will go back to the main function and start the second iteration, and again control will go to the function definition.
The cycle goes on repeating until 4th iteration gets completed.
also, see:
A program consisting of return type and parameter list
call by value and call by reference
Post a Comment
Post a Comment