C function arguments and return values
C function argument and return values
1. Function with no argument, no return value
When a function with no arguments, does not receive any data from the calling function and when it does not return a value, the calling function does not receive any data from the called function.
Syntax
Function declaration : void function(); Function call : function(); Function definition : void function() { statements; }
Program function with no argument, no return value
#include <stdio.h> void value(void); void main() { value(); } void value(void) { int yr = 1, period = 5, amnt = 5000, inrate = 0.12; float sum; sum = amount; while (yr <= period) { sum = sum * (1 + inrate); year = year + 1; } printf(" The total amnt is %f:", sum); }
Output
The total amnt is 5000.000000
2. Function without arguments, with return value
An example is getchar function it has no parameters but it returns an integer and int type data that represents a character.
Syntax
Function declaration : int function(); Function call : function(); Function definition : int function() { statements; return x; }
Program for function without arguments, with return value
#include<stdio.h> void printName(); void main () { printf("Hello "); printName(); } void printName() { printf("StudyExperts"); }
Output
Hello StudyExperts
3. Function with arguments, without return value
When a function has arguments, it receives any data from the calling function but it does not return values.
Syntax
Function declaration : void function (int); Function call : function( x ); Function definition: void function(int x) { statements; }
Program for function with arguments, without return value
#include<stdio.h> void sum(int, int); void main() { int a,b,result; printf("\nsum of two numbers:"); printf("\nEnter two numbers:"); scanf("%d %d",&a,&b); sum(a,b); } void sum(int a, int b) { printf("\nThe sum is %d",a+b); }
Output
sum of two numbers: Enter two numbers 10 20 The sum is 30
4. Function with arguments, with return value
Syntax
Function declaration : int function (int); Function call : function( x ); Function definition: int function(int x) { statements; return x; }
Program for function with arguments, with return value
#include<stdio.h> int sum(int, int); void main() { int a,b,result; printf("\sum of two numbers"); printf("\nEnter two numbers:"); scanf("%d %d",&a,&b); result = sum(a,b); printf("\nThe sum is : %d",result); } int sum(int a, int b) { return a+b; }
Output
sum of two numbers: Enter two numbers:10 20 The sum is : 30
Also read, Types of Function