C is a language that allows and even facilitates the structured programming, which in theory can be applied in any high-level programming language. In fact today there is practically no more language that encourages unstructured programming, even some that encouraged today have stronger dialects that does not encourage or practically does not allow.
The paradigm is usually applied in code. Most programming languages allow you to use any paradigm. Of course they facilitate some more than others. Obviously similar paradigms are more easily adapted.
There are some "versions" of structured programming. One of the most accepted is that you cannot use goto
indiscriminately and give preference to more organized flow control structures such as while
or functions, for example. It preaches avoiding noodle code. The use of functions to help flow is encouraged, but nothing is said but flow.
The term was important in the 60s and 70s when it was common to program in an unstructured way, privileging the optimization and not the legibility (remembering that before computers were much less powerful and the compilers were well simplified, not possessing optimizations. And the codes were very simple. Today it is more common to use the terms imperative and procedural.
Procedural
Indeed procedural programming is an evolution of structured programming, or at least a more specific definition. The procedural only encourages more modularization of the code, but it is not yet modular programming.
Imperative
The imperative paradigm (comparison with functional and declarative) defines that the code will be executed step by step, i.e., command by command, changing the state of the data, including the execution flow. Whether the flow is controlled in a structured way is a more evolved paradigm. The vast majority of programming languages mainstream are predominantly imperative with excellent structuring capabilities, and other organizations.
Analyzing the example
Interestingly the example code even shows some structure, but it is not a code with good structure. I know it’s just an example, but it violates a principle that is best explored today, which is the sole responsibility, in all three functions. This principle is the basis of the procedural paradigm and even more in the modular. A better written code would be more procedural and modular:
void imprimeSoma(int valor1, int valor2) { //preferi mudar o nome do que mudar a função
printf("\n\nSoma: %d", valor1 + valor2);
}
void imprimeSubtracao(int valor1, int valor2) { //se mantiver subtrai, não poderia imprimir
printf("\n\nSubtracao: %d", valor1 - valor2);
}
int pegaValor(char * texto) {
printf(texto);
int valor;
scanf("%d", &valor);
return valor;
}
int main(void) {
int valor1 = pegaValor("Valor 1: "); //é mais procedural deixar a variável local
int valor2 = pegaValor("Valor 2: ");
imprimeSoma(valor1, valor2); //os dados devem ser locais e passados para funções
imprimeSubtracao(valor1, valor2); //a função comunica com parâmetros
}
I put in the Github for future reference.
I don’t know if the code works.
Correct code http://ideone.com/JiLNhV
– KingRider