Here’s a brief overview and some key points for Unit-VI: Functions in C:
A function is a block of code that performs a specific task.
Types of functions:
Standard library functions: pre-defined functions like printf(), scanf(), etc.
User-defined functions: created by the programmer to solve specific tasks.
Function Declaration: A declaration tells the compiler about the function name, return type, and parameters.
Syntax:
return_type function_name(parameter_list); Example:
int add(int, int);
Function Definition: It provides the actual body of the function.
Syntax:
return_type function_name(parameter_list) { // function body } Example:
int add(int a, int b) {
return a + b;
}
Function Call: It is used to execute a function.
Syntax:
function_name(arguments); Example:
int result = add(5, 3);
Standard Function: Functions like printf(), scanf() which are pre-defined.
User-defined Function: Functions created by the programmer to perform specific tasks.
Parameter Passing refers to the method by which data is passed into a function.
Call by Value: In this method, the actual parameter is passed to the function as a copy. Changes to the parameter do not affect the actual argument.
Example:
void add(int x, int y) {
x = x + y;
}
Call by Reference: In this method, the address of the actual parameter is passed. Changes made in the function will affect the actual argument.
Example:
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
Scope refers to the region of the program where a variable is accessible.
Local Scope: Variables declared inside a function or block and are only accessible within that function/block.
Global Scope: Variables declared outside all functions and are accessible throughout the program.
Storage classes define the lifetime and visibility of variables.
auto: Default storage class for local variables (auto variables are local to the function).
register: Used to store variables in CPU registers for fast access.
static: Retains the value of the variable between function calls (both inside functions and global scope).
extern: Used to declare a variable that is defined outside the current function or file.
Recursion is when a function calls itself to solve a problem.
A base case is required to prevent infinite recursion.
Example:
int factorial(int n) {
if(n == 0) return 1;
return n * factorial(n - 1);
}
1. Call by Value Example:
#include <stdio.h>
void add(int a, int b) {
a = a + b; // This change will not affect the original variables
printf("Sum inside function: %d\n", a);
}
int main() {
int x = 5, y = 3;
add(x, y); // Call by value
printf("Sum outside function: %d\n", x); // Original value of x remains unchanged
return 0;
}
2. Call by Reference Example:
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5, y = 3;
swap(&x, &y); // Call by reference
printf("Swapped values: x = %d, y = %d\n", x, y); // Original values are swapped
return 0;
}
3. Recursion Example (Factorial Function):
#include <stdio.h>
int factorial(int n) {
if(n == 0) return 1; // Base case
return n * factorial(n - 1); // Recursive call
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
Functions in C help in organizing the code into manageable blocks, improving readability and reusability.
Understanding parameter passing, recursion, and scope is crucial for writing efficient C programs.
Would you like any detailed examples or explanations of specific topics?
Here’s a brief overview and some key points for Unit-VI: Functions in C:
A function is a block of code that performs a specific task.
Types of functions:
Standard library functions: pre-defined functions like printf(), scanf(), etc.
User-defined functions: created by the programmer to solve specific tasks.
Function Declaration: A declaration tells the compiler about the function name, return type, and parameters.
Syntax:
return_type function_name(parameter_list); Example:
int add(int, int);
Function Definition: It provides the actual body of the function.
Syntax:
return_type function_name(parameter_list) { // function body } Example:
int add(int a, int b) {
return a + b;
}
Function Call: It is used to execute a function.
Syntax:
function_name(arguments); Example:
int result = add(5, 3);
Standard Function: Functions like printf(), scanf() which are pre-defined.
User-defined Function: Functions created by the programmer to perform specific tasks.
Parameter Passing refers to the method by which data is passed into a function.
Call by Value: In this method, the actual parameter is passed to the function as a copy. Changes to the parameter do not affect the actual argument.
Example:
void add(int x, int y) {
x = x + y;
}
Call by Reference: In this method, the address of the actual parameter is passed. Changes made in the function will affect the actual argument.
Example:
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
Scope refers to the region of the program where a variable is accessible.
Local Scope: Variables declared inside a function or block and are only accessible within that function/block.
Global Scope: Variables declared outside all functions and are accessible throughout the program.
Storage classes define the lifetime and visibility of variables.
auto: Default storage class for local variables (auto variables are local to the function).
register: Used to store variables in CPU registers for fast access.
static: Retains the value of the variable between function calls (both inside functions and global scope).
extern: Used to declare a variable that is defined outside the current function or file.
Recursion is when a function calls itself to solve a problem.
A base case is required to prevent infinite recursion.
Example:
int factorial(int n) {
if(n == 0) return 1;
return n * factorial(n - 1);
}
1. Call by Value Example:
#include <stdio.h>
void add(int a, int b) {
a = a + b; // This change will not affect the original variables
printf("Sum inside function: %d\n", a);
}
int main() {
int x = 5, y = 3;
add(x, y); // Call by value
printf("Sum outside function: %d\n", x); // Original value of x remains unchanged
return 0;
}
2. Call by Reference Example:
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5, y = 3;
swap(&x, &y); // Call by reference
printf("Swapped values: x = %d, y = %d\n", x, y); // Original values are swapped
return 0;
}
3. Recursion Example (Factorial Function):
#include <stdio.h>
int factorial(int n) {
if(n == 0) return 1; // Base case
return n * factorial(n - 1); // Recursive call
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
Functions in C help in organizing the code into manageable blocks, improving readability and reusability.
Understanding parameter passing, recursion, and scope is crucial for writing efficient C programs.
Would you like any detailed examples or explanations of specific topics?
Here are the short notes on Functions in C, covering basic types of functions, function call, parameter passing, recursion, and storage classes:
Definition: A function is a block of code that performs a specific task. Functions help break down complex programs into smaller, manageable parts.
Function Declaration: The function prototype declares the function’s name, return type, and parameters.
return_type function_name(parameter_list);
Function Definition: The actual body of the function that implements the logic.
return_type function_name(parameter_list) {
// function body
}
Function Call: To execute a function, it needs to be called from the main program or other functions.
function_name(parameters);
Basic Types:
Library Functions: Predefined functions in C (e.g., printf(), scanf()).
User-Defined Functions: Functions created by the programmer to perform specific tasks.
Types of Function Calls:
Call by Value: A copy of the actual argument is passed to the function.
Example: The original values remain unchanged in the calling function.
Call by Reference: The actual memory addresses of arguments are passed, so changes affect the original variables.
Example: Used for swapping values.
Parameter Passing:
Call by Value: Arguments are copied, and changes inside the function do not affect the original values.
Call by Reference: The function operates on the actual memory addresses of the arguments.
Defines the scope and lifetime of variables:
auto: Default for local variables, scope is the function, lifetime is until the function ends.
register: Stores variables in CPU registers for faster access (scope and lifetime same as auto).
static: Retains value between function calls. Initialized only once.
extern: Defines a variable or function outside the current file.
Definition: A function calling itself to solve smaller instances of a problem.
Base Case: Prevents infinite recursion by specifying a stopping condition.
Example: Factorial calculation using recursion.
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
#include <stdio.h>
// Function to add two numbers (Call by Value)
int add(int a, int b) {
return a + b;
}
// Function to swap two numbers (Call by Reference)
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
// Function to calculate factorial (Recursion)
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
int a = 10, b = 20;
printf("Sum of a and b: %d\n", add(a, b)); // Call by Value
swap(&a, &b); // Call by Reference
printf("After Swap: a = %d, b = %d\n", a, b);
printf("Factorial of 5: %d\n", factorial(5)); // Recursion
return 0;
}
Call by Value: The function works on copies of the arguments.
Call by Reference: The function works on the actual arguments using pointers.
Recursion: The function solves problems by calling itself.
Let me know if you'd like any specific topics explained further!
Here are some questions based on the Functions in C topic:
What is the difference between Call by Value and Call by Reference?
Explain the role of storage classes in C functions. What are the four storage classes in C?
What is recursion? Write a C program to calculate the factorial of a number using recursion.
What is the purpose of a function declaration and function definition in C? Provide examples.
How does a function return a value in C? Illustrate with an example.
What is scope of a variable in C? How does it relate to functions?
What is the base case in recursion? Why is it important?
What is the use of extern keyword in functions?
Write a C program to calculate the sum of two numbers using a function.
Write a C program to swap two numbers using Call by Reference.
Write a C program to find the Greatest Common Divisor (GCD) of two numbers using recursion.
Write a program to calculate the Fibonacci series using recursion.
Implement a function to check if a number is prime or not, using recursion.
Write a C program to demonstrate the use of storage classes auto, register, static, and extern.
Let me know if you'd like answers or explanations for any of these questions!