Functions:
A function definition as follows:return_type function_name( parameter )
{
body of the function
}
Return Type: A function may return a value(int or float or char). The return_type is the data type of the value the function returns. Some functions does not returning a value. In this case, the return_type is void.
Parameters: A parameter is like a placeholder. When a function is called, you pass a value to the parameter. This value is referred to as parameter or argument. Parameters are optional; that is, a function may contain no parameters.
Function Body: The function body contains a collection of statements that define what the function does.
Calling of a Function:
To use a function, it must be called in main.When a program calls a function, program control is transferred to the called function. A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.
To call a function, it is needed to pass the required parameters along with function name, and if the function returns a value, then returned value can be stored.
Passing Arguments by Value and by Reference:
In many programming languages, there are two ways to pass arguments:pass-by-value
pass-by-reference.
pass-by-value:
When arguments are passed by value, a copy of the argument’s value is made and passed to the called function. Changes to the copy do not affect an original variable’s value in the caller.
pass-by-reference:
When an argument is passed by reference, the caller allows the called function to modify the original variable’s value.
Code:
#include <stdio.h> int FuncByValue(int a) { return a=a*a; } int FuncByReference(int *a) { return *a = *a * *a; } int main() { int c = 5; int a = FuncByValue(c); printf(“After calling by value:%dn”,a); printf(“Passed value:%dnn”,c); int d = FuncByReference(&c); printf(“After calling by reference:%dn”,d); printf(“Passed value:%dn”,c); return 0; }
Output:
After calling by value:25Passed value:5
After calling by reference:25
Passed value:25
Implementation of Functions in C:
Q: Write a program containing three functions named ‘Input_Matrix’, ‘Transpose_Matrix’ and ‘Display_Transpose’ to enter, calculate and display the transpose of a two dimensional matrix. The dimensions of the matrix must be taken as input. Use appropriate parameters and return type. The main fuction must call only one function to start the execution./* QUESTION#4 Write a program containing three functions named ‘Input_Matrix’, ‘Transpose_Matrix’ and ‘Display_Transpose’ to enter, calculate and display the transpose of a two dimensional matrix. The dimensions of the matrix must be taken as input. Use appropriate parameters and return type. The main fuction must call only one function to start the execution. HINT: Call the next function inside the previous one. */ #include<stdio.h> int rows; int column; int Input_Matrix() { printf("Please enter number of Rows "); //taking dimension input scanf("%d",&rows); printf("Please enter number of column "); scanf("%d",&column); int array[rows][column]; //declearing array for (int i=0;i<rows;i++) { for (int j=0;j<column;j++) //taking value of array as input { printf("Please enter elements of an array %d%d ",i,j); scanf("%d",&array[i][j]); } } Transpose_Matrix(array); } int Transpose_Matrix(int array[rows][column]) { int transpose[10][10]; for (int i=0;i<rows;i++) { for (int j=0;j<column;j++) // taking transpose { transpose[j][i]=array[i][j]; } } Display_Matrix(array,transpose); } int Display_Matrix(int array[rows][column],int transpose[10][10]) { printf("The value of array is \n"); for (int i=0;i<rows;i++) { for (int j=0;j<column;j++) //printing transpose of matrix { printf("%d ",array[i][j]); } printf("\n\n"); } printf("\n"); printf("Transpose of array is \n"); for (int i=0;i<column;i++) { for (int j=0;j<rows;j++) //printing transpose of matrix { printf("%d ",transpose[i][j]); } printf("\n\n"); } } int main() { Input_Matrix(); return 0; }
Comments
Post a Comment