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

Pointers

Pointers: A pointer is a variable whose value is a memory address. A pointer contains the memory address of a variable that, in turn, contains a specific value. In this sense, a variable name directly references a value, and a pointer indirectly references a value. Syntax: type * variable ; Interpretation: The value of the pointer variable ptr is a memory address. A data item whose address is stored in this variable must be of the specified type. Dynamic Memory Management: C/C++ enables programmers to control the allocation and deallocation of memory in a program for any built in or user defined type. The ability for a program to obtain more memory space at execution time to hold new nodes, and to release space no longer needed is known as dynamic memory management. Syntax: int *num = (int *)malloc(sizeof (int)*numCount); or int *ptr = (int *)calloc(numCount, sizeof (int)); /* returns a pointer to a section of memory just large enough to hold the integers, whose q...

Single LinkList in Java

Linked List: Linked List contains a sequence nodes which are linked together. Each node contains a connection to another link and data. Linked list is the second most-used data structure after array. Following are the important terms to understand the concept of Linked List. Link − Each link of a linked list can store a data called an element. Next − Each link of a linked list contains a link to the next link called Next. LinkedList − A Linked List contains the connection link to the first link called First. Types of Linked List: Following are the various types of linked list. Simple Linked List − Item navigation is forward only. Doubly Linked List − Items can be navigated forward and backward. Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous. Basic Operations: Insert:  Inserts at tail,  specific index. Delete: Deletes from the tail.  specific index. ...

Object Oriented Programming (OOP)

Object Oriented Programming: Object Oriented Programming (OOP) is a programming concept which used in the modern programming world. Languages like Java, C++,  and Python support Object Oriented Programming (OOP). It works on the principle that objects are the most important part of a program. In OOP we think in terms of objects and every object has its attributes (properties) and a state (behavior/functions). Object Oriented Programming (OOP) is a technique of system modeling and its main purpose is to understand the product before developing it and manipulating these objects to achieve a specific task.   Pillars of Object Oriented Language (OOP): There are four basic principles of Object Oriented Language (OOP). Inheritance Polymorphism Data Encapsulation Abstraction WHY is Object Oriented Language (OOP) NEEDED? Problems with Procedural Languages: Functions have unrestricted access to global data Unrelated Functions and data. Before Object Oriente...