Monday, April 28, 2025

C Tutorial - 17 - Functions

What are Functions in Programming and why do we need them

Functions are blocks of code that are designed to perform a specific task. They are self-contained and can be used repeatedly within a program, which makes code more modular, easier to understand, and easier to maintain.

Functions in programming provide a way to break down complex problems into smaller, more manageable pieces. By using functions, a programmer can divide a program into smaller, more manageable pieces that can be developed, tested, and debugged independently. Functions can be used to improve the readability of code by breaking it up into smaller, more understandable parts.

Functions are also useful for code reuse. When a particular task needs to be performed multiple times, it is more efficient to create a function that can be called repeatedly rather than writing the same code over and over again. By using functions, a programmer can avoid duplicating code and reduce the chances of introducing errors.

Overall, functions are an essential part of programming because they allow programmers to create more organized, efficient, and reusable code.

 

Example of multiple Functions

#include <stdio.h>

void someFunction()
{
    printf("I am from Some Function \n");
}

void callerMITM()
{
    someFunction();
    someFunction();
    someFunction();
}

void simpleCalc()
{
    int x = 10;
    int y = 20;
    int result = x + y;

    printf("--------------------- \n");
    printf("Result: %d \n", result);
    printf("--------------------- \n");

}

void pingGoogle()
{
    system("ping google.com");
}

int main()
{

    //callerMITM();
    //simpleCalc();
    //system("calc");
    pingGoogle();

    return 0;
}

This program defines several functions, namely someFunction(), callerMITM(), simpleCalc(), and pingGoogle().

someFunction() simply prints a message to the console.

callerMITM() calls someFunction() three times.

simpleCalc() defines two integer variables, x and y, adds them together, and prints the result to the console.

pingGoogle() uses the system() function to execute the command ping google.com, which sends a ping request to Google's servers and displays the results in the console.

In main(), different functions are called by uncommenting the relevant lines of code. callerMITM() calls someFunction() three times, simpleCalc() adds two integers and prints the result to the console, and pingGoogle() sends a ping request to Google's servers and displays the results in the console.

 

Why do we need to use () while naming functions

In C programming, parentheses are used to denote a function. When we declare a function, we use parentheses to indicate that it's a function and not a variable or some other data type. For example, if we want to declare a function named "myFunction", we write it as:

void myFunction() {
    // code here
}

Here, the parentheses after "myFunction" indicates that it is a function. When we call a function, we also use parentheses to pass arguments to the function. For example:

myFunction(arg1, arg2);

Here, the parentheses after "myFunction" indicate that we are calling a function, and the arguments are enclosed within the parentheses.

Therefore, parentheses are an essential part of function declaration and calling in C programming.

 

What is void

In programming, void is a keyword used to indicate that a function does not return any value. When a function is declared with a void return type, it means that the function will perform some operations but will not return a value.

For example, the following function takes two integers as input and prints their sum, but does not return any value:

void sum(int a, int b) {
    int result = a + b;
    printf("The sum is: %d", result);
}

Since the return type is void, we know that the function will not return any value, but will only print the result on the console.

 

Does that mean "Direct Action Fuctions"

Void is a data type in C that is used to indicate the absence of a value. When a function's return type is void, it means that the function does not return any value. In other words, it is a direct action function that performs a task or operation without returning a value. This is useful for functions that perform actions such as printing output or modifying variables, but do not need to return a specific value to the caller.

 

What "Return from Function" means

A function in programming can return a value to the calling program or function using the return statement. When a function is called, the calling program or function may need to receive a value or information from the called function. This is where the return statement comes in.

The return statement in a function is used to return a value or information to the calling program or function. The value or information returned by the function can then be used by the calling program or function.

In some cases, a function may not need to return a value. In such cases, the void keyword is used in the function definition to indicate that the function does not return a value. The void keyword can also be used in place of a return type in function prototypes to indicate that the function does not return a value.

 

Do we need to be specific about data type that function returns

Yes, it is important to specify the data type that a function returns. The data type that a function returns indicates what kind of data the function will return to the caller. If a function does not return any data, you can use the void data type to indicate this. However, if a function does return a value, you should specify the data type of the returned value in the function declaration and definition, using the appropriate data type keyword (e.g., int, float, char, etc.). This helps to ensure that the data returned by the function is correctly interpreted and used by the calling code.

 

Can we define functions inside main() function

No, functions cannot be defined inside main() function in C programming language. All functions in C must be defined outside of main() function. It is possible to declare a function inside main() function, but the function definition must be outside of main().

For example, the following code is valid:

#include <stdio.h>

void myFunction(); // function declaration

int main() {
    myFunction(); // function call
    return 0;
}

void myFunction() { // function definition
    printf("Hello, world!");
}

However, defining a function inside main() function is not allowed in C.

 

What is function "declaration" and "definition"

In C programming, a function declaration tells the compiler about the existence of a function and the data types of its parameters and return value, but does not provide the actual implementation of the function.

A function definition, on the other hand, not only declares the function but also provides the actual code that implements the function.

In other words, a function declaration is a promise to the compiler that the function will be defined later in the program, while a function definition provides the actual implementation of the function.

Here's an example of a function declaration:

int addNumbers(int a, int b);

And here's an example of a function definition:

int addNumbers(int a, int b)
{
    return a + b;
}

In this example, the function addNumbers is declared with its parameter data types and return value data type in the declaration. Later, the function is defined with the actual code that adds the two input parameters and returns the result.

 

Can we have function declaration after main() function

Yes, we can have function declaration after the main() function, as long as the function is defined before it is called. This is because function declarations only provide the function prototype (the function signature with its parameter list and return type) to the compiler, allowing it to recognize the function's name and signature when it is called elsewhere in the program. The actual function definition can be located elsewhere in the program or even in a separate source file. However, it is generally considered good programming practice to declare functions before they are called to ensure that the program is properly organized and easy to read.

 

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...