Ticker

6/recent/ticker-posts

Functions

 Chapter 7: Functions



 




Functions in C allow you to break down your program
into smaller, manageable parts. They promote code reusability and modular
design. A function is a block of code that performs a specific task and may
return a value. Let's explore how to define and use functions in C:



 



7.1
Function Declaration and Definition:



A function declaration specifies the function's
name, return type, and parameters (if any). The function definition provides
the implementation of the function.



 





return_type
function_name(parameter1, parameter2, ...) {



    // Function body



    // Code to be executed



    // ...



    return value; // (optional)



}





 



Example:



 





#include
<stdio.h>



//
Function declaration



int
add(int a, int b);



int
main() {



    int num1 = 5, num2 = 3;



    int sum = add(num1, num2); // Function call



    printf("Sum: %d\n", sum);



    return 0;



}



 



//
Function definition



int
add(int a, int b) {



    int result = a + b;



    return result;



}





 



 



The program defines a function `add()` that takes
two integer parameters and returns their sum. The `main()` function calls the
`add()` function and prints the result. Compile and run the program to see the
output.



 



7.2
Function Parameters:



Functions can have zero or more parameters.
Parameters act as placeholders for values passed to the function.



 



Example:



 





#include
<stdio.h>



void
greet(char name[]) {



    printf("Hello, %s!\n", name);



}



 



int
main() {



    char username[] = "John";



    greet(username); // Function call



    return 0;



}





 



 



The program defines a function `greet()` that takes
a character array as a parameter and prints a greeting message. The `main()`
function calls the `greet()` function with the `username` array. Compile and
run the program to see the output.



 



7.3
Function Return Type:



The return type of a function specifies the type of
value that the function returns. It can be any valid C data type or `void` if
the function doesn't return a value.



 



Example:





#include
<stdio.h>



int
square(int num) {



    int result = num * num;



    return result;



}



 



void
printMessage() {



    printf("This is a void
function.\n");



}



 



int
main() {



    int num = 5;



    int squared = square(num); // Function call



    printf("Squared: %d\n", squared);



 



    printMessage(); // Function call



 



    return 0;



}





 



 



The program defines a function `square()` that takes
an integer parameter and returns its square. The `main()` function calls the
`square()` function and prints the result. It also defines a `printMessage()`
function with a `void` return type, which doesn't return any value. Compile and
run the program to see the output.



 



7.4
Function Prototypes:



A function prototype provides the function's declaration
without the implementation. It allows you to use a function before its
definition, which can be useful when functions depend on each other.



Example:



 





#include
<stdio.h>



//
Function prototype



int
multiply(int a, int b);



 



int
main() {



    int num1 = 5, num2 = 3;



    int product = multiply(num1, num2); //
Function call



    printf("Product: %d\n", product);



    return 0;



}



 



//
Function definition



int
multiply(int a, int b) {



    int result = a * b;



    return result;



}





 



The program provides a function prototype for
`multiply()` before the `main()` function. This allows `main()` to call
`multiply()` even though its definition comes later in the program. Compile and
run the program to see the output.



 



7.5
Recursive Functions:



A recursive function is a function that calls
itself. It can be used to solve problems that can be broken down into smaller,
similar subproblems.



 



Example:



 





#include
<stdio.h>



int
factorial(int n) {



    if (n == 0 || n == 1) {



        return 1;



    } else {



        return n * factorial(n - 1);



    }



}



 



int
main() {



    int num = 5;



    int fact = factorial(num);



    printf("Factorial of %d: %d\n",
num, fact);



    return 0;



}





 



 



The program defines a recursive function
`factorial()` that calculates the factorial of a number. The `main()` function
calls `factorial()` and prints the result. Compile and run the program to see
the output.



 



That concludes our exploration of functions in C. In
the next chapter, we will cover arrays, which allow for storing and
manipulating multiple values of the same type.







 

Post a Comment

0 Comments