What is function in C language?

I just learn computer programming for my it course. I dun understand what is function and return data type, function name,parameter list?
What used for them in programming
i always start my program using
1
2
3
4
#include <stdio.h>
int main()
{
}


what is int main means for function name and return data type?
A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.
Learn C here: https://hackr.io/tutorials/learn-c

You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task.

A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.

The C standard library provides numerous built-in functions that your program can call. For example, strcat() to concatenate two strings, memcpy() to copy one memory location to another location, and many more functions.

A function can also be referred as a method or a sub-routine or a procedure, etc.

Defining a Function
The general form of a function definition in C programming language is as follows −

return_type function_name( parameter list ) {
body of the function
}
A function definition in C programming consists of a function header and a function body. Here are all the parts of a function −

Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.

Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature.

Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.

Function Body − The function body contains a collection of statements that define what the function does.

Example
Given below is the source code for a function called max(). This function takes two parameters num1 and num2 and returns the maximum value between the two −

/* function returning the max between two numbers */
int max(int num1, int num2) {

/* local variable declaration */
int result;

if (num1 > num2)
result = num1;
else
result = num2;

return result;
}
what does return result means in your function? can you give example for given return data type for double , float and character?
The 2 following code do the same. You should understand what return does with those examples:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

bool useless_function () {
    return false;
}

int main() {
	bool condition = useless_function();
}


1
2
3
4
5
6
#include <iostream>
using namespace std;

int main() {
	bool condition = false;
}
you means the return means the result of the function?
Basically, yes.

But keep in mind you can return "different stuff".

For example all these functions return false

1
2
3
bool useless_function () {
    return false;
}


1
2
3
4
bool useless_function () {
    bool result = false;
    return result;
}


1
2
3
bool useless_function () {
    return 0;
}


1
2
3
bool useless_function () {
    return (0 > 1);
}


I program in C++, but I believe this also works in C you should try it, in the case I am wrong.
Last edited on
C does not have bool as a built-in type. C99 has _Bool which is also accessible as bool

Traditionally C programmers just use 0 for the concept of false, anything else is the concept of true.

C does not have true or false as keywords, but there are macros true and false

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>

int main (void) {

_Bool MyBool = 1;

if (MyBool) {

  printf("_Bool is true!\n");
}
else {
  printf("_Bool is false!\n");
}

return 0;
}


Compiled with:
gcc -std=c99 -Wall -Wextra -pedantic-errors *.c -o TestBool

run with this (on a linux terminal):
$ ./TestBool

_Bool is true!


To use bool, true and false, #include <stdbool.h>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <stdbool.h>

int main (void) {

bool MyBool = true;

if (MyBool) {

  printf("MyBool is true!\n");
}
else {
  printf("MyBool is false!\n");
}

return 0;
}

Topic archived. No new replies allowed.