Categories: C

Passing arguments between functions in C

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

Share
Published by
Rabecca Fatima

Recent Posts

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

4 days ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

3 weeks ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

3 weeks ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

1 month ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

1 month ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

1 month ago