Let's use the example function f(x) = x^2
This should be very familiar to you.
If i gave you mathematical instructions to solve the values for a and z, could you do it?
a= f(2)
y = 5
z = f(y)
What is the value of a and z?
a = 4
z =25
You got the answer by using the above function. The function f(2) got replaced by whatever f(2) returned.(In this case it was 4 because x was replaced by the number 2)
In the case of f(y), y is equal to 5 so x in f(x) get's replaced by 5 and f(y) returned the value 25.
/* File: FirstFunction.c
By: Christopher Manloloyo
This Program is used to learn about functions
The function that is implemented is f(x)=x^2 for integers
*/
#include<stdio.h>
int f(int x); // This is known as the prototype
int main()
{
// declare variables
int a, y, z;
a = f(2); // the first function call
y = 5;
z = f(y); // the second function call
/* Printing the Results To the Screen */
printf("a = %d \n", a);
printf("z = %d \n", z);
}
//notice the main function ends above
//The function f starts here
int f(int x) // This part is the function definition
{
int result;
result = x*x;
return result
// Alternatively, you could just use the code below
// return x*x;
}