Users Online
· Guests Online: 28
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
FAQ: C Functions
FAQ (Frequently Asked Questions) >C Functions |
01 Can the Formal Parameters to a Function in C be Declared Static?
Question: Can the Formal Parameters to a Function in C be declared Static?
Answer: Let’s first try a static variable to see how it behaves when function containing the variable is called more than once. For example,
/*
* behaviour_static_var_fun.c -- program shows how static variable
* behaves
*/
#include <stdio.h>
void display_static_var(void); /* declaration */
int main(void)
{
int FLAG = 1;
while (FLAG) {
display_static_var();
printf("User, want to continue, enter 1 else 0,\n");
scanf("%d", &FLAG);
}
printf("Thank you!\n");
return 0;
}
void display_static_var(void)
{
static int count = 1;/* count is declared static and initialized once */
printf("static integer variable \"count\" is %d\n", count++);
}
Now, we turn to analyse the output of the above program below,
static integer variable "count" is 1
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 2
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 3
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 4
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 5
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 6
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 7
User, want to continue, enter 1 else 0,
0
Thank you!
We observed that static variable ‘count’ is initialized just once and thereon during each successive calls to the function ‘display_static_var()’ it retained its last modified value from previous call to function during the entire lifetime of program.
Let’s turn to see what happens when we declare formal parameters of type static. Consider an example,
/*
* static_formal_arg1.c -- program shows if formal parameters can be
* declared static
*/
#include <stdio.h>
void sum_ints(static int, static int); /*function declaration */
int main(void)
{
static int x, y;
int n;
printf("user, type in two integers...for their sum; (q to quit)\n");
while (n = scanf("%d %d", &x, &y) == 2) {
sum_ints(x, y);
printf("user, type in other two integers...for their sum;"
" (q to quit)\n");
}
printf("Thank you!\n");
return 0;
}
/* formal arguments of type static */
void sum_ints(static int u, static int v)
{
printf("Sum of two static integers %d and %d is %d\n", u, v, u + v);
}
Check the type of error when above program was compiled, storage class error for static parameters in function declaration and function definition.
static_formal_arg.c:3:1: error: storage class specified for unnamed
parameter
static_formal_arg.c:3:1: error: storage class specified for unnamed
parameter
static_formal_arg.c:19:26: error: storage class specified for parameter ‘u’
static_formal_arg.c:19:40: error: storage class specified for parameter ‘v’
This is because of nature of static variable. Static variable in a function is initialized once and retains its last modified value from the previous call to function during the entire execution of the program. When function is called, instructions in the function execute and function exits. However, static variable with its last modified value still exists in the memory. When function is called again static variable isn’t reinitialized instead its last modified value is used. When we attempted to declare formal parameters of type static, and compiled the program, it resulted in storage class error for formal parameters in function definition and for unnamed parameters in function declaration.
Recall that ANSI C Standard recommends function declaration for each function in a program. Function prototype tells the compiler that function can appear somewhere in the program. Further, in a function declaration, specified arguments are not created and therefore they can be omitted from the prototype. For example,
void new_msg(char *msg);
could be equivalently rewritten as
void new_msg(char *); /* name is omitted */
Therefore, formal parameters to a function are not declared static because of following two reasons:
1. TOTAL MEMORY REQUIREMENT IS LESS if the parameters are declared automatic because these are created as the function is entered and are destroyed as the function exits.
2. RECURSION CAN ONLY BE IMPLEMENTED if variables are declared automatic because these are pushed onto the stack when the function is called. For example:
#include <stdio.h>
long int compute_prod_recur(int);
int main(void)
{
int num; /* num is an automatic variable */
long int product; /* product is also an automatic variable */
printf("user, enter the natural number for which product successively"
" from 1,2,3,...till the num is to be computed\n");
scanf("%d", &num);
product = compute_prod_recur(num);
printf("Product through Recursion for the number is %ld\n", product);
return 0;
}
long int compute_prod_recur(int number)
{
/* number is an automatic variable */
if (number == 1)
return 1;
else
return number * compute_prod_recur(--number);
/*
* each time fun. compute_prod_recur() is called, number,
* an automatic variable, is created, initialized with value
* passed as an argument when the fun. was called & pushed onto
* the stack in LIFO (Last In First Out) manner.
*/
}
Top
Question: Can the Formal Parameters to a Function in C be declared Static?
Answer: Let’s first try a static variable to see how it behaves when function containing the variable is called more than once. For example,
/*
* behaviour_static_var_fun.c -- program shows how static variable
* behaves
*/
#include <stdio.h>
void display_static_var(void); /* declaration */
int main(void)
{
int FLAG = 1;
while (FLAG) {
display_static_var();
printf("User, want to continue, enter 1 else 0,\n");
scanf("%d", &FLAG);
}
printf("Thank you!\n");
return 0;
}
void display_static_var(void)
{
static int count = 1;/* count is declared static and initialized once */
printf("static integer variable \"count\" is %d\n", count++);
}
Now, we turn to analyse the output of the above program below,
static integer variable "count" is 1
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 2
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 3
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 4
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 5
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 6
User, want to continue, enter 1 else 0,
1
static integer variable "count" is 7
User, want to continue, enter 1 else 0,
0
Thank you!
We observed that static variable ‘count’ is initialized just once and thereon during each successive calls to the function ‘display_static_var()’ it retained its last modified value from previous call to function during the entire lifetime of program.
Let’s turn to see what happens when we declare formal parameters of type static. Consider an example,
/*
* static_formal_arg1.c -- program shows if formal parameters can be
* declared static
*/
#include <stdio.h>
void sum_ints(static int, static int); /*function declaration */
int main(void)
{
static int x, y;
int n;
printf("user, type in two integers...for their sum; (q to quit)\n");
while (n = scanf("%d %d", &x, &y) == 2) {
sum_ints(x, y);
printf("user, type in other two integers...for their sum;"
" (q to quit)\n");
}
printf("Thank you!\n");
return 0;
}
/* formal arguments of type static */
void sum_ints(static int u, static int v)
{
printf("Sum of two static integers %d and %d is %d\n", u, v, u + v);
}
Check the type of error when above program was compiled, storage class error for static parameters in function declaration and function definition.
static_formal_arg.c:3:1: error: storage class specified for unnamed
parameter
static_formal_arg.c:3:1: error: storage class specified for unnamed
parameter
static_formal_arg.c:19:26: error: storage class specified for parameter ‘u’
static_formal_arg.c:19:40: error: storage class specified for parameter ‘v’
This is because of nature of static variable. Static variable in a function is initialized once and retains its last modified value from the previous call to function during the entire execution of the program. When function is called, instructions in the function execute and function exits. However, static variable with its last modified value still exists in the memory. When function is called again static variable isn’t reinitialized instead its last modified value is used. When we attempted to declare formal parameters of type static, and compiled the program, it resulted in storage class error for formal parameters in function definition and for unnamed parameters in function declaration.
Recall that ANSI C Standard recommends function declaration for each function in a program. Function prototype tells the compiler that function can appear somewhere in the program. Further, in a function declaration, specified arguments are not created and therefore they can be omitted from the prototype. For example,
void new_msg(char *msg);
could be equivalently rewritten as
void new_msg(char *); /* name is omitted */
Therefore, formal parameters to a function are not declared static because of following two reasons:
1. TOTAL MEMORY REQUIREMENT IS LESS if the parameters are declared automatic because these are created as the function is entered and are destroyed as the function exits.
2. RECURSION CAN ONLY BE IMPLEMENTED if variables are declared automatic because these are pushed onto the stack when the function is called. For example:
#include <stdio.h>
long int compute_prod_recur(int);
int main(void)
{
int num; /* num is an automatic variable */
long int product; /* product is also an automatic variable */
printf("user, enter the natural number for which product successively"
" from 1,2,3,...till the num is to be computed\n");
scanf("%d", &num);
product = compute_prod_recur(num);
printf("Product through Recursion for the number is %ld\n", product);
return 0;
}
long int compute_prod_recur(int number)
{
/* number is an automatic variable */
if (number == 1)
return 1;
else
return number * compute_prod_recur(--number);
/*
* each time fun. compute_prod_recur() is called, number,
* an automatic variable, is created, initialized with value
* passed as an argument when the fun. was called & pushed onto
* the stack in LIFO (Last In First Out) manner.
*/
}
Top
02 Is Function an Integral Component of Every C Program?
Question: Is Function an Integral Component of Every C Program?
Answer: Before we proceed to answer this, let’s look at the design, in its simplest form, of a C program,
/*
* sim_c_prog.c -- program shows even simplest C program requires function
* main()
*/
void main(void)
{
/* Nothing happens here! */
}
When compile and run the program on Linux system, nothing happens! Here, ‘main()’ function takes no arguments nor does it return any value and further, it performs nothing. Hence, no headers are required! So why this is the simplest C Program!
Remember to specify ‘void’ keyword in parenthesis following the function name if any C function doesn’t take any arguments, and when any C function doesn’t return any value, specify type of function as ‘void’. Headers contain declarations for ANSI C library functions. In the above program, we haven’t used any C library function, for example, printf(), scanf() etc., so we didn’t include any header.
So, we, here, observed, function is an integral component of every C Program! Basically, according to C’s philosophy, functions are used to organize larger programs and allow them to be used across other programs too. Therefore, functions enable us to avoid redundancy of code!
Top
Question: Is Function an Integral Component of Every C Program?
Answer: Before we proceed to answer this, let’s look at the design, in its simplest form, of a C program,
/*
* sim_c_prog.c -- program shows even simplest C program requires function
* main()
*/
void main(void)
{
/* Nothing happens here! */
}
When compile and run the program on Linux system, nothing happens! Here, ‘main()’ function takes no arguments nor does it return any value and further, it performs nothing. Hence, no headers are required! So why this is the simplest C Program!
Remember to specify ‘void’ keyword in parenthesis following the function name if any C function doesn’t take any arguments, and when any C function doesn’t return any value, specify type of function as ‘void’. Headers contain declarations for ANSI C library functions. In the above program, we haven’t used any C library function, for example, printf(), scanf() etc., so we didn’t include any header.
So, we, here, observed, function is an integral component of every C Program! Basically, according to C’s philosophy, functions are used to organize larger programs and allow them to be used across other programs too. Therefore, functions enable us to avoid redundancy of code!
Top
03 Explain Functions in C with Examples
This C Tutorial Explains Different Components of a C Function with Example(s).
ANSI C Standard specifies general syntax of any C function as:
type function_name(formal_arguments) /* function definition header */
{
statements; /*function body*/
return statement;
}
Like variables, every function also has a type, meaning that what type of value function returns to its calling function. If, for ex., function returns integer, then function is of type int, if it returns float, then function is of type float and so on. Remember if a function returns nothing to its calling function, type of function is void.
Function name chosen should be descriptive specifying clearly what particular task function does. Function name is followed by a pair of parenthesis, within which specify comma separated list of arguments with their number and types in order corresponding to arguments from its calling function, and use void if function doesn’t take any arguments.
Function type, function name followed by arguments, in parenthesis, if any comprise function header. Function header is followed by pair of braces containing instructions about how function does particular task. Let’s see an example now,
#include <stdio.h>
void display(void); /* function declaration */
int main(void)
{
printf("main: going to call display...\n");
display(); /* call to display() */
return 0;
}
void display(void)
{
printf("i am display(): I display massage!\n");
return;
}
When a function returns nothing i.e. function is of type void, we can use return statement as ‘return;’ i.e. return keyword is not followed by any value.
Let’s take one more example,
/* sum2ints.c -- program sums up two integers */
#include <stdio.h>
int sum2ints(int, int); /* declaration or function prototype */
int main(void)
{
int u = 5, v = 10;
printf("sum of %d and %d is %d\n", u, v, sum2ints(u, v));
return 0;
}
/* sum2ints() sums up two integers and returns their sum to calling fun. */
int sum2ints(int x, int y) /* x, y are formal arguments */
{
return x + y;
}
Here’s output,
sum of 5 and 10 is 15
In the above program, function sum2ints() takes two integers, sum them up and return their sum to calling function to be displayed to the user.
Top
This C Tutorial Explains Different Components of a C Function with Example(s).
ANSI C Standard specifies general syntax of any C function as:
type function_name(formal_arguments) /* function definition header */
{
statements; /*function body*/
return statement;
}
Like variables, every function also has a type, meaning that what type of value function returns to its calling function. If, for ex., function returns integer, then function is of type int, if it returns float, then function is of type float and so on. Remember if a function returns nothing to its calling function, type of function is void.
Function name chosen should be descriptive specifying clearly what particular task function does. Function name is followed by a pair of parenthesis, within which specify comma separated list of arguments with their number and types in order corresponding to arguments from its calling function, and use void if function doesn’t take any arguments.
Function type, function name followed by arguments, in parenthesis, if any comprise function header. Function header is followed by pair of braces containing instructions about how function does particular task. Let’s see an example now,
#include <stdio.h>
void display(void); /* function declaration */
int main(void)
{
printf("main: going to call display...\n");
display(); /* call to display() */
return 0;
}
void display(void)
{
printf("i am display(): I display massage!\n");
return;
}
When a function returns nothing i.e. function is of type void, we can use return statement as ‘return;’ i.e. return keyword is not followed by any value.
Let’s take one more example,
/* sum2ints.c -- program sums up two integers */
#include <stdio.h>
int sum2ints(int, int); /* declaration or function prototype */
int main(void)
{
int u = 5, v = 10;
printf("sum of %d and %d is %d\n", u, v, sum2ints(u, v));
return 0;
}
/* sum2ints() sums up two integers and returns their sum to calling fun. */
int sum2ints(int x, int y) /* x, y are formal arguments */
{
return x + y;
}
Here’s output,
sum of 5 and 10 is 15
In the above program, function sum2ints() takes two integers, sum them up and return their sum to calling function to be displayed to the user.
Top
04 Explain Function Arguments in C Programming with Examples
This C Tutorial Explains Function Arguments in C Programming with Example(s).
Let’s, first, try to understand why function arguments are required. Consider, for example, a C Program below:
#include <stdio.h>
void hahaha(void); /* function hahaha() declared here */
int main(void)
{
hahaha(); /* hahaha() is called in main() */
return 0;
}
/* hahaha() defined here */
void hahaha(void)
{
printf("ha ha ha!\n");
}
Output as:
ha ha ha!
In the above program, ‘hahaha()’ just displayed a massage. It neither required to obtain any arguments from calling function in ‘main()’, nor did it need to return any value back to the calling function. In other words, ‘hahaha()’ didn’t need to communicate any information either ways with the calling function.
Basically, functions in a program are used for communicating information with calling function. For such communication, it requires arguments to be used both in function call and in function definition in correct number and order with their respective types. Function declaration, however, allows to omit argument names but their number and types in correct order, in a comma separated list, to be specified in the parenthesis following the function name. For example:
#include <stdio.h>
double mul_floats(float, float); /* declaration: variable names omitted */
int main(void)
{
float x = 4.67, y = 3.55;
double result;
result = mul_floats(x, y);
/* function call with arguments in correct order with types */
printf("multiplication of %f and %f is %lf\n", x, y, result);
return 0;
}
double mul_floats(float u, float v) /* u and v two floats declared */
{
return u * v;
}
Here’s output,
multiplication of 4.670000 and 3.550000 is 16.578501
In above program, two floats ‘x’ and ‘y’ in the calling function, mul_floats(x, y), are passed to function mul_floats() for multiplication. mul_floats() fun. received their copies in two other floats ‘u’ and ‘v’ respectively, which are private/local to this function. Recall here that floats ‘x’ and ‘y’ are actual arguments while floats ‘u’ and ‘v’ are formal arguments. mul_floats() then returned the product of two floats back to calling function via return statement. Notice that function mul_floats() is of type double, therefore return statement
return u * v;
will return a double value i.e. product of two floats u and v is typecast implicitly to double before sending back to the calling function.
Calling function received the product in a double variable named result & displayed the product to user.
Variables used in function call and function definition respectively to pass and receive the values are called function arguments. Note however that variables used in the calling function are called actual arguments while those used in the function definition are called as formal arguments or parameters.
Top
This C Tutorial Explains Function Arguments in C Programming with Example(s).
Let’s, first, try to understand why function arguments are required. Consider, for example, a C Program below:
#include <stdio.h>
void hahaha(void); /* function hahaha() declared here */
int main(void)
{
hahaha(); /* hahaha() is called in main() */
return 0;
}
/* hahaha() defined here */
void hahaha(void)
{
printf("ha ha ha!\n");
}
Output as:
ha ha ha!
In the above program, ‘hahaha()’ just displayed a massage. It neither required to obtain any arguments from calling function in ‘main()’, nor did it need to return any value back to the calling function. In other words, ‘hahaha()’ didn’t need to communicate any information either ways with the calling function.
Basically, functions in a program are used for communicating information with calling function. For such communication, it requires arguments to be used both in function call and in function definition in correct number and order with their respective types. Function declaration, however, allows to omit argument names but their number and types in correct order, in a comma separated list, to be specified in the parenthesis following the function name. For example:
#include <stdio.h>
double mul_floats(float, float); /* declaration: variable names omitted */
int main(void)
{
float x = 4.67, y = 3.55;
double result;
result = mul_floats(x, y);
/* function call with arguments in correct order with types */
printf("multiplication of %f and %f is %lf\n", x, y, result);
return 0;
}
double mul_floats(float u, float v) /* u and v two floats declared */
{
return u * v;
}
Here’s output,
multiplication of 4.670000 and 3.550000 is 16.578501
In above program, two floats ‘x’ and ‘y’ in the calling function, mul_floats(x, y), are passed to function mul_floats() for multiplication. mul_floats() fun. received their copies in two other floats ‘u’ and ‘v’ respectively, which are private/local to this function. Recall here that floats ‘x’ and ‘y’ are actual arguments while floats ‘u’ and ‘v’ are formal arguments. mul_floats() then returned the product of two floats back to calling function via return statement. Notice that function mul_floats() is of type double, therefore return statement
return u * v;
will return a double value i.e. product of two floats u and v is typecast implicitly to double before sending back to the calling function.
Calling function received the product in a double variable named result & displayed the product to user.
Variables used in function call and function definition respectively to pass and receive the values are called function arguments. Note however that variables used in the calling function are called actual arguments while those used in the function definition are called as formal arguments or parameters.
Top
05 What are Actual and Formal Arguments in a C Function
This C Tutorial Explains Actual and Formal Arguments in a C Function with Example(s).
Consider, for example, a C program below,
/*
* actual_formal_arg.c -- program hinges on relationship between actual and
* formal arguments
*/
#include <stdio.h>
void even_odd(int);
int main(void)
{
int num, what;
printf("\nProgram determines a given integer is EVEN or ODD\n\n");
printf("Enter an integer number: (q to quit)\n");
while (what = scanf("%d", &num) == 1) {
even_odd(num);
/* integer num here is distinct from integer num in even_odd() */
printf("Enter another integer: (q to quit)\n");
}
printf("Thank you!\n");
return 0;
}
/* num declared here is private to even_odd() */
void even_odd(int num)
{
if (num % 2 == 0)
printf("%d is EVEN Number!\n", num);
else
printf("%d is ODD Number!\n", num);
return;
}
Below is output of the above program, for several test integers,
Program determines a given integer is EVEN or ODD
Enter an integer number: (q to quit)
1009
1009 is ODD Number!
Enter another integer: (q to quit)
34
34 is EVEN Number!
Enter another integer: (q to quit)
12
12 is EVEN Number!
Enter another integer: (q to quit)
1
1 is ODD Number!
Enter another integer: (q to quit)
2
2 is EVEN Number!
Enter another integer: (q to quit)
q
Thank you!
Now, we unravel difference between actual and formal arguments. Integer variable num in ‘main()’ is local to ‘main()’ and is distinct from integer variable num, being private to ‘even_odd()’ even they have same names. Therefore, there’s no conflict with same names when used in different functions. When function
even_odd(num);
in ‘main()’ is called, actual value of num, here for ex. 1009, is placed in temporary storage area called stack, from where this value is copied to the argument num of called function.
void even_odd(int num)
{
}
Function ‘even_odd()’ performs on the copied value of original value of num. Therefore, argument num private to even_odd() function is called formal argument or formal parameter and argument num local to main() function is called actual argument.
Top
This C Tutorial Explains Actual and Formal Arguments in a C Function with Example(s).
Consider, for example, a C program below,
/*
* actual_formal_arg.c -- program hinges on relationship between actual and
* formal arguments
*/
#include <stdio.h>
void even_odd(int);
int main(void)
{
int num, what;
printf("\nProgram determines a given integer is EVEN or ODD\n\n");
printf("Enter an integer number: (q to quit)\n");
while (what = scanf("%d", &num) == 1) {
even_odd(num);
/* integer num here is distinct from integer num in even_odd() */
printf("Enter another integer: (q to quit)\n");
}
printf("Thank you!\n");
return 0;
}
/* num declared here is private to even_odd() */
void even_odd(int num)
{
if (num % 2 == 0)
printf("%d is EVEN Number!\n", num);
else
printf("%d is ODD Number!\n", num);
return;
}
Below is output of the above program, for several test integers,
Program determines a given integer is EVEN or ODD
Enter an integer number: (q to quit)
1009
1009 is ODD Number!
Enter another integer: (q to quit)
34
34 is EVEN Number!
Enter another integer: (q to quit)
12
12 is EVEN Number!
Enter another integer: (q to quit)
1
1 is ODD Number!
Enter another integer: (q to quit)
2
2 is EVEN Number!
Enter another integer: (q to quit)
q
Thank you!
Now, we unravel difference between actual and formal arguments. Integer variable num in ‘main()’ is local to ‘main()’ and is distinct from integer variable num, being private to ‘even_odd()’ even they have same names. Therefore, there’s no conflict with same names when used in different functions. When function
even_odd(num);
in ‘main()’ is called, actual value of num, here for ex. 1009, is placed in temporary storage area called stack, from where this value is copied to the argument num of called function.
void even_odd(int num)
{
}
Function ‘even_odd()’ performs on the copied value of original value of num. Therefore, argument num private to even_odd() function is called formal argument or formal parameter and argument num local to main() function is called actual argument.
Top
06 Can a Function in C Programming have Variable Number of Arguments?
Question: Can a Function in C Programming have Variable Number of Arguments?
Answer: Of course! Functions in a C program can have variable number of arguments. let’s first explore a very familiar standard C library output function ‘printf()’ below,
/*
* fun_with_varible_arg1.c -- program shows function takes variable number
* of arguments
*/
#include <stdio.h>
int main(void)
{
int num1 = 10, num2 = 20;
printf("\nWe are familiar with printf() and scanf() functions which\n"
"accept variable number of arguments!\n\n");
printf("First argument is String.\n");
printf("Successive arguments are comma separated list and type "
"specifiers\nfor each in Format String.\n\n");
printf("Sum of %d and %d is %d\n", num1, num2, num1 + num2);
/* four arguments */
printf("Numbers %d, %d, %d, %d, %d, %d are all Prime Numbers!\n",
2, 3, 5, 7, 11, 13); /* six arguments */
printf("\n");
return 0;
}
Output produced when we compiled and run the above program as,
Question: Can a Function in C Programming have Variable Number of Arguments?
Answer: Of course! Functions in a C program can have variable number of arguments. let’s first explore a very familiar standard C library output function ‘printf()’ below,
/*
* fun_with_varible_arg1.c -- program shows function takes variable number
* of arguments
*/
#include <stdio.h>
int main(void)
{
int num1 = 10, num2 = 20;
printf("\nWe are familiar with printf() and scanf() functions which\n"
"accept variable number of arguments!\n\n");
printf("First argument is String.\n");
printf("Successive arguments are comma separated list and type "
"specifiers\nfor each in Format String.\n\n");
printf("Sum of %d and %d is %d\n", num1, num2, num1 + num2);
/* four arguments */
printf("Numbers %d, %d, %d, %d, %d, %d are all Prime Numbers!\n",
2, 3, 5, 7, 11, 13); /* six arguments */
printf("\n");
return 0;
}
Output produced when we compiled and run the above program as,
We are familiar with printf() and scanf() functions which accept variable number of arguments!
First argument is String.
Successive arguments are comma separated list and type specifiers
for each in Format String.
Sum of 10 and 20 is 30
Numbers 2, 3, 5, 7, 11, 13 are all Prime Numbers!
Likewise, ‘scanf()’ function takes variable number of arguments. Now, we turn to ours’ concern about how can we declare and define such function that accepts variable no. of arguments.
The need for a function which accepts variable number of arguments arises where we need to pass different number and types of arguments to a function. Function with variable arguments can be implemented using macros. These macros are defined in stdarg.h header which is part of standard library. stdarg header declares a type called va_list and three macros viz. va_start, va_arg and va_end. The variable of type va_list is used with three macros to access the variable arguments.
Prototyping such a function is done as follows:
type name_function(type no_of_arguments,...);
/* note three ellipsis specify variable argument list */
Function definition header is given as below:
/* note three ellipsis specify var. arg. list */
type fun_name(type no_of_values, ...)
{
}
Let’s now consider an example,
/*
* fun_var_arg_imp_stdarg.c -- program shows how a function with variable
* arguments can be implemented
*/
#include <stdio.h>
#include /* macros are declared therein */
double average(int n_values, ...); /* prototyping function */
/* three ellipsis indicate unspecified arguments with unspecified types */
int main(void)
{
double av;
av = average(5, 1, 2, 3, 4, 5); /* calling the function */
printf("average of 1,2,3,4,5 is %lf\n\n", av);
return 0;
}
double average(int num_values, ...)
{
va_list varg; /* declared varg of type va_list*/
int count, sum = 0;
/* initialize varg to point to first of variable arguments */
va_start(varg, num_values);
printf("\n");
for (count = 0; count < num_values; count++ ) {
/* to access value of variable arguments, call macro va_arg() */
sum += va_arg(varg, int);
printf("sum is %d\n", sum);
}
printf("\n");
return (double)sum / num_values;
}
Let’s analyse the output when function is called as:
av = average(5, 1, 2, 3, 4, 5);
/* first argument specifies 5 variable integer arguments */
output is follows:
sum is 1
sum is 3
sum is 6
sum is 10
sum is 15
average of 1,2,3,4,5 is 3.000000
…what if we have had called the function as:
av = average(2, 1, 2, 3, 4, 5);
then output followed as:
sum is 1
sum is 3
average of 1,2,3,4,5 is 1.500000
and when we had called the function as:
av = average(6, 1, 2, 3, 4, 5);
output then followed as:
sum is 1
sum is 3
sum is 6
sum is 10
sum is 15
sum is -218509745
average of 1,2,3,4,5 is -36418292.000000
Limitations of Functions with variable arguments:
1. Macros can’t determine number of variable arguments
2. Type of a variable argument can’t be determined by examining the bits of argument.
The first named argument answers both specifying number and types of arguments. In the above program, named argument num_values specify number of variable arguments and their types assumed to be integers.
Top
07 What are ADTs and Black Boxes in Context with Functions in C?
This C Tutorial Explains ADTs and Black Boxes in Context with Functions in C.
Many programmers like to think of a function as a “black box” defined in terms of the information that goes in (its input) and the value or action it produces (its output). What goes on inside the black box is not your concern, unless you are the one who has to write the function. For example, when you use ‘printf()’, you know that you have to give it a control string and, perhaps, some arguments. You also know what output ‘printf()’ should produce. You never have to think about the programming that went into creating ‘printf()’. Thinking of functions in this manner helps you concentrate on the program’s overall design rather than the details. Think carefully about what the function should do and how it relates to the program as a whole before worrying about writing the code. Let’s see an example,
#include <stdio.h>
int largerof2ints(int, int);
int main(void)
{
int num1, num2, larger;
printf("\nProgram displays larger of two integers.\n");
printf("User, enter two integers...,(q to quit)\n");
while (scanf("%d %d", &num1, &num2) == 2) {
larger = largerof2ints(num1, num2);
printf("larger of two integers %d and %d is %d\n",
num1, num2, larger);
printf("User wanna continue, enter two integers...,(q to quit)\n");
}
return 0;
}
int largerof2ints(int n, int m)
{
return if (n > m) ? n : m;
}
Output of program as follows:
Program displays larger of two integers.
User, enter two integers...,(q to quit)
23
12
larger of two integers 23 and 12 is 23
User want to continue, enter two integers...,(q to quit)
67
1111
larger of two integers 67 and 1111 is 1111
User want to continue, enter two integers...,(q to quit)
q
In order to implement black-box and abstract data-types in context with functions, practice to place function definitions in a separate file, say file2.c, and all function headers and named constants in a separate user defined header, for example, fun_header.h. Remember that user specified header should be enclosed in double quotes to indicate that it exists in the current directory.
Considering the above information, we rewrite the above program below as,
/* fun_blackbox2.c -- program implements black-box programming */
/* reference about user defined header */
#include "fun_header.h"
int main(void)
{
int num1, num2, larger;
printf("\nProgram displays larger of two integers.\n");
printf("User, enter two integers...,(q to quit)\n");
while (scanf("%d %d", &num1, &num2) == 2) {
larger = largerof2ints(num1, num2);
printf("larger of two integers %d and %d is %d\n",
num1, num2, larger);
printf("User wanna continue, enter two integers...,(q to quit)\n");
}
return 0;
}
/* fun_def.c -- all function definitions are defined here */
int largerof2ints(int n, int m)
{
return if (n > m) ? n : m;
}
/*
* fun_header.h -- all function declarations and symbolic constants are
* declared in the header
*/
#include <stdio.h>
int largerof2ints(int, int);
How to compile a multi-file program on Linux or Unix system? let’s see the command for the above program, for example,
gcc fun_blackbox2.c fun_def.c on Linux
cc fun_blackbox2.c fun_def.c on Unix
Advantages of programming functions as black-boxes simplifies ‘main()’ function. ‘main()’ function includes only interfaces to calling functions, no prototypes, no defined constants and no function definitions. Further, functions can be shared across several other programs leading to reputability of functions.
Top
This C Tutorial Explains ADTs and Black Boxes in Context with Functions in C.
Many programmers like to think of a function as a “black box” defined in terms of the information that goes in (its input) and the value or action it produces (its output). What goes on inside the black box is not your concern, unless you are the one who has to write the function. For example, when you use ‘printf()’, you know that you have to give it a control string and, perhaps, some arguments. You also know what output ‘printf()’ should produce. You never have to think about the programming that went into creating ‘printf()’. Thinking of functions in this manner helps you concentrate on the program’s overall design rather than the details. Think carefully about what the function should do and how it relates to the program as a whole before worrying about writing the code. Let’s see an example,
#include <stdio.h>
int largerof2ints(int, int);
int main(void)
{
int num1, num2, larger;
printf("\nProgram displays larger of two integers.\n");
printf("User, enter two integers...,(q to quit)\n");
while (scanf("%d %d", &num1, &num2) == 2) {
larger = largerof2ints(num1, num2);
printf("larger of two integers %d and %d is %d\n",
num1, num2, larger);
printf("User wanna continue, enter two integers...,(q to quit)\n");
}
return 0;
}
int largerof2ints(int n, int m)
{
return if (n > m) ? n : m;
}
Output of program as follows:
Program displays larger of two integers.
User, enter two integers...,(q to quit)
23
12
larger of two integers 23 and 12 is 23
User want to continue, enter two integers...,(q to quit)
67
1111
larger of two integers 67 and 1111 is 1111
User want to continue, enter two integers...,(q to quit)
q
In order to implement black-box and abstract data-types in context with functions, practice to place function definitions in a separate file, say file2.c, and all function headers and named constants in a separate user defined header, for example, fun_header.h. Remember that user specified header should be enclosed in double quotes to indicate that it exists in the current directory.
Considering the above information, we rewrite the above program below as,
/* fun_blackbox2.c -- program implements black-box programming */
/* reference about user defined header */
#include "fun_header.h"
int main(void)
{
int num1, num2, larger;
printf("\nProgram displays larger of two integers.\n");
printf("User, enter two integers...,(q to quit)\n");
while (scanf("%d %d", &num1, &num2) == 2) {
larger = largerof2ints(num1, num2);
printf("larger of two integers %d and %d is %d\n",
num1, num2, larger);
printf("User wanna continue, enter two integers...,(q to quit)\n");
}
return 0;
}
/* fun_def.c -- all function definitions are defined here */
int largerof2ints(int n, int m)
{
return if (n > m) ? n : m;
}
/*
* fun_header.h -- all function declarations and symbolic constants are
* declared in the header
*/
#include <stdio.h>
int largerof2ints(int, int);
How to compile a multi-file program on Linux or Unix system? let’s see the command for the above program, for example,
gcc fun_blackbox2.c fun_def.c on Linux
cc fun_blackbox2.c fun_def.c on Unix
Advantages of programming functions as black-boxes simplifies ‘main()’ function. ‘main()’ function includes only interfaces to calling functions, no prototypes, no defined constants and no function definitions. Further, functions can be shared across several other programs leading to reputability of functions.
Top
08 Explain Recursion in C Programming with Examples
This C Tutorial Explains the concept Recursion in C Programming with examples.
C allows a function to call itself. This is called Recursion. Let’s see an example,
void main(void)
{
main();
}
What happens when the program is compiled and run, main() function’s type, here for ex., is void and it doesn’t take any arguments. main() has only one statement, main(), calling to itself. When main() starts executing, it calls to itself, let’s say level 1, then level 1 main() executes and calls level 2 main() and so on indefinitely until program is aborted automatically on Linux.
Let’s take one more simple example,
/* down_recur.c -- program counts down ways */
#include <stdio.h>
void countdown(int);
int main(void)
{
int num = 100;
countdown(num);
return 0;
}
void countdown(int count)
{
if (count >= 1) {
printf("%d\n", count);
countdown(count--);
}
}
Now, let’s analyse the above program. Function ‘countdown()’ doesn’t return any value to its calling function as its type is void. However it takes an integer value. Will ‘countdown()’, too, during its successive recursive calls to itself, end up in a program abnormally aborted or should this behave differently this time? let’s unravel this. When ‘main()’ called the ‘countdown()’ with integer ‘num’ with value 100, for the first time, say level 1, ‘countdown()’ executes instructions defined therein. Value of ‘count’ is tested and if condition founds to be true, displays current value of ‘count’ which is 100 and ‘countdown()’ calls to itself with ‘count’ minus 1. This is level 2 call to ‘countdown()’. Again ‘countdown()’ executes, tested value of ‘count’ if >= 1, if true, displays the current value, 99, of ‘count’ before ‘countdown()’ is again called to itself with ‘count’ minus 1. This is level 3 call. This way ‘countdown()’ recursively calls to itself until ‘count’ reduced to 0. At this point, test condition
if (count >= 1) {
printf("%d\n", count);
countdown(--count);
/* interesting when count-- is used! */
}
failed and statements within if construct didn’t execute. So ‘countdown()’ further didn’t call to itself. Since type of recursive function ‘countdown()’ was void, every recursive call, beginnig from when recursion stopped, to the function doesn’t return any value to its previous level of recursive call.
Also, notice that each time ‘countdown()’ is called recursively, formal integer argument ‘count’ is created and it obtained copy of integer from previous recursive call of function ‘countdown()’. Therefore, there were created 101 integers with same name ‘count’, last one with value 0. Each ‘count’ is private/local to its function and so there’s no same name conflict.
Therefore, we observed here that we must design some condition in recursive function which during successive recursive calls eventually fails and recursion stops. If type of recursive function isn’t void, each recursive call only after recursion stopped returns that type of value to its calling function.
Top
This C Tutorial Explains the concept Recursion in C Programming with examples.
C allows a function to call itself. This is called Recursion. Let’s see an example,
void main(void)
{
main();
}
What happens when the program is compiled and run, main() function’s type, here for ex., is void and it doesn’t take any arguments. main() has only one statement, main(), calling to itself. When main() starts executing, it calls to itself, let’s say level 1, then level 1 main() executes and calls level 2 main() and so on indefinitely until program is aborted automatically on Linux.
Let’s take one more simple example,
/* down_recur.c -- program counts down ways */
#include <stdio.h>
void countdown(int);
int main(void)
{
int num = 100;
countdown(num);
return 0;
}
void countdown(int count)
{
if (count >= 1) {
printf("%d\n", count);
countdown(count--);
}
}
Now, let’s analyse the above program. Function ‘countdown()’ doesn’t return any value to its calling function as its type is void. However it takes an integer value. Will ‘countdown()’, too, during its successive recursive calls to itself, end up in a program abnormally aborted or should this behave differently this time? let’s unravel this. When ‘main()’ called the ‘countdown()’ with integer ‘num’ with value 100, for the first time, say level 1, ‘countdown()’ executes instructions defined therein. Value of ‘count’ is tested and if condition founds to be true, displays current value of ‘count’ which is 100 and ‘countdown()’ calls to itself with ‘count’ minus 1. This is level 2 call to ‘countdown()’. Again ‘countdown()’ executes, tested value of ‘count’ if >= 1, if true, displays the current value, 99, of ‘count’ before ‘countdown()’ is again called to itself with ‘count’ minus 1. This is level 3 call. This way ‘countdown()’ recursively calls to itself until ‘count’ reduced to 0. At this point, test condition
if (count >= 1) {
printf("%d\n", count);
countdown(--count);
/* interesting when count-- is used! */
}
failed and statements within if construct didn’t execute. So ‘countdown()’ further didn’t call to itself. Since type of recursive function ‘countdown()’ was void, every recursive call, beginnig from when recursion stopped, to the function doesn’t return any value to its previous level of recursive call.
Also, notice that each time ‘countdown()’ is called recursively, formal integer argument ‘count’ is created and it obtained copy of integer from previous recursive call of function ‘countdown()’. Therefore, there were created 101 integers with same name ‘count’, last one with value 0. Each ‘count’ is private/local to its function and so there’s no same name conflict.
Therefore, we observed here that we must design some condition in recursive function which during successive recursive calls eventually fails and recursion stops. If type of recursive function isn’t void, each recursive call only after recursion stopped returns that type of value to its calling function.
Top
09 Is Recursion in C Different from Iteration Implemented Through Loops
Question: Is Recursion in C Different from Iteration Implemented Through Loops
Answer: To understand the difference between Recursion and Iteration implemented through loops, let’s first consider a simple program,
/* down_recur.c -- program counts down ways */
#include <stdio.h>
void countdown(int);
int main(void)
{
int num = 100;
countdown(num);
return 0;
}
void countdown(int count)
{
if (count >= 1) {
printf("%d\n", count);
countdown(count--);
}
}
Observe the output below, for simplicity, some portion of output is displayed, dots … indicate numbers in sequence,
100
99
98
97
96
95
.
.
.
6
5
4
3
2
1
Now, we try to perform the same job using loops, for example,
/*
* diff_recur_and_loops.c -- program displays integers 100 through 1 using
* loops
*/
#include <stdio.h>
int main(void)
{
int num = 100;
for (; num >= 1; num--) {
printf("%d\n", num);
}
return 0;
}
Output of the above program is as follows,
100
99
98
97
96
95
.
.
.
6
5
4
3
2
1
Both outputs are same. Where’s the difference in two approaches? let’s unravel this, now.
Notice that each time ‘countdown()’ is called recursively, formal integer argument ‘count’ is created and it obtained copy of integer from previous recursive call of function ‘countdown()’. Therefore, there were created 101 integers with same name ‘count’, last one with value 0. Each ‘count’ is private/local to its function and so there’s no same name conflict. We know in C that when a function is called, it’s allotted a portion of temporary area called STACK for placing up there its private/local arguments, return addresses of calling functions etc. This way, for 101 recursive function calls to itself, there were created 101 STACKS, one for each function. Further, this caused recursion slow. Also, recursion exhausts systems important memory resources and might cause the program to abnormally aborted.
Also, we observed here that we must design some condition in recursive function which during successive recursive calls eventually fails and recursion stops. If type of recursive function isn’t void, each recursive call only after recursion stopped returns that type of value to its calling function.
Now, we discuss same problem using loops. Here, we implemented for loop, just took one integer variable ‘num’ and performed the job efficiently with single variable. There were no trade-offs involved in creating separate STACK for every Iteration instead iterations used the modified single integer and performed the task efficiently.
However, b>we conclude that aside from creating its own local variables during each recursive call, recursion and loops are alike!
Top
Question: Is Recursion in C Different from Iteration Implemented Through Loops
Answer: To understand the difference between Recursion and Iteration implemented through loops, let’s first consider a simple program,
/* down_recur.c -- program counts down ways */
#include <stdio.h>
void countdown(int);
int main(void)
{
int num = 100;
countdown(num);
return 0;
}
void countdown(int count)
{
if (count >= 1) {
printf("%d\n", count);
countdown(count--);
}
}
Observe the output below, for simplicity, some portion of output is displayed, dots … indicate numbers in sequence,
100
99
98
97
96
95
.
.
.
6
5
4
3
2
1
Now, we try to perform the same job using loops, for example,
/*
* diff_recur_and_loops.c -- program displays integers 100 through 1 using
* loops
*/
#include <stdio.h>
int main(void)
{
int num = 100;
for (; num >= 1; num--) {
printf("%d\n", num);
}
return 0;
}
Output of the above program is as follows,
100
99
98
97
96
95
.
.
.
6
5
4
3
2
1
Both outputs are same. Where’s the difference in two approaches? let’s unravel this, now.
Notice that each time ‘countdown()’ is called recursively, formal integer argument ‘count’ is created and it obtained copy of integer from previous recursive call of function ‘countdown()’. Therefore, there were created 101 integers with same name ‘count’, last one with value 0. Each ‘count’ is private/local to its function and so there’s no same name conflict. We know in C that when a function is called, it’s allotted a portion of temporary area called STACK for placing up there its private/local arguments, return addresses of calling functions etc. This way, for 101 recursive function calls to itself, there were created 101 STACKS, one for each function. Further, this caused recursion slow. Also, recursion exhausts systems important memory resources and might cause the program to abnormally aborted.
Also, we observed here that we must design some condition in recursive function which during successive recursive calls eventually fails and recursion stops. If type of recursive function isn’t void, each recursive call only after recursion stopped returns that type of value to its calling function.
Now, we discuss same problem using loops. Here, we implemented for loop, just took one integer variable ‘num’ and performed the job efficiently with single variable. There were no trade-offs involved in creating separate STACK for every Iteration instead iterations used the modified single integer and performed the task efficiently.
However, b>we conclude that aside from creating its own local variables during each recursive call, recursion and loops are alike!
Top
10 Which is more Efficient in C Programming – Recursion OR Iteration?
Question: Which is more Efficient in C Programming – Recursion OR Iteration?
Answer: In general, recursion is slow, exhausting computer’s memory resources while iteration performs on the same variables and so is efficient. But recursion on the other hand, in some situations, offers convenient tool than iterations. Let’s take an example of a program below which converts integers to binary and displays them.
/*
* int2bin_thru_recur.c -- program converts integer to binary using
* recursion
*/
#include <stdio.h>
void int2bin(int);
int main(void)
{
int num;
printf("Program converts integer to binary...\n");
printf("Enter some positive integer: (q to quit)\n");
while (scanf("%d", &num) == 1) {
int2bin(num);
printf("\nEnter some positive integer: (q to quit)\n");
}
printf("Thank you!\n");
return 0;
}
void int2bin(int n)
{
int bit;
if (n >= 2) {
bit = n % 2;
int2bin(n / 2);
printf("%d", bit);
return;
}
else {
bit = n;
printf("%d", bit);
return; /* function type is void */
}
}
Output of the program when compiled on Linux System.
Program converts integer to binary...
Enter some positive integer: (q to quit)
12
1100
Enter some positive integer: (q to quit)
44
101100
Enter some positive integer: (q to quit)
123
1111011
Enter some positive integer: (q to quit)
654
1010001110
Enter some positive integer: (q to quit)
q
Thank you!
While same job when performs using loop requires, in one of approaches, all previous values to be stored in an array and print the values in reverse order.
Top
Question: Which is more Efficient in C Programming – Recursion OR Iteration?
Answer: In general, recursion is slow, exhausting computer’s memory resources while iteration performs on the same variables and so is efficient. But recursion on the other hand, in some situations, offers convenient tool than iterations. Let’s take an example of a program below which converts integers to binary and displays them.
/*
* int2bin_thru_recur.c -- program converts integer to binary using
* recursion
*/
#include <stdio.h>
void int2bin(int);
int main(void)
{
int num;
printf("Program converts integer to binary...\n");
printf("Enter some positive integer: (q to quit)\n");
while (scanf("%d", &num) == 1) {
int2bin(num);
printf("\nEnter some positive integer: (q to quit)\n");
}
printf("Thank you!\n");
return 0;
}
void int2bin(int n)
{
int bit;
if (n >= 2) {
bit = n % 2;
int2bin(n / 2);
printf("%d", bit);
return;
}
else {
bit = n;
printf("%d", bit);
return; /* function type is void */
}
}
Output of the program when compiled on Linux System.
Program converts integer to binary...
Enter some positive integer: (q to quit)
12
1100
Enter some positive integer: (q to quit)
44
101100
Enter some positive integer: (q to quit)
123
1111011
Enter some positive integer: (q to quit)
654
1010001110
Enter some positive integer: (q to quit)
q
Thank you!
While same job when performs using loop requires, in one of approaches, all previous values to be stored in an array and print the values in reverse order.
Top