Skip to main content

Functions

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:25
Passed 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

Popular posts from this blog

Data Types

DATA TYPES in C/C++: Since C and C++ are strongly type languages, therefore every thing must be defined. Variables are classified according to their data type, which determines the kind of information that may be stored in them. Data Types Description C-Keyword Format Specifier integer Integers are whole numbers that can have both positive and negative values but no decimal values.   int   %d or %i Float Floating type variables can hold real numbers precision of 6 digits. float %f Double can hold real numbers with the precision of 14 digits. double %f Character data type allows a variable to store only one character. char %c Implementation in C: #include<stdio.h> int main() { printf(“%d n”,18); //this will print integer printf(“%fn”,32.56); //this will print decimal number printf(“%d %d %dn”,32,56,89); // %d is a place holder for integer and it provide place for one integer printf(“%ld...

Tic-Tac-Toe Implementation in Cpp (C++) Step by Step

Conditional Statement

Conditional Statement: A conditional statement is an if-then statement which means that if some statement is true then it follows the if-block of code and if the condition is false then it follows the else-block of code. In the programming world, there are two types of conditional statement namely if-else and switch. if-else Statement: Syntax: if (condition 1) { } else if (condition 2) { } else { }   Implementation of If-else statement in C: Q: Write a program to find the greatest of three numbers. #include<stdio.h> int main() { int a,b,c; printf(“Please Enter First Number “); scanf(“%d”,&a); printf(“Please Enter Second Number “); scanf(“%d”,&b); printf(“Please Enter Third Number “); scanf(“%d”,&c); if (a>b && a>c) { printf(“The Greatest Number is %d”,a); } else if (b>c) { printf(“The Greatest Number is %d”,b); } else { pr...