Functions
Defining and calling functions, passing arguments, and returning values. Understanding function prototypes and scope.
Defining Functions in C
Functions are fundamental building blocks in C programming. They allow you to encapsulate a block of code that performs a specific task, making your code more modular, reusable, and easier to understand.
What is a Function?
A function is a self-contained block of code that performs a specific task. It can accept input values (parameters), process them, and return a result. Functions promote code reusability and improve program organization.
Syntax for Defining a Function in C
The syntax for defining a function in C follows this general structure:
return_type function_name(parameter_list) {
// Function body (code to be executed)
return return_value; // Optional, depending on return_type
}
Let's break down each part:
return_type
: Specifies the data type of the value the function will return. If the function doesn't return a value, you usevoid
as the return type.function_name
: A descriptive name that identifies the function. Choose names that clearly indicate what the function does.parameter_list
: A comma-separated list of parameters that the function accepts as input. Each parameter consists of a data type and a variable name. If the function doesn't accept any parameters, you can leave the parentheses empty, or explicitly specifyvoid
.{ }
: Curly braces enclose the function body, which contains the code that will be executed when the function is called.return return_value;
: (Optional) If the function has a non-void
return type, this statement returns a value of the specified type to the caller. Thereturn
statement also terminates the function's execution. If the return type is `void`, you do not need a `return` statement (though you *can* include a `return;` to exit the function early).
Examples of Function Definitions
1. A Function that Adds Two Integers
int add(int a, int b) {
int sum = a + b;
return sum;
}
This function, named add
, takes two integer parameters (a
and b
), calculates their sum, and returns the result as an integer.
2. A Function that Prints a Message (No Return Value)
void printMessage(char message[]) {
printf("%s\n", message);
}
This function, named printMessage
, takes a character array (string) as a parameter and prints it to the console. It has a void
return type, indicating that it doesn't return a value.
3. A Function that Returns the Maximum of Two Numbers
int findMax(int x, int y) {
if (x > y) {
return x;
} else {
return y;
}
}
This function, named findMax
, takes two integer parameters (x
and y
) and returns the larger of the two.
4. A Function with No Parameters
int getRandomNumber() {
// Seed the random number generator (only do this once in your program)
static int seeded = 0;
if (!seeded) {
srand(time(NULL)); // Seed using current time
seeded = 1;
}
return rand(); // Returns a pseudo-random integer
}
This function, named `getRandomNumber`, takes no parameters. It returns a random integer. Note the use of `static` to make the `seeded` variable persistent across calls, ensuring we only seed the random number generator once.
5. A Function with No Parameters and No Return Value
void doSomething() {
printf("Doing something...\n");
}
This function, named `doSomething`, takes no parameters and returns nothing. It simply prints a message to the console.
Key Takeaways
- Functions are essential for code organization and reusability.
- The
return_type
specifies the data type of the returned value. Usevoid
if the function doesn't return anything. - The
parameter_list
defines the input parameters of the function. - The function body contains the code that performs the function's task.
- The
return
statement is used to return a value from the function (if the return type is notvoid
).