You are on page 1of 3

Birla Institute of Technology & Science, Pilani, Hyderabad

Campus First Semester 2021-2022


Computer Programming [CS F111]

Lab-sheet 6
Functions & Recursion
Functions break large computing tasks into smaller ones, and enable people to build on what
others have done instead of starting over from scratch. C program may consist of several small
functions. A program may reside in one or more source files. Source files may be compiled
separately and loaded together, along with previously compiled functions from libraries.

➢ Each function definition has the form:


return-type function-name (argument declaration)
{
declarations and statements
}

Note:
1. All parts except function-name and () are non-mandatory.
2. If return type is omitted, int is assumed (will give warning).
3. Function may not be declared before use (in C89/90). However, a prototype declaration is
must for C99. However, it is always a best practice to declare a function before use, so as to
perform a strict type-checking. A prototype declaration is terminated by a semi-colon(;). 4. All
automatic and register variables must be initialized within a function.

➢ A function may return a value using


return expression;
➢ The expression will be converted to return-type of the function.

➢ A function is called by its name followed by a list of argument which must match the

definition. variable-name=function-name(arg1, arg2,…);

Type the following program and observe how it is working:

void message(); //Function Declaration #include<stdio.h>


int main() {
printf(“I am in function main()\n”);
message(); /* Function message() is called in function main()
*/
printf(“I return back from the function message()\n”);
return 0;
}
void message() /* Function to print a message. It does not return
anything and does not take any parameters as input*/ { printf(“I
am in function message\n”); return;
}

Parameter passing:

➢ A Parameter is the symbolic name for "data" that goes into a function. There are two ways to
pass parameters in C: (i) Pass by Value, (ii) Pass by Reference.

(i) Pass by Value: pass by Value, means that a copy of the data is made and stored by way
of the name of the parameter. Any changes to the parameter have NO effect on data in
the calling function.
(ii) Pass by Reference: a reference parameter "refers" to the original data in the calling
function. Thus any changes made to the parameter are ALSO MADE TO THE ORIGINAL
variable. Reference to a variable may be passed using ampersand operator (&) function-
name(&param1, &param2,… )

The ampersand (&) is the syntax to tell C that any changes made to the parameter also
modify the original variable containing the data.

Example: Write a program to swap two numbers.

➢ Recursion: C functions may be used recursively; that is a function may call itself either
directly or indirectly. When a function calls itself recursively, each invocation gets a fresh set
of all the automatic variables, independent of the previous set. Recursion may provide no
saving in storage, since somewhere a stack of the values being processed must be
maintained. Nor will it be faster.

➢ The C Preprocessor: C Preprocessor is just a text substitution tool and it instructs the
compiler to do required pre-processing before the actual compilation. All preprocessor
commands begin with a hash symbol (#)

Example: #define MAX_ARRAY_LENGTH 100


The C preprocessor is not a part of compilation process.
Some of the pre-processor directives are #define, #include, #undef, #ifdef, #ifndef, etc.

#include includes a contents of the specified file. Example:


#include<stdio.h> #define is a macro substitution. Format: #define name
replacement-text Note:
1. The scope of #define ends with the current source file.
2. Replacement-text may be continued to several lines by placing a \ at the end of each line.
#define message_for(a, b) \
printf(#a " and " #b ": We love you!\n")
3. One must be careful while defining macros. For example, consider what will happen if
#define square(x) x*x is invoked as square(z+1)!
➢ Types of variables: extern, static, automatic, register.
➢ Extern Variables: A C program consists of a set of external objects, which are either
variables or functions. The adjective “external” is used in contrast to “internal”, which
describes the arguments and variables defined inside functions. External variables are
defined outside of any function, and are thus potentially available to many functions. External
variables are globally accessible. They provide an alternative to function arguments and
return values for communicating data between functions. External variables must be used
judiciously. The scope of the external variable or a function lasts from the point it is
declared to the end of file being compiled. Must be declared before using it using the
extern keyword. The extern
variables may be present in the same source file or different file. There should be only one
definition of extern variables. Other source files trying to use this variable has to declare them
before using them.
➢ Static variables: The static declaration to a variable causes the value of that variable to
remain intact even after control goes out of its scope. Format:
static data-type var-name=value;
Note:
1. Static variables should be initialized. Static variables (like global variables) are initialized
as 0 if not initialized explicitly. Should be initialized only using constant expressions. 2. A
static int variable remains in memory while the program is running. A normal or auto variable
is destroyed when a function call where the variable was declared is over. 3. Static variables
are allocated memory in data segment, not stack segment.

Programs:
1. Write a C program that takes an integer number as a parameter and returns the reverse of
it. Use the following function in your program

int reverse(int n); //Function declaration

2. Write a program to print the factorial using recursion.


3. Write a program to find the GCD of a number using Euclid’s algorithm
(Hint: GCD(a,0) = a, GCD (a,b)=GCD(b,a%b), if a>=b else, first swap a and b. Stop when
b=0. Output=a)
4. Write a program to find Fibonacci series and numbers using recursion.
5. Write a program to find 2’s complement of a number.
6. Write a recursive function to calculate xn.

You might also like