cs:c_language:functions_1
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/functions_1?do=diff&rev2%5B0%5D=1551188114&rev2%5B1%5D=1606429133&difftype=sidebyside
Differences
This shows you the differences between two versions of the page.
— | cs:c_language:functions_1 [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Functions (Example 1) ====== | ||
+ | **Concepts: | ||
+ | Very simple function with arguments passed by value | ||
+ | |||
+ | **Text:**\\ | ||
+ | * Write a function that receives as arguments two float numbers (a and b) | ||
+ | * The function multiplies these two values and it returns the result of the multiplication | ||
+ | * The produced function must be applied to a vector with name '' | ||
+ | * The result of the multiplication must be stored in a vector of name '' | ||
+ | |||
+ | |||
+ | **Solution: | ||
+ | <file C functions_1.c> | ||
+ | /* | ||
+ | Write a function that receives as input two float numbers (a and b), | ||
+ | it multiplies them and it returns the result of the multiplications. | ||
+ | The function must be applied to a vector of name vet in order to multiplicate by 3.0 | ||
+ | each element of the vector. | ||
+ | The result of the multiplication must be stored in a vector of name res with the same dimension | ||
+ | of vet. | ||
+ | */ | ||
+ | |||
+ | |||
+ | #include < | ||
+ | #define SIZE 10 | ||
+ | |||
+ | /* Prototype */ | ||
+ | float mul(float a, float b); | ||
+ | |||
+ | int main(){ | ||
+ | |||
+ | float vet[SIZE] = {1.0, 5.0, 3.0, 7.0, 10.0, 1.0, 2.0, 3.0, 4.0, 5.0}; | ||
+ | float res[SIZE]; | ||
+ | int i; | ||
+ | |||
+ | /* Multiplication of each element of vet by 3.0 and storing of the result in res */ | ||
+ | for(i=0; i < SIZE; i++){ | ||
+ | res[i] = mul(vet[i], 3.0); | ||
+ | } | ||
+ | |||
+ | /* Print results, i.e., the content of vector res */ | ||
+ | for(i=0; i < SIZE-1; i++){ | ||
+ | printf(" | ||
+ | } | ||
+ | printf(" | ||
+ | | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | |||
+ | /* Implementation of the function mul that performs the multiplication between a and b */ | ||
+ | float mul(float a, float b){ | ||
+ | float result; | ||
+ | |||
+ | result = a*b; | ||
+ | |||
+ | return result; | ||
+ | } | ||
+ | |||
+ | /* The function mul can be also implemented in the following way */ | ||
+ | /* | ||
+ | float mul(float a, float b){ | ||
+ | return a*b; | ||
+ | } | ||
+ | */ | ||
+ | </ | ||
+ | |||
+ | **Comments: | ||
+ | * During the compilation process, the compiler must know in advance the prototypes of all the functions used in the program, before their calling. This is performed or by the ''# | ||
+ | <code c> | ||
+ | /* Prototype */ | ||
+ | float mul(float a, float b); | ||
+ | </ | ||
+ | indicates that in the following will be used the function with name '' | ||
+ | <code c> | ||
+ | /* Prototype */ | ||
+ | float mul(float, float); | ||
+ | </ | ||
+ | **Note:** If you write the // | ||
+ | * Analyzing the program: | ||
+ | <code c> | ||
+ | int main(){ | ||
+ | |||
+ | float vet[SIZE] = {1.0, 5.0, 3.0, 7.0, 10.0, 1.0, 2.0, 3.0, 4.0, 5.0}; | ||
+ | float res[SIZE]; | ||
+ | int i; | ||
+ | |||
+ | /* Multiplication of each element of vet by 3.0 and storing of the result in res */ | ||
+ | for(i=0; i < SIZE; i++){ | ||
+ | res[i] = mul(vet[i], 3.0); | ||
+ | } | ||
+ | |||
+ | /* Print results, i.e., the content of vector res */ | ||
+ | for(i=0; i < SIZE-1; i++){ | ||
+ | printf(" | ||
+ | } | ||
+ | printf(" | ||
+ | | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | after the declaration and the initialization of the vector '' | ||
+ | <code c> | ||
+ | res[i] = mul(vet[i], 3.0); | ||
+ | </ | ||
+ | To perform the multiplication, | ||
+ | The following line of code: | ||
+ | <code c> | ||
+ | res[i] = mul(vet[i], 1.0+2); | ||
+ | </ | ||
+ | would have obtained the same result, as well as the following line of code that does not make use of the function '' | ||
+ | <code c> | ||
+ | res[i] = vet[i]*3.0; | ||
+ | </ | ||
+ | The second '' | ||
+ | * The implementation of the function '' | ||
+ | < | ||
+ | /* Implementation of the function mul that performs the multiplication between a and b */ | ||
+ | float mul(float a, float b){ | ||
+ | float result; | ||
+ | |||
+ | result = a*b; | ||
+ | |||
+ | return result; | ||
+ | } | ||
+ | </ | ||
+ | In practice, the values passed to the function (i.e., '' | ||
+ | * The function '' | ||
+ | <code c> | ||
+ | float mul(float a, float b){ | ||
+ | return a*b; | ||
+ | } | ||
+ | </ | ||
+ | in this case it does not use the variable '' |
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/functions_1?do=diff&rev2%5B0%5D=1551188114&rev2%5B1%5D=1606429133&difftype=sidebyside
/web/htdocs/www.skenz.it/home/data/pages/cs/c_language/functions_1.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1