Function
Function
PROGRAMMING
Prepared by: Lochan Raj Dahal
C PROGRAMMING FUNCTIONS
Here, as shown in the figure above arguments_value is used to send values to the called program.
PASS BY VALUE
// arguments pass by value ▪ Pass by value is a method in which a copy of the value of the variables is passed to
# include <stdio.h> the function for the specific operation.
int add (int a, int b) ▪ In this method, the arguments in the function call are not modified by the change in
{ parameters of the called function. So the original variables remain unchanged.
return( a + b ); ▪ Example of passing arguments by value to function in C
}
int main()
{ • In this program, function add() is called by passing the arguments x and y.
int x, y, z;
x = 5; • The copy of the values of x and y are passed to a and b respectively and then
y = 5; are used in the function.
z = add(x,y); // call by value
return 0; • So by changing the values of a and b, there will be no change in the actual
} arguments x and y in the function call.
//end of program
Pass by reference
▪ Pass by reference is a method in which rather than passing direct value the
address of the variable is passed as an argument to the called function.
▪ When we pass arguments by reference, the formal arguments in the called
function becomes the assumed name or aliases of the actual arguments in the
calling function. So the function works on the actual data.
EXAMPLE OF PASSING ARGUMENTS BY REFERENCE
TO FUNCTION IN C
Function Call
Here, function square is called in main
Here, int before function name indicates that this function returns integer value to the caller while int inside
parentheses indicates that this function will receive an integer value from caller.
Function definition
• return;
The above return statement does not return value to the caller.
• return expression;
The above return statement returns the value of expression to
the caller.
• return 0;
The above return statement indicate whether the program
executed correctly.
Types of user defined functions in C
Depending upon the presence of arguments and the return values, user defined functions
can be classified into five categories.
1. Function with no arguments and no return values
2. Function with no arguments and one return value
3. Function with arguments and no return values
4. Function with arguments and one return value
5. Function with multiple return values
#include <stdio.h> ❑ Here function will accept data from the calling
void area( int square_side); //function prototype function as there are arguments, however, since there
int main() is no return type nothing will be returned to the
{ calling program. So it’s a one-way type
int square_side;
printf("Enter the side of square :");
communication.
scanf("%d",&square_side);
area(square_side); //function call
return 0; Explanation
} In this function, the integer value entered by the user in
void area(int square_side) square_side variable is passed to area(); .The called function has
{ void as a return type as a result, it does not return value.
int square_area;
square_area = square_side * square_side; This program calculates the area of a square and prints the result.
printf("Area of Square = %d",square_area);
}
4: function with arguments and one return value
Function with arguments and one return value means both the calling function and called function will
receive data from each other. It’s like a dual communication.
▪ C program to calculate the area of square using the function with arguments and one return value.
#include <stdio.h>
int area(int square_side); //function prototype with return type int
int main()
{
int square_area,square_side;
printf("Enter the side of square :");
scanf("%d",&square_side);
square_area = area(square_side); //function call
printf("Area of Square = %d",square_area);
return 0;
}
int area(int square_side)
{
int square_area;
square_area = square_side * square_side;
return square_area;
}
5: function with multiple return values
C program to calculate the area and volume of square using the function with multiple return values