C Functions – Create and Run with Examples – POFTUT

C Functions – Create and Run with Examples


[rps-include post=6557]

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 function_name(parameter_list)
{
    statements;
    return variable;
}
  • 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.

Defining Function

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.

#include <stdio.h> 
 
int sum(int val1, int val2) 
{ 
        int result = val1 + val2; 
 
        return result; 
} 
 
int main() 
{ 
 
        printf("%d\n",sum(2,3)); 
 
        return 0; 
}

Return Types

Calling Functions

Function Arguments

Call By Value and Call By Reference

[rps-include post=6557]

LEARN MORE  Php - Class

Leave a Comment