A Function is a separate module,sub-program procedure which is usually written after the main function and is capable of performing a small complete independent job.
In C,there are two types of function-:
Function serve two purposes-:
- They allow a programmer to say ," This piece of code does a specific job which stand by itself and should not be mixed up with anything else.
- They make a block of code reuse-able , since a function can be reused in different context ,without repeating parts of the program text.
- Pre-defined/ Library Function-: They are the parts of the C Language,means they are present in any of the various headers,that can be included in the program.
The function getch() and clrscr() are present in <conio.h>
- User-define Function-: The function which are created by the user to accomplish a particular task is known as user-define function.
While using a user-define function in a program it is must to declare its prototype.
Syntax-: return type function name(data type - - -);
Where,
- return type-: Any data type can be written here,This data type represent the type of value , returning by the function .If a function does not return any value ,"void" should be written here.
- function name-: A name given to function to identify it ,any user define word can be written here (identifier).
- data type-: These data type represent the type of the variables value send to the function ,while calling it.
Examples-: int add_value(int,float);
void null();
swap_value(int,int);
The Function prototype is required to inform the compiler regarding the user - define function ,in-advance .So that the compiler could may the necessary arrangement into the memory for its execution.
Example-:
#include<stdio.h>
#include<conio.h>
main()
{ int a=0,b=0,s=0;
printf("Enter the value of a and b");
scanf("%d %d ",&a,&b);
s=sum_value(a,b);
printf("\n sum= %d",s);
getch();
}
int sum_value(int a, int b)
{ int s=0;
s=a+b;
return s;
}
Output-:
Enter the value of a and b 10
5
sum=15
Points To Remember-:
- A function may be called or invoked ,either from main or other parts of program.
- A function is invoked / called by writing its name along with the list of actual arguement enclosed within the pair of parenthesis ().
- A function whenever invoked usually return , a single result to the invoking program ,a function may or may not return the result ,to the calling function or it may return more than one result.
- A function cannot be defined inside the body of another function but can be called.
- Any C program contains at least one function ,though there is no limit of the no. of function that might be present in a C program ,if a program have only one function then it must be main().
- A function may or may not return value to any calling function ,if it does it is done through "return statement" , The return statement should be the last executable statement of any function.
- In a function ,two return statement should never occur sucessively ,a function may have more than one return statement ,this situation arises when the value return is based on certain conditions.
<!DOCTYPE html>
<html>
<head>
<title>Search Page</title>
</head>
<body>
<h1>Search Data</h1>
<form method="get" action="search_results.php">
<input type="text" name="search_query" placeholder="Enter your search query">
<button type="submit">Search</button>
</form>
</body>
</html>
<?php
// Establish database connection (Replace the placeholders with your database credentials)
$servername = "your_servername";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Handle search query
if (isset($_GET['search_query'])) {
$search_query = $_GET['search_query'];
// Construct SQL query with a WHERE clause for searching
$sql = "SELECT * FROM your_table_name WHERE column_name LIKE '%$search_query%'";
// Execute the query
$result = $conn->query($sql);
// Display the search results
echo "<h2>Search Results:</h2>";
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
// Display relevant data from the fetched row
echo "Data: " . $row['column_name'] . "<br>";
}
} else {
echo "<p>No results found.</p>";
}
}
$conn->close();
?>
learn function easily ...
ReplyDelete