Docsity

Mathematics engineering, Schemes and Mind Maps of Computer science

Partial preview of the text

Download Mathematics engineering and more Schemes and Mind Maps Computer science in PDF only on Docsity!

Computing Fundamentals

Muhammad Irtaza Saleem

Slide Courtesy - Dr. Muhammad Yousaf Hamza

Deputy Chief Engineer, PIEAS

FUNCTIONS

// Addition of two integers using

Function

#include int addnumbers(int x, int y); int main() { int num1, num2, add_result; printf("Enter first number\n"); scanf("%d", &num1); printf("Enter second number\n"); scanf("%d", &num2); add_result = addnumbers(num1, num2); printf("Addition result is %d\n",add_result); getchar(); return 0; }

int addnumbers(int x, int y)

int z;

z = x +y;

return z;

• The function is one of the most important topic to

understand in C programming.

• A function groups a number of program statements

into a unit and gives it a name. This unit can then

be invoked (called) from other parts of the

program.

• A function is a sub-unit of a program which

performs a specific task.

• Functions are modules in c.

• Functions take arguments (constants values or

variables) and may return a value.

Functions

• The most important reason to use functions is to

aid in the conceptual organization of a program.

Dividing a program into functions is, one of the

major principles of structured programming.

• Another reason to use functions is to reduce

program size.

• The function ’ s code is stored in only one place in

memory, even though the function is executed

many times in the course of the program.

Benefits of Functions

Functions

• A function receives zero or more parameters,

performs a specific task, and returns none or

one value.

• A function is invoked by its name and

parameters.

  • No two functions have the same name in your C

program.

  • The communication between the function and

invoker is through the parameters and the return

value.

• A function is independent.

  • It is “completely” self - contained.
  • It can be called at any places of your code.

• Functions make programs reusable and

readable.

More Terminologies

Function Terminologies

#include

int addnumbers(int x, int y);

int main() { int num1 = 10, num2 = 20, add_result;

add_result = addnumbers(num1,

num2); printf (“Result is %d \n",add_result); getchar(); return 0; } int addnumbers(int x, int y) { int z; z = x +y; return z; }

Function Prototype

// Addition of two integers using

Function

#include

int addnumbers(int x, int y);

int main() { int num1, num2, add_result; printf("Enter first number\n"); scanf("%d", &num1); printf("Enter second number\n"); scanf("%d", &num2);

add_result = addnumbers(num1,num2);

printf("Addition result is %d\n",add_result); getchar(); return 0; }

int addnumbers(int x, int y)

int z;

z = x +y;

return z;

• A function prototype means to declare the

function

(as you declare a variable prior to use)

• A prototype tells your C program what to expect

from a function - what arguments it takes (if

any) and what it returns (if any)

• Prototypes should go before main()

• A function MUST return the variable type we say

that it does in the prototype.

• #include finds the prototypes for library

functions (e.g. printf)

Function Prototype

Function Definition

// Addition of two integers using

Function

#include int addnumbers(int x, int y); int main() { int num1, num2, add_result; printf("Enter first number\n"); scanf("%d", &num1); printf("Enter second number\n"); scanf("%d", &num2); add_result = addnumbers(num1, num2); printf("Addition result is %d\n",add_result); getchar(); return 0; }

int addnumbers(int x, int y)

int z;

z = x +y;

return z;

Function Header

// Addition of two integers using

Function

#include int addnumbers(int x, int y); int main() { int num1, num2, add_result; printf("Enter first number\n"); scanf("%d", &num1); printf("Enter second number\n"); scanf("%d", &num2); add_result = addnumbers(num1, num2); printf("Addition result is %d\n",add_result); getchar(); return 0; }

int addnumbers(int x, int y)

int z;

z = x +y;

return z;

/* The first line of the

function definition (as

shown in blue color

above) is called Function

Header. */

• Function definition format (continued)

return-value-type function-name( parameter-list )

declarations and statements

– Declarations and statements: function body

(block)

  • Variables can be declared inside blocks
  • Functions can not be defined inside other functions

Function Definition

Invoking a Function

• Invoking a function means calling a function to use

it.

• Provide function name and arguments (data)

• Function performs operations or

manipulations

• Function returns results

• Function call analogy:

• Boss asks worker to complete task

– Worker gets information, does task, returns

result

– Information hiding: boss does not know

details