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

Estimate π Using Monte Carlo Method

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

5 days ago

Longest Substring with K Distinct Characters

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

6 days ago

Staircase Climbing Ways

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

2 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

2 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

2 weeks ago

Largest Sum of Non-Adjacent Numbers

Problem Statement (Asked By Airbnb) Given a list of integers, write a function to compute…

2 weeks ago