Passing & Returning Arguments
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.
Return-type function-name(argument list);
result = calculate(args1, args2, args3); //function call
calculate(int var1, int var2, float var3) //function definition
Where args1, args2 and args3 are the actual arguments(parameters) and var1, var2, var3 are the formal parameters.
- //Calculating area of triangle
- #include <stdio.h>
- #include <conio.h>
- void main()
printf("Enter the base value : ");
printf("Enter the vertical height value : ");
printf("Area of Triangle : %d",result);
- int calculate(int b, int h)
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.
Caller function braces ends with semicolon, while function definition braces doesn't ends with semicolon.