C Functions – Create and Run with Examples
Functions are a grouping statements to perform operations. Why we group statements in a function? While developing applications same operations can be done thousand of times and writing same code a lot of time is not efficient and exiting. Also changing single statement will require all of the copied statements to change. Every C applications have at least one function which is the entrance function named main() . Below we can see the syntax of the function.
- return_type defined what type of data will be return with return variable; line. As an example if we return integer we should set return_type as int and put integer variable or value as return variable;
- function_name is the name of the function and will be used while calling the function. Function names have similar restrictions to the variables.
- (parameter_list) parameter list provides required variables for the function. Parameter_list is used while calling function with its name.
- statements is the steps will be done while running inside a function. Function is implemented in the statements part.
Now we will create a simple function named sum which will sum two integer and return result. We can also expand this application by adding divide,multiply, subtract functions as a calculator.
int sum(int val1, int val2)
int result = val1 + val2;
Call By Value and Call By Reference