a function is a bunch of statements grouped together and making it as a logical unit .functions are used to minimize the code for the required program .separate function does separate tasks. We can split our program into different functions, to make the program readable and maintainable .or a function is a set of instructions, which does some specific tasks by accepting relevant data to output the necessary result.
these functions are divided into 1)standard library functions 2)user-defined functions
Standard library functions
these are ready-made functions .these are inbuilt functions in the c programming language. These are used as I/O processing, string handling and common functions like printf() and scanf() .these all functions are written in header file stdio.h .as we all know we cannot write a c programming without main() , because this is a function and every program starts execution from this main function.
in c languages we can (users) can create their own function according to their needs and to minimize the length of code.by this the code can be easily understandable .we can use the same code in other resources .we can dived larger code into smaller parts
every program starts from main function. when the controller encounters any function name, it automatically jumps to the function and executes it, after executing the controller jumps to the next statement or if we provide return statement controller returns.
we will see one example int add(int a,int b); .here int is the return datatype and add is a function name ,int a is argument with its type int.
calling a function:
controller is transferred to the function by calling it .syntax is given below.
Function definition: this contains body of the function ,syntax is given below
Passing arguments to a function:
variables used in the function calling is called arguments .variables used in the function definition is called arguments.in the above example at the 9th line a,b variables are passed during the function call. the parameters p,q accepts the passed arguments in the function defination.t hese are called formal parameters of the function.
The type of arguments passed to a function and the formal parameters must be same , otherwise the compiler throws error. if a,b are character type p,q should be char type. if a,b are float type p,q should be float type
the return statement is used to terminate the execution of program and returns a value to function call.in the above example results returned to variable c.
user-defined functions can be categorized into four:
- Function with no arguments and no return value
- Function with no arguments and a return value
- Function with arguments and no return value
- Function with arguments and a return value
we will see examples for each type