CFORBEGINNERS.COM

C Functions

Quick Note

Functions are very powerful programming tools that is used in almost every program. Functions allow programmers to break up code into different jobs and test those functions individually. In other words, instead of having hundreds or even thousands of line in your main program (int main ()) you can have different functions and call upon them whenever you desire. The best way to learn is to do, shall we?

int my_function(int n)

Here is what a function looks like. The first int is telling what data type will be returned just like how we put int main. My_function is just simply the name of the function, I could have called this anything I like, the reason why I put an underscore is because when you name functions, data types, are really anything else in C it is illegal to have spaces between words, so replace the space with an underscore and you'll be fine. (int n) is telling the compiler what will be going into the function in this case n and n will be an integer hence int. Whenever you use a function your first have to prototype it. The prototype must go in between the pre-processor directives and int main(). Prototyping is something that is required in C programming, this is basically telling the compiler hey this is a function that I will be using so heads up. The easy part is that prototyping a function is literally typing in the same code I did above and adding a semicolon so that it would like this. Outside of your main program is where you will actually define the function, this is where you write the code of what the function is to do.

int my_function(int n);

So lets take a look at a program that uses a function to find the greater of two numbers a and b.

#include <stdio.h>
#include <stdlib.h>

int my_function(int a, int b);
/*This is the prototype, notice how it's above main. This function takes in two arguments int a and int b*/ int main() {
int a,b;
printf("Please enter a number");
scanf("%i",&a);
printf("Please enter another number");
scanf("%i",&b);
printf("The greater number is %i",my_function(a,b) );
/*This is how you call the function, its name and what variables you are sending it*/
system("pause"); return 0; } /*This is the part where we actually define the function, that is, write what the function is to do,
also notice how we define the function completely out of main, this is how it will always be done*/
int my_function(int a, int b) { if (a>b) { return a; /*If a is greater than b we want the function to return just a*/ } if (b>a) { return b; /*If b is greater than a than we want the function to return just b*/
} }

You may already be asking yourself, couldn't we of just done this simple procedure in the main program, well yes! In order to keep complexity at a minimum this is a good program to demonstrate. However, in more complex programs functions are very useful and necessary. NOTE! Notice how we scanned in a and b and in our function our arguments were also a and b, it isn't required that the variable be the same, I could have just as easily declared x any y in my main program and sent those variables to my_function. It is required however, for your prototype and your defining of a function to be identical (aside from the prototype having a semicolon after it and the defining not). This can be a little confusing at first but practice makes perfect! Lets check out another program.

#include <stdio.h>
#include <stdlib.h>

double add(double a, double b); 
/*Prototype, this functions name is add and will accept to double parameters a & b and then return a double*/
int main() { double x,y; printf("Enter a value\n"); scanf("%lf",&x); printf("Enter another value to be added to the other value\n"); scanf("%lf",&y); printf("The sum of the two numbers is %.2f\n",add(x,y) ); system ("pause"); return 0; } double add(double a, double b) /*Defining the function*/ { double sum; /* Declaring a local variable called sum*/ sum=a+b;
return sum;
}

This program has a function that takes in two values, adds them, and then returns their sum. Notice how in the function there is two double values a and b but in the main program x and y is being sent, this is okay because the variables do not have to match. This is because x and y's values are being sent to a and b, as long as they are of the same data type (in this case double) you will be fine. Also notice how I declared a variable inside a function, whenever this is done the variable is local and is not used in the main program. Sum is just holding the sum of a and b and that is why the value of sum is what gets returned to the main program. The thing with functions is that when you pass a variable to them it is actually making a copy of that variable. It is this copy that it uses withing the function, this is a process called pass by value, this procedure is safe because it doesn't allow a variable to be altered in the function, the con however, is sometimes you want to alter a variable within a function. How do you get around this? Instead of passing a copy of the variable we pass its address, this way we are allowed to change a variables value. The next lesson will go over pointers, a handy tool that holds variables addresses.