CodesandTutorials
Home   Programming   Hardware   Networking
 
Next generation website theme is here (black, neon and white color) - Reduces eye strain and headache problems.  

C Categories

Passing & Returning Arguments

« Previous Point - Function Introduction Next Point - Call by Value »

Description

An argument is a variable name having a data-type in a function. A function may have one or more arguments with same or different data-type. It is also called as parameter.

Rules to pass and return agruments between functions

  • Argument must be decalred in the function or earlier in the program.
  • Number of arguments declared in the function should be same in the definition of the function i.e. count of the arguments.
  • Agruments should be seprated by commas.

Syntax

Return-type function-name(argument list);

Example:

result = calculate(args1, args2, args3);   //function call
.
.
.
calculate(int var1, int var2, float var3)   //function definition
{
    //function body
    return value;
}

Where args1, args2 and args3 are the actual arguments(parameters) and var1, var2, var3 are the formal parameters.

Source code

  • //Calculating area of triangle
  • #include <stdio.h>
  • #include <conio.h>
  • void main()
  • {
  • 	int base,height,result;
  • 	clrscr();
  • 	printf("Enter the base value : ");
  • 	scanf("%d",&base);
  • 	printf("Enter the vertical height value : ");
  • 	scanf("%d",&height);
  • 	result=calculate(base,height);
  • 	printf("Area of Triangle : %d",result);
  • 	getch();
  • }
  • int calculate(int b, int h)
  • {
  • 	int area;
  • 	area =0.5*b*h;
  • 	return area;
  • }

Program working steps

  • First line is the comment about the program
  • 2nd and 3rd lines are preprocessor directives which include function defintion of clrscr(), getch(), printf(), scanf() etc.
  • void main() is the function, where program execution starts.
  • Next is the declaration of variables and clearing of the screen using clrscr() function.
  • Now program prompts to enter the value of base and height and stores to the respective variables.
  • On line no.13 calculate() is the caller function which passes two variables base and height. When this calculate function is called the program control jumps to the line no.17 where calculate function return data-type is int with arguments declared int b and h.
  • Value passed from caller function are copied into the variable b and h.
  • Line no.20 calculates the area of triangle supplied with base and height values.
  • return area; statement returns the calcuated value and program control moves back to the line no .13.
  • The returned value from the function is stored into the variable result.
  • Solution of program area of triangle is displayed using printf statement.
  • getch() function to wait for the user to exit the program.

« Previous Point - Function Introduction Next Point - Call by Value »

Tip Box

Remember

Caller function braces ends with semicolon, while function definition braces doesn't ends with semicolon.


Advertisement

Rate Website
   

 


Terms of Use | Privacy Policy | Contact Us
CodesandTutorials © 2014 All Rights Reserved