0% found this document useful (0 votes)
29 views

Functions

When a function is called, control transfers to the function, a stack frame is created to hold local variables and parameters, the code in the function executes, and any return value is returned before control returns to the caller and the stack frame is removed. The document also provides examples of a function prototype accepting different parameter types, and explanations of call by value and call by reference.

Uploaded by

Sk Samim Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Functions

When a function is called, control transfers to the function, a stack frame is created to hold local variables and parameters, the code in the function executes, and any return value is returned before control returns to the caller and the stack frame is removed. The document also provides examples of a function prototype accepting different parameter types, and explanations of call by value and call by reference.

Uploaded by

Sk Samim Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Functions :

1. What happens when a function is called?


When a function is called in a program, the following
sequence of events typically occurs:

1. Control Transfer: Program execution moves to the


function.
2. Local Variables and Stack Frame: Function creates a
stack frame with variables and information.
3. Parameter Passing: Arguments are passed to
function parameters.
4. Function Execution: Code inside the function is
executed.
5. Return Value: If applicable, a value is computed and
returned.
6. Control Return: Program control returns to the point
after the function call.
7. Stack Frame Cleanup: Function's stack frame is
removed, freeing resources.
2. write a function prototype which accepts an
integers, a float pointer, a string and a
structure pointer as a parameter

Certainly! Here's an example of a function prototype that


accepts an integer, a float pointer, a string, and a
structure pointer as parameters:

void myFunction(int myInteger, float


*myFloatPointer, char *myString, struct MyStructure
*myStructurePointer);

In this prototype, the function is named myFunction. It takes


four parameters:

1. myInteger is an integer parameter that accepts an integer


value.
2. myFloatPointer is a float pointer parameter that accepts
the address of a float variable.
3. myString is a character pointer parameter that accepts a
string (i.e., a pointer to the first character of a character
array).
4. myStructurePointer is a structure pointer parameter that
accepts the address of a structure variable of type
MyStructure.
3. explain call by value and call by reference with
examples

1. Call by Value: In this mechanism, a copy of the value of the


actual argument is passed to the formal parameter.
Changes made to the formal parameter within the
function do not affect the actual argument. Example:

2. Call by Reference: In this mechanism, the memory address


of the actual argument is passed to the formal parameter.
Changes made to the formal parameter within the
function affect the actual argument. Example:
3. C programme to count no of vowel in string
4. Swapping two numbers using functions
5. C Program to Find GCD of two Numbers
6. What is the purpose of return statement in a function?

The return statement in a function is used to


specify the value that the function will produce
as its result. It terminates the function's
execution, communicates the result back to
the caller, and allows the value to be used or
assigned for further processing.

7. What are the differences between recursion and iterations.


8.Find the HCF of a Numbers using Recursion in C
8. what is recursion

Recursion is a programming technique where a function calls itself to solve a problem


by breaking it down into smaller instances of the same problem. It involves defining a
base case to terminate the recursive calls and handles the problem in a self-
referential manner.

9.
10.Fibonacci Series Program in C Using Recursion
11. WAP in C for recursive way of calling factorial function. Explain with proper diagram the
various stack calls involved.
12. Which way is more efficient- recursive or iterative

The efficiency of recursive versus iterative solutions depends on various factors,


including the specific problem, the programming language, and the implementation
details. In general, iterative solutions tend to be more efficient in terms of both time
and space complexity compared to recursive solutions.

Recursive solutions often have higher overhead due to the repeated function calls,
memory allocation for the call stack, and potential function call stack limitations.
Recursive solutions may also lead to redundant computations or repeated work,
especially if the same subproblems are encountered multiple times.

Iterative solutions, on the other hand, usually have a more direct control flow,
avoiding the overhead of function calls and the associated stack operations. They can
often be optimized to use less memory and exhibit better performance
characteristics.

in conclusion, while iterative solutions tend to be more efficient in terms of performance,


recursive solutions can still be valuable in certain scenarios for their simplicity and readability.
13.

You might also like