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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago