0% found this document useful (0 votes)
0 views

Functions

The document explains the concept of functions in programming, highlighting their benefits such as organization, simplification, and reusability. It provides examples of function definitions, including a 'cube' function and a 'swap' function, along with their prototypes and usage in a main function. The document illustrates how local variables and parameters work within these functions.

Uploaded by

Lyall
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Functions

The document explains the concept of functions in programming, highlighting their benefits such as organization, simplification, and reusability. It provides examples of function definitions, including a 'cube' function and a 'swap' function, along with their prototypes and usage in a main function. The document illustrates how local variables and parameters work within these functions.

Uploaded by

Lyall
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Functions

Inputs Output
Why Functions?

- Organization
- Simplification
- Reusability
A Function Definition

int cube(int input)


{
int output = input * input *
input;
return output;
}
Header
function name parameter list

return type int cube(int input)


{
int output = input * input * input;
return output;

}
Body
#include <stdio.h>

int cube(int input); Function prototype

int main(void)
{
int x = 2;
printf("x is %i\n", x);
x = cube(x);
printf("x is %i\n", x);
}

int cube(int input)


{
int output = input * input * input;
return output;
}
cube()'s locals

cube()'s parameters

main()'s locals

main()'s parameters
#include <stdio.h>
void swap(int a, int b);
int main(void)
{
int x = 1;
int y = 2;
swap(x, y);
printf("x is %i\n", x);
printf("y is %i\n", y);
}
void swap(int a, int b)
{
int tmp = a;
a = b;
b = tmp;
}

You might also like