Welcome to our deep dive into C Function Examples! This lesson is designed for both beginners and intermediate learners, so let's start from the ground up. By the end, you'll be able to write, understand, and debug various C functions like a pro. 💡 Pro Tip: Feel free to pause and try out the examples as we go along!
Functions are self-contained blocks of code designed to perform a specific task. They make our programs more organized, modular, and reusable.
In C, we can create our own functions, or we can use built-in functions provided by the language.
Let's create our first function, which will display a greeting message.
#include <stdio.h>
void greet() {
printf("Hello, World!\n");
}
int main() {
greet(); // Call the greet function
return 0;
}Here's a breakdown of the code:
void greet() - This line declares a new function called greet, which doesn't return any value (indicated by the void keyword).printf("Hello, World!\n"); - Inside the function, we're using the printf function from the standard library to output a message.main() - This is the main function where our program starts execution.greet(); - We're calling the greet function we defined earlier.Try running this code and see the output for yourself!
Functions can also accept parameters, allowing them to accept and use input. Here's an example where we create a function to calculate the area of a rectangle:
#include <stdio.h>
double areaRectangle(double length, double width) {
double area = length * width;
return area;
}
int main() {
double length = 5.0;
double width = 3.0;
double area = areaRectangle(length, width);
printf("The area of the rectangle is: %f\n", area);
return 0;
}In this example:
double areaRectangle(double length, double width) - We're declaring a function called areaRectangle that takes two double parameters: length and width.double area = length * width; - Inside the function, we calculate the area of the rectangle using multiplication.double area = areaRectangle(length, width); - In the main function, we call areaRectangle with the length and width variables and assign the returned area to a new variable.So far, we've seen functions that don't return any value (using void). However, we can also make functions return values. In our previous example, we returned the calculated area as a double.
#include <stdio.h>
int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
int main() {
int num1 = 10;
int num2 = 20;
int maxNum = max(num1, num2);
printf("The maximum number is: %d\n", maxNum);
return 0;
}Here, we've created a max function that takes two int parameters and returns the maximum of the two. In the main function, we call the max function with two integer variables and store the returned maximum in a new variable.
Variables in C have a specific scope, which determines where they can be accessed. We'll cover variable scope in more detail in a future lesson, but for now, let's see how function scope affects our code:
#include <stdio.h>
void greet(char* name) {
printf("Hello, %s!\n", name);
}
int main() {
char* name = "John";
greet(name);
printf("Hello, World!\n");
return 0;
}In this example, the name variable is defined within the main function and passed as a parameter to the greet function. The greet function can access the name parameter, but the variable name is not visible inside the greet function.
What does the `void` keyword indicate in a C function?
Keep learning, and you'll master C functions in no time! 🚀
Stay tuned for upcoming lessons on advanced C functions, pointers, and more! 💡 Pro Tip: Practice makes perfect, so don't hesitate to write and experiment with your own functions.