coderz.py

Keep Coding Keep Cheering!

Passing arguments between functions in C

Matrix multiplication

The functions that we use need to be flexible, i.e, they should be able to pass values among other functions. The mechanism used to convey information to the function is the ‘argument‘. For instance, we have used the scanf() and printf() function, in which we had been passing arguments like the format string and the list of variables used inside the parentheses in these functions. The arguments are also known as parameters.

Let’s consider a program to calculate the sum of integers.
#include<stdio.h> 
 void main() 
 { 
        int a,b; 
        printf("Enter Two Number : "); 
        scanf("%d%d",&a,&b); 
        sum(a,b); 
        
 } 
       void sum(int x,int y) 
   { 
        int z; 
        z=x+y; 
        printf("Sum of Two Number is : %d",z); 
        
 } 
output:
Enter Two Number : 4 5
Sum of Two Number is : 9
  • In this program, from the function main(), the values of a and b are passed on to the function sum(), by making a call to the function sum() and mentioning a and b in the parenthesis: sum(a,b );
  • In the sum() function, the values get added and stored in the z variable and get printed.
  • the variables a and b are called ‘actual arguments’, whereas the variables x and y are called ‘formal arguments’.
  • Note: the type, order, and number of the actual and formal arguments must always be the same.
return statement:

The return statement has the following functionality:

  • On executing the return statement, it immediately transfers the control back to the calling function.
  • It returns the value, present in the parentheses after return, to the calling function.
  • There is no restriction on the number of return statements that may be present in a function.

Note: also read about the do-while loop in c & while loop examples in c

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement