Functions in C Programming
Functions in C Programming
main()
func1()
{
{
…………..
………….. Statement Block;
func1(); }
…………
………..
return 0;
}
© Oxford University Press 2018. All rights reserved.
Introduction (Contd.)
• It is not necessary that the main() can call only one function, it can call as many functions as it
wants and as many times as it wants. For example, a function call placed within a for loop,
while loop, or do-while loop may call the same function multiple times until the condition
holds true.
• It is not that only the main() can call another functions. Any function can call any other
function. In the figure, one function calls another, and the other function in turn calls some
other function.
• Understanding, coding, and testing multiple separate functions are far easier than doing the same for
one large function.
• If a big program has to be developed without the use of any function (except main()), then there will
be
countless lines in the main() function.
• All the libraries in C contain a set of functions that the programmers are free to use in their programs.
These functions have been prewritten and pre-tested, so the programmers use them without worrying
about their code details. This speeds up program development.
• When a called function returns some result back to the calling function, it is said to return that result.
• The calling function may or may not pass parameters to the called function. If the called function
accepts arguments, the calling function will pass parameters, else not.
• main() is the function that is called by the operating system and, therefore, it is supposed to return the
result of its processing to the operating system.
• The general format for declaring a function that accepts some arguments and returns some value as
result can be given as:
return_data_type function_name(data_type variable1, data_type variable2,…);
• Function definition consists of a function header that identifies the function, followed by the body of the
function containing the executable code for that function.
• When a function is defined, space is allocated for that function in the memory.
• The syntax of a function definition can be given as:
………….
statements
………….
return( variable);
}
• The number and the order
of arguments in the
function header must be
same as that given in
function © Oxford University Press 2018. All rights reserved.
Function
Call
• The function call statement invokes the function.
• When a function is invoked, the Compiler jumps to the called function to execute the statements that
are a part of that function.
• Once the called function is executed, the program control passes back to the calling function.
• The return statement is used to terminate the execution of a function and return control to the calling
function. When the return statement is encountered, the program execution resumes in the calling
function at the point immediately following the function call.
• Programming Tip: It is an error to use a return statement in a function that has void as its return type.
• A return statement may or may not return a value to the calling function. The syntax of return statement
can be given as
return <expression> ;
• The value of expression, if present, is returned to the calling function. However, in case expression is
omitted, the return value of the function is undefined.
• By default, the return type of a function is int.
• For functions that have no return statement, the control automatically returns to the calling function
after the last statement of the called function is executed.
• There are two ways in which arguments or parameters can be passed to the called function.
Call by value in which values of the variables are passed by the calling function to the called function.
Call by reference in which address of the variables are passed by the calling function to the called
function.
Passing parameters to
function
• In call by reference, we declare the function parameters as references rather than normal
variables. When this is done, any changes made by the function to the arguments it received are
visible in the calling function.
• To indicate that an argument is passed using call by reference, an asterisk symbol (*) is placed after the
type in the parameter list. This way, changes made to that parameter in the called function body will
then be reflected in its value in the calling function.
Variable
Scope
• Function scope is applicable only with goto label names. That is the programmer cannot have the same
label name inside a function.
• To allow a variable to have file scope, declare that variable with the static keyword before specifying its
data type:
• A global static variable can be used anywhere from the file in which it is declared but it is not accessible
by any other file.
• Such variables are useful when a programmer writes his or her own header files.
• The storage class of a variable defines the scope (visibility) and lifetime of variables and/or functions
declared within a C Program.
• In addition to this, the storage class gives the following information about the variable or the
function.
It is used to determine the part of memory where storage space will be allocated for that variable or
function (whether the variable/function will be stored in a register or in RAM).
It specifies how long the storage allocation will continue to exist for that function or variable.
It specifies the scope of the variable or function. That is, the part of the C program in which the variable
name is visible or accessible.
It specifies whether the variable or function has internal, external, or no linkage.
It specifies whether the variable will be automatically initialized to zero or to any indeterminate value.
Local:
Accessible within the
Accessible within
Accessible within the all program files Accessible within the function or block in
Accessibility function or block in which that are a part of function or block in which it is declared
it is declared which it is declared Global: Accessible
the program within the program in
which it is declared
whether the function calls itself directly or indirectly (direct or indirect recursion)
Recursion
• A function is said to be indirectly recursive if it contains a call to another function which ultimately calls
it. Look at the functions given below.
• These two functions are indirectly recursive as they both call each other.
• A recursive function is said to be tail recursive if no operations are pending to be performed when the
recursive function returns to its caller.
• That is, when the called function returns, the returned value is immediately returned from the calling
function.
• Tail recursive functions are highly desirable because they are much more efficient to use as in their
case, the amount of information that has to be stored on the system stack is independent of the
number of recursive calls.
• Recursive functions can also be characterized depending on whether recursion grows in a linear fashion
or forms a tree structure.
• In simple words, a recursive function is said to be linearly recursive when no pending operation involves
another recursive call to the function. For example, the factorial function is linearly recursive as the
pending operation involves only multiplication to be performed and does not involve another call to
Fact.
• On the contrary, a recursive function is said to be tree recursive (or non-linearly recursive) if the pending
operation makes another recursive call to the function. For example, the Fibonacci function (Fib) in
which the pending operations recursively call the Fib function.
• Pros: Recursive solutions often tend to be shorter and simpler than non-recursive ones.
Code is clearer and easier to use.
Using a recursive function takes more memory and time to execute as compared to its non-recursive
counterpart.
It is difficult to find bugs, particularly when using global variables.
A B C
A B C
If there is only one ring, then simply move the ring from source to the destination
A B C
A B C A B C
If there are two rings, then first move ring 1 to the spare pole and then
move ring 2 from source to the destination. Finally move ring 1 from the
source to the destination
A B C © Oxford University Press 2018. All rights reserved.
•
A B C
A B C
A B C
A B C A B C
A B C
A B C A B C