Free Ebook cover Complete Logic Programming Course for Beginners

Complete Logic Programming Course for Beginners

4

(3)

83 pages

Data Types: Functions

Capítulo 12

Estimated reading time: 3 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

In programming, a function is a set of instructions that performs a specific task. Functions are fundamental for structuring a program, as they allow code reuse and organization in logical and independent blocks. In this chapter, we'll explore the concept of functions, their types, and how they're used in programming logic.

Functions: A Definition

A function is basically a sequence of instructions that performs a specific task. In a program, a function is defined by a unique name and can be called (or invoked) from anywhere in the program. When the function is called, the instructions contained in it are executed. Once the execution of the function is complete, control is returned to the point in the program where the function was called.

Function Types

There are two main types of functions in programming: predefined functions and user-defined functions.

Predefined Functions

Predefined functions are those that are already available in the programming language and can be used directly in programs. For example, in many programming languages, there are predefined functions to perform mathematical operations, string manipulation, file manipulation, and more.

User Defined Functions

User-defined functions are those that are created by the programmer to perform specific tasks that are not covered by predefined functions. To create a user-defined function, the programmer needs to define the name of the function, the parameters the function accepts (if any), and the instructions the function is to execute.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

How to Use Functions

To use a function in a program, you need to do two things: define the function and call the function.

Defining a Function

To define a function, you need to specify the name of the function, the parameters the function accepts (if any), and the statements the function should execute. The exact syntax for defining a function varies depending on the programming language you're using, but usually involves using keywords like 'function' or 'def'.

Calling a Function

Once you define a function, you can call it from anywhere in your program. To call a function, you need to use the function name followed by parentheses. If the function accepts parameters, you need to enclose the parameter values ​​in the parentheses.

Example Function

To illustrate the concept of functions, let's consider a simple example. Suppose you want to create a program that calculates the average of three numbers. You could define a function called 'average' that takes three parameters and returns the average of the three numbers.

function average(a, b, c) {
    return (a + b + c) / 3;
}

Once you define the 'media' function, you can call it from anywhere in your program. For example, you could call the 'average' function to calculate the average of three numbers and print the result.

var result = average(10, 20, 30);
console.log(result);

In short, functions are a powerful tool in programming that allow you to reuse code and organize your program into logical, independent blocks. Functions can be predefined or user defined depending on the needs of the program.

Now answer the exercise about the content:

What are the two main types of functions in programming and how are they defined?

You are right! Congratulations, now go to the next page

You missed! Try again.

The two main types of functions in programming are predefined functions and user-defined functions. Predefined functions are already available in the programming language for use, such as mathematical operations. User-defined functions are created by the programmer to perform specific tasks not covered by predefined functions.

Next chapter

Data Types: Arrays

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.