Introduction to C Functions

A function is a named block of code that performs a specific task and optionally returns a value. A function is named. Every function has a unique name; which can be used to call the function from within another function. This function may pass information back to the function that called it in the form of a return value.

#include <stdio.h>


void firstFunction(){
 
  prinf("\n - This is our first function. - \n");
 
}


int main(void){
 
  printf("Let's call the function we defined!\n");
 
  firstFunction();
 
  printf("Let's call it again!\n");
 
  firstFunction();
 
  return 0;
 
}

To pass arguments to a function, we list them in parentheses following the function name. Be aware that the number of arguments and the type of each argument must match the parameters in the function definition. If a function has multiple arguments, the arguments are listed in order.

#include <stdio.h>

int cube(int val){
  return val * val * val;
}

int main(void){
 
  int x = 2;
  int y = 6;
 
  printf("The value of %d cubed is %d\n", x, cube(x));
  printf("The value of %d cubed is %d\n", y, cube(y));
 
  return 0;
 
}

A function definition contains the code that will be executed. The first line of a function definition is called the function header. The function header gives the function’s name. The header also gives the function’s return type and describes its arguments. A function header shouldn’t end with a semicolon. Following the header is the function body, containing the statements that the function will execute. The function body begins with an opening curly brace and ends with a closing curly brace.

#include <stdio.h>


void function1(int arg1){
  printf("\nBegin function1()\n");
  printf("function1() received the argument %d\n", arg1);
  printf("End function1()\n\n");
}

void function2(double arg1, char arg2){
  printf("\nBegin function2()\n");
  printf("function2() received the arguments:\n");
  printf("%f and %c\n", arg1, arg2);
  printf("End function2()\n");
}


int main(void){
  printf("Begin function main()\n");
 
  function1(42);
  function2(802.11, 'n');
 
  printf("End function main()\n");
 
  return 0;
 
}

Although the two terms are often used interchangeably, a parameter and an argument are different. A parameter is an entry in the function header; it acts as a placeholder for an argument. An argument is an actual value passed to the function by the calling program.

A function should know what kind of argument to expect, this means that the function must know the data type of each argument. A value of any of C’s data types may be passed to a function. The argument type is specified in the parameter list part of the function header. For each argument passed to the function, the parameter list must contain one item.

 #include <stdio.h>

double functionA(int x, double y){
   return x * y;
}

long long functionB(int y, int z){
  return (y*y) + (z*z);
}

int main(void){
  int a = 1999;
  int b = 1138;
 
  double y = 1.618;
  double z = 2.718;
 
 
  printf("%d * %f = %f\n", a, y, functionA(a, y));
  printf("%d * %f = %f\n", b, z, functionA(b, z));
  printf("%d^2 + %d^2 = %lld\n", a, b, functionB(a, b));
 
  return 0;
    
}

A function prototype is a model for a function that will appear later in the program. A program should include a prototype for each function that it uses. The prototype for a function is the same as the function header, only with a semicolon added to the end. Like the function header, the function prototype contains information about the function’s return type, name, and parameters.

#include <stdio.h>

double byHalf(double a);

int main(void){
 
  printf("Half of 4.4 is %f\n", byHalf(4.4));
  printf("Half of 7 is %f\n", byHalf((double)7));
  printf("Half of 12 is %f\n", byHalf((double)12));
 
 
  return 0;
 
}


double byHalf(double a){
  return a / 2.0;
}

Function prototypes should be placed before the start of the first function. Typically, function prototypes are all grouped together in one location.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s