0% found this document useful (0 votes)
2 views2 pages

Loops, Functions, ModulesC

The document explains the basics of loops, functions, and modules in programming. It covers various types of loops, how to create functions with return types and parameters, and the structure of modules using .c and .h files. Additionally, it highlights the importance of static variables and functions for preserving values and restricting function access to the defining file.

Uploaded by

fritzbillaspeer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Loops, Functions, ModulesC

The document explains the basics of loops, functions, and modules in programming. It covers various types of loops, how to create functions with return types and parameters, and the structure of modules using .c and .h files. Additionally, it highlights the importance of static variables and functions for preserving values and restricting function access to the defining file.

Uploaded by

fritzbillaspeer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Loop

-------
There are : while(condition){}, do{}while(condition), for(variable; condition;
incrementation or decrementation){}
***Caution!! Don't forget to increment or decrement the loop's variable to get out
of the loop.
For "do{}while", incrementation or decrementation is done in do-brackets.
When the loop will contain just one line of code, we put that whithout brackets
e.g: if(age< 18 && age >1) printf("You cannot drink alcool)

Function
---------
To create a function we must : Give the return'type of the function, the
function'name and the type
and the name of his parameter : type name(type pameter's name){}
e.g:
int location(int x){x = 100; return x;)
***We have to declare a function before the main function. Then, the variable which
will be used by
the function we created has to be too declare before the main function. If we
create a function
after the main function, we must use a prototype before the main function:
The prototype is formed by : type of the return'function we created after main,
name of his(type'var)
e.g:
int a;
int main(void) {
a = add(a);
printf("%d", a);
printf("hello world);
return 0;
int add(int number)
{number = 0; return number;}

***Non-void function cannot return a value


e.g: void Age(int age)
{
if(age==0) printf("you're not born yet");
else if(age>=18) printf("you can drink alcool");
else printf("You can not drink for the moment");

}
***A function can have no-parameter and no-return function
e.g: **void hello(void) printf("Hello, you're beautiful")

Modules
-----------
for creating a module, we need two principal things: a ".c" file and a ".h" file.
The ".c" file will contain all functions of the module; and the ".h" file(for Head
file)
will contain all prototypes of functions include in the ".c" file.
when all is done, to use the module, we must include the ".h" file in the place we
want to
use our module.

e.g
----
---------
player.c
---------
#include <stdio.h>

void hello(void) printf("Hello everyone\n");(write it correctly)


----------
player.h
---------
void hello(void);

--------
main.c
--------
#include <stdio.h>
#include "player.h"

int main(void)
{
hello();
return 0;
}

***Caution: we must compile all files at the same time : gcc *.c -o executable
name;
module's files (".c" and ".h") can get different names, but, it's usefull to give
them the
same one; to include a library we created, we have to put #include "file.h" on the
top of our
main file.
At the start of our ".h" file, we can add a "#ifndef __a name__" and "#define __the
same name__".
At the end of our ".h" file, we have to add "#endif". That process will help to
protect our code
of infinite inclusion. For the code meaning...Translate by yourself.

Static var or function is use to conserve the variables value no matter the end of
the function
because, normally, at the end of a function all variables are destroyed, but, the
keyword static
helps to conserve them.
In the case of functions, static means this function cannot be used in an other
file than the one
which contains the function.

You might also like