Exforsys

Home arrow Technical Training arrow C Tutorials

C Programming - Functions (Part-II)

Author:      Published on: 22nd May 2006    |   Last Updated on: 22nd Jul 2011

Types of Functions

You may have noticed the discussion about the <data type> portion of the function declaration missing from the last section. The <data type> refers to the type of data that would be returned by the function when it finishes executing. Data types are the same as any variable data types plus another one called void. Void type functions do not return any values.

Ads

Function with no arguments and no return value:

Sample Code
  1.  void function_name();
Copyright exforsys.com


Think of this as a self-contained block of reusable code that does not need any data from the outside or have to return any data back to its caller. Functions that print values are good candidates for this type of function.

Sometimes you have a list of data you want to print to the console and you keep copy and pasting it or retyping it over and over again in different places. This practice is error prone and unprofessional. Consider the following example.

Sample Code
  1.  void main() {
  2. int x = 10;
  3. int y = 5;
  4.  
  5. printf("value of x: %d", x); printf("value of y: %d", y); printf("n");
  6. x += y;
  7. printf("value of x: %d", x); printf("value of y: %d", y);
  8. y++;
  9. printf("value of x: %d", x); printf("value of y: %d", y);
  10. }
Copyright exforsys.com


The quality of this program would greatly be improved by moving the print statements into a function. printxy1.c:

Sample Code
  1.  #include <stdio.h></stdio.h>
  2. //global variables. int x, y;
  3. //Function for printing variable's values.
  4. void printxy();
  5. void main() {
  6. x = 10;
  7. y = 5;
  8. printxy();
  9. x += y;
  10. printxy();
  11. y++;
  12. printxy();
  13. }
  14. void printxy() { printf("value of x: %d, ", x); printf("value of y: %d", y); printf("n");
  15. }
Copyright exforsys.com


Now instead of retyping the same three lines over and over, you simply make a call to the function to perform the exact same task in a more readable and maintainable manner.

Here is the output.

Function with arguments and no return value:

Sample Code
  1.  void function_name(int var);
Copyright exforsys.com


Did you notice in the last example that we had to move the x and y variables to the global scope in order to use this function type. This should be avoided whenever possible. Global variables are considered bad practice, but in reality there are times (especially in embedded systems development) where you just have to give in.

In our case we can fix it by using a function that takes arguments and does not have to return a value to its caller.

Let us change our function declaration from the last example to this:

Sample Code
  1.  void printxy(int val1, int val2);
Copyright exforsys.com


This function declaration has a parameter list which is just a comma-separated list of variables that will be used within the function that the caller can assign values when the function is called.

Here is a good place to introduce the idea of function signatures. A signature is just a function declaration or prototype, but it is called as such because no two functions may have the same signature or the compiler will not be able to resolve the call to a function. The signature is made up of the data type, function name, and the parameter list.

Let us fix our program so that it does not need to rely on global variables. Changes are in bold.

printxy2.c:

Sample Code
  1.  #include <stdio.h></stdio.h>
  2. //Function for printing variable's values. 2 arguments, no return value.
  3. void printxy(int x, int y);
  4. void main() {
  5. int x, y;
  6. x = 10;
  7. y = 5;
  8. printxy(x, y);
  9. x += y;
  10. printxy(x, y);
  11. y++;
  12. printxy(x, y);
  13. }
  14. void printxy(int x, int y) { printf("value of x: %d, ", x); printf("value of y: %d", y); printf("n");
  15. }
Copyright exforsys.com


All we did was change the signature of our printxy function so it now takes two arguments and prints those values. Main is the caller, and passes the new function the values it currently has for x and y when it makes the call. Now we do not need to use global variables to access x and y, we can just pass local variables as arguments.

Here is the output.

Functions with no arguments and a return value:

Sample Code
  1.  <data type></data> function_name();
Copyright exforsys.com


A function of this form takes no arguments, but does return a value of some variable local to the function back to its caller. The data type refers to what kind of data is returned by the function on exiting.

Sometimes global flags are used in a program to indicate the current state of something, this is very common in embedded systems applications or system level programming. That is one thing you might use this type of function for.

Instead of inventing some useless function, let us use the standard C function from the stdio.h library as an example. This is the prototype for the getchar function:

Sample Code
  1.  int getchar();
Copyright exforsys.com


This function returns a character input from standard input as an integer. Although you would most likely set a char variable with this function.

Sample Code
  1.  char character = getchar();
Copyright exforsys.com


That would set character to the value returned by getchar once it finished executing.

Function with arguments and a return value:

Sample Code
  1.  <data type></data> function_name(<data type></data> arg1,
  2. <data type></data> arg2, ...);
Copyright exforsys.com


This type allows you to use a variable from an external source within the scope of the function, and you can return data back to the caller.

Main is actually a function of this type, we just have not been passing it arguments or using it is return values for anything. It is not necessary to use or catch a function's return value but you can if you want to.

Usually main takes the following form:

Sample Code
  1.  int main(int argc, char *argv[]);
Copyright exforsys.com


For now, ignore the odd looking second parameter, its explanation is reserved for the pointers topic. This form of main is very useful because it allows your program to receive command-line arguments and return data. Unix, DOS, and Linux programmers know about this if they ever use terminal commands or write bash scripts that rely on the output of a command.

Let us modify our printxy example to show the name of the program before it runs and a message telling when it is finished. Ignore the fact that the argv argument is a pointer to an array of chars for now and focus on the fact that we are able to use the values passed through these function arguments.

Below is the latest source code for our printxy example. Changes are in bold.

printxy3.c:

Sample Code
  1.  #include <stdio.h></stdio.h>
  2. #include <stdlib.h></stdlib.h>
  3. //Function for printing variable's values. 2 arguments, no return value.
  4. void printxy(int x, int y);
  5. int main(int argc, char *argv[]) {
  6. int x, y;
  7. x = 10;
  8. y = 5;
  9. printf("%s is runningn", argv[0]); printxy(x, y);
  10. x += y; printxy(x, y);
  11. y++; printxy(x, y);
  12. printf("%s is finishedn", argv[0]);
  13. return 0;
  14. }
  15. void printxy(int x, int y) { printf("value of x: %d, ", x); printf("value of y: %d", y); printf("n");
  16. }
Copyright exforsys.com


The function main takes two arguments, argc and argv, we use the value in argv to print the name of the program and a message about the state of the program. Main returns 0 to indicate that it completed successfully.

You may be wondering where the program name was passed to main from. It is passed automatically by the command line as the first element in the argv array. In case you were wondering, argc is the number of arguments passed by the command-line when the program is launched from the terminal.

Here is what the output of printxy3 should look like.

Ads



 
This tutorial is part of a C Tutorials tutorial series. Read it from the beginning and learn yourself.

C Tutorials

 

Comments