Function declaration and function definition and function call illustrated with examples.
1. Function Declaration (Prototype)
Purpose:
A function declaration informs the compiler about the function name, return type, and parameters before it is used in the program. It helps the compiler check for errors during compilation.
Syntax:
return_type function_name(parameter_list);
Example:
int add(int, int);
This tells the compiler that there is a function named add which takes two integers and returns an integer.
2. Function Definition
Purpose:
A function definition contains the actual body of the function where the task or operations are performed.
Syntax:
return_type function_name(parameter_list)
{
// statements
}
Example:
int add(int a, int b)
{
int sum;
sum = a + b;
return sum;
}
3. Function Call
Purpose:
A function call is used to invoke or execute the function from another part of the program (usually from main()).
Syntax:
function_name(arguments);
Example:
result = add(5, 3);
Complete Example Program
#include <stdio.h>
int add(int, int); // function declaration
int main()
{
int result;
result = add(5, 3); // function call
printf(“Sum = %d”, result);
return 0;
}
int add(int a, int b) // function definition
{
return a + b;
}
Summary
| Concept | Purpose |
| Function Declaration | Informs the compiler about the function before use |
| Function Definition | Contains the actual code of the function |
| Function Call | Executes the function in the program |
Explain any 3 string handling functions using examples.
In C programming, string handling functions are predefined functions available in the string library (string.h)..
- strlen() – Find Length of a String
Purpose:
The strlen() function returns the number of characters in a string (excluding the null character \0).
Syntax:
strlen(string_name);
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = “Computer”;
printf(“Length of the string = %d”, strlen(str));
return 0;
}
Output:
Length of the string = 8
- strcpy() – Copy One String to Another
Purpose:
The strcpy() function copies the contents of one string into another string.
Syntax:
strcpy(destination, source);
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20], str2[] = “Hello”;
strcpy(str1, str2);
printf(“Copied string: %s”, str1);
return 0;
}
Output:
Copied string: Hello
- strcat() – Concatenate Two Strings
Purpose:
The strcat() function joins (concatenates) two strings by adding the second string to the end of the first string.
Syntax:
strcat(string1, string2);
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = “Good”;
char str2[] = ” Morning”;
strcat(str1, str2);
printf(“Concatenated string: %s”, str1);
return 0;
}
Output:
Concatenated string: Good Morning
- strcmp()
Purpose:
Used to compare two strings.
Syntax
strcmp(string1, string2);
Return Value
- 0 → Strings are equal
- < 0 → First string is smaller
- > 0 → First string is greater
Example
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = “Apple”;
char str2[] = “Apple”;
if(strcmp(str1, str2) == 0)
printf(“Strings are equal”);
else
printf(“Strings are not equal”);
return 0;
}
Summary
| Function | Purpose |
| strlen() | Finds the length of a string |
| strcpy() | Copies one string to another |
| strcat() | Joins two strings |
| strcmp() | Compares two strings |
What is recursion? Write a C program to display Fibonacci series using recursive
function.
Recursion
Recursion is a technique in programming where a function calls itself repeatedly until a base condition is satisfied.
It is commonly used to solve problems that can be broken down into smaller subproblems.
Example uses: factorial, Fibonacci series, tree traversal, etc.
C Program to Display Fibonacci Series Using Recursion
#include <stdio.h>
int fib(int n)
{
if(n == 0)
return 0;
else if(n == 1)
return 1;
else
return fib(n-1) + fib(n-2);
}
int main()
{
int i, n;
printf(“Enter the number of terms: “);
scanf(“%d”, &n);
printf(“Fibonacci Series:\n”);
for(i = 0; i < n; i++)
{
printf(“%d “, fib(i));
}
return 0;
}
Explanation
- The function fib() calls itself to calculate Fibonacci numbers.
- Base cases
- fib(0) = 0
- fib(1) = 1
- For other values:
fib(n) = fib(n-1) + fib(n-2) - A for loop calls the recursive function to print the series.
Example Output
Enter the number of terms: 6
Fibonacci Series:
0 1 1 2 3 5
Formal parameters and actual parameters Illustrated with an example
In C programming, parameters are variables used to pass data between a function and the calling program.
- Formal Parameters
Definition:
Formal parameters are the variables declared in the function definition that receive values from the calling function.
- Actual Parameters
Definition:
Actual parameters are the values or variables passed to the function when it is called.
Example Program
#include <stdio.h>
void add(int a, int b) // a and b are formal parameters
{
int sum = a + b;
printf(“Sum = %d”, sum);
}
int main()
{
int x = 5, y = 10;
add(x, y); // x and y are actual parameters
return 0;
}
Explanation
- a and b in the function add() are formal parameters because they are defined in the function.
- x and y in the statement add(x, y) are actual parameters because they pass the values to the function.
Summary
| Formal Parameters | Actual Parameters |
| Declared in the function definition | Passed in the function call |
| Receive values from actual parameters | Supply values to the function |
| Example: int a, int b | Example: x, y |
Calling function to a called function.
There are two types of parameter passing in C:
- Call by Value
- Call by Reference (Call by Address)
1. Call by Value
In call by value, a copy of the actual argument is passed to the function.
Changes made inside the function do not affect the original variable.
Example Program
#include <stdio.h>
void change(int x)
{
x = x + 10;
printf(“Value inside function: %d\n”, x);
}
int main()
{
int a = 5;
change(a);
printf(“Value in main function: %d\n”, a);
return 0;
}
Output
Value inside function: 15
Value in main function: 5
Explanation
- The value of a is copied to x.
- Changes made to x do not change a in the main function.
2. Call by Reference (Call by Address)
In call by reference, the address of the variable is passed to the function using pointers.
Changes made inside the function affect the original variable.
Example Program
#include <stdio.h>
void change(int *x)
{
*x = *x + 10;
}
int main()
{
int a = 5;
change(&a);
printf(“Value after function call: %d\n”, a);
return 0;
}
Output
Value after function call: 15
Explanation
- The address of a is passed to the function.
- The pointer x accesses the original variable using *x.
- Therefore, the change is reflected in a.
Summery
| Type | What is Passed | Effect on Original Variable |
| Call by Vaalue | Copy of value | No change |
| Call by Referwnce | Address of variable | Original value changes |